Here’s the whole program, explained. I start with a URL like this
https://www.asdf.io/api/feeds/320738?filter=123456
That end point (123456) can have multiple sets of numbers there, separated by a comma. This URL is basically a JSOn file (more on that later). My current issue is that I need to figure out if there are actually no numbers at the end of filter=
. Because that tells me that someone screwed up (to sum it up).
Now, I need to basically take a URL like this:
https://www.asdf.io/api/feeds/320738?filter=123456,234567
And separate it into URLs like this
https://www.asdf.io/api/feeds/320738?filter=123456
https://www.asdf.io/api/feeds/320738?filter=234567
Etc, however many numbers are at the end. EACH number here corresponds to a social media source. E.g. they can both be facebook, or one facebook, one twitter, etc. By separating out each URL, i can parse the JSON to determine which it is, and create URLs based on that.
So let’s say I have a URL with 5 numbers.
https://www.asdf.io/api/feeds/320738?filter=644679,616690,123456,234567,345678
Now, let’s say that the first 2 are facebook, then 2 twitters, and the last is an instagram. I parse each separated URL and combine the URLs (eventually). I’ll break down the program later. So that example will get turned into
https://www.asdf.io/api/feeds/320738?filter=644679,616690
https://www.asdf.io/api/feeds/320738?filter=123456,234567
https://www.asdf.io/api/feeds/320738?filter=345678
Now, below is how I call the function. I call the function like this
$('.home-social2 .fsFeeds.fsCustom').each(function() {
var self = $(this);
var options = {
filters: {
"instagram": 1,
"twitter": 2,
"facebook": 1
},
}
var feedInstance = new MultiRequestFeed(self, options, function() {
});
feedInstance.init();
});
However, if they want call this function with “twitter” and “2”, and I don’t find any URLs that are actually “twitter”, I need to create a URL like so
https://www.asdf.io/api/feeds/320738?filter=twitter
In it’s place.
The most important parts of my whole program is this
combineURLData(filteredURLs, options) {
var finalURLs = [];
var urlData = Object.entries(filteredURLs);
urlData.forEach(function(data, index) {
for(let i=0;i<urlData.length;i++) {
if(data[0].toLowerCase() === options[i].split("&")[0]) {
finalURLs.push(data[1]+"&"+options[i].substring(options[i].indexOf("&")+1));
}
};
});
return finalURLs;
}
requestPostData(endpoint, feedOptions, callback) {
var filters = Object.entries(this.filters);
var urls = Object.entries(endpoint.split("?filter=").pop().split(","));
var urls2 = urls.map(filter => endpoint.split("?")[0] + "?filter=" + filter[1]);
var sourceData = {};
console.log(urls);
console.log(urls2);
Promise.all(urls2.map(url =>
fetch(url).then(resp => resp.json())
)).then(values => {
console.log(values);
values.forEach(function(value, index) {
// console.log(value);
if(value.posts.items[0].source.source in sourceData) {
sourceData[value.posts.items[0].source.source] = sourceData[value.posts.items[0].source.source] + "," + urls2[index].split("?filter=")[1];
} else {
console.log("here");
sourceData[value.posts.items[0].source.source] = endpoint.split("?filter")[0] + "?filter=" + urls2[index].split("?filter=")[1];
}
});
var sources = Object.keys(sourceData).map((x) => x.toLowerCase());
var difference = Object.keys(this.filters).filter((x) => !sources.includes(x.toLowerCase()));
// console.log(sources);
// console.log(difference);
difference.forEach(function(value, index) {
sourceData[value] = endpoint.split("?filter")[0] + "?filter="+value;
});
var filterOptions = filters.map(filter => filter[0] + "&per=" + filter[1] + "&page=" + this.options.feed.page);
var finalData = this.combineURLData(sourceData, filterOptions);
console.log(finalData);
return finalData;
}).then(function(finalizedURLs) {
Promise.all(finalizedURLs.map(url =>
fetch(url).then(resp => resp.json())
)).then(callback);
});
}
First, requestPostData
is called, and what this.filters
basically is, is this part
filters: {
"instagram": 1,
"twitter": 2,
"facebook": 1
},
This is saying I need 1 instagram, 2 twitter, 1 facebook.
Going down to variables urls/urls2, urls
gets me each individual number that’s separated by a comma, in the initial URL
urls2
makes the new URLs that I need to parse over
https://www.asdf.io/api/feeds/320738?filter=644679
https://www.asdf.io/api/feeds/320738?filter=616690
https://www.asdf.io/api/feeds/320738?filter=123456,234567
https://www.asdf.io/api/feeds/320738?filter=234567
https://www.asdf.io/api/feeds/320738?filter=345678
Now, I then do a Promise on EACH of these URLs, and have a sourceData
array. I have an if/else. If (e.g.) sourceData[facebook] doesn’t exist, I create the initial URL, and if it’s found, then I just append to the URL by separating it by a comma. So e.g. this might be my final product
1. Facebook: "https://www.asdf.io/api/feeds/320738?filter=616690,123456"
2. Instagram: "https://www.asdf.io/api/feeds/320738?filter=644680,234567"
3. Twitter: "https://www.asdf.io/api/feeds/320738?filter=605975"
Just an example. In this scenario, only one of the URLs were identified as twitter.
Then, I have variables source/difference, which this thread goes over. I basically see which filters are defined, and compare them to my sourceData. If the filters call for 2 instagram, 2 facebook, and 1 twitter, but sourceData[twitter] doesn’t exist (for example), because no URL was found to have twitter, then I need sourceData[twitter] created and have
https://www.asdf.io/api/feeds/320738?filter=twitter
Put in its’ place.
Then I modify the URLs once last time to add in more filter options (see variable filterOptions
). So e.g. if the intial function call has twitter: 2
, then I append &per=2
to the end of the twitter URL. That tells the URL to only load in 2 posts in the JSON.
I do a lot of URL work and it’s a bit convoluted right now.
Then the rest of the function is of no worry to you. It’s a sloppy bit of code so far, but those 2 functions are the only one you ened to really look at. I need to refactor it but I want the core functionality done first. I’m open to any suggestions or improvements .