#If it works is it wrong
1 messages ยท Page 1 of 1 (latest)
No it did not work. (I mean your code was great and clear. it worked) But for loop, it is outputting only once not whole loop. It only see first link image0.png...none after that. I am using this code in function node >https://pastebin.com/1yRaPcKBNo
I had moved " return msg;" inside the loop.
But it seems, even outside it only returns the last Image15.png
oh, okay...hang on
@tepid juniper Where is the list of files coming from?
or rather...are these being auto-generated?
Understanding what controls how many URLs are going to be created will be helpful.
Actually its a website ..it autouploads everyday (PDF than png).
Number of files is 1 to 16 1.pdf, 2.pdf, 16.pdf . Like that
I need to feed these to a telegram group, where they are fetched. That feeding part etc is all working in nodered
I am just crawling everyday
Okay, so the file count isn't necessarily consistent.
Can you show me the code that feeds the node we're working on?
Its consistent mostly. 1 to 12 pages. SOmetimes 16 on sundays etc (its a newpaper actually)
Hmm, this could be useful for me. I live on an island so I'm beholden to a ferry provider and been thinking about how to scrape their schedules since they don't provide API access.
Is that relevant? Lets take is i need just need 16 links . Counter from 1 to 16. Simple.
Issue is for loop is only sending out 1st (when return is inside loop) or last link (when return is outside loop). Thats we need to fix. Isnt it?
Yeah, and I know why it's only sending one link, probably the last link actually.
Because you need to build an object, right now we're just building msg.link and then overwriting it until the counter limit is reached
and then when that limit is reached, the loop exits and msg.link has the last definition stored in it
make sense?
Actually i am scrapping here, i am just pushing the links to Telegram. Telegram has a preview feature for PDF and Technically i am just using a group to read a newspaper pdf pages everyday
Only code before it, is cron node, which will inject it every day at 6am in morning (to push links to telegram). Thats all
Okay, so the cron node scrapes the page and harvests the links, and that's the inbound msg.payload we're working with?
Yes.
1st link (when return is inside loop) or last link (when return is outside loop)
yeah, return "returns" out of a function, so wherever you put it, that's the "get out of here" function.
yes makes sense... thats what i am wondering...how will the node inject all for loop iterations to next node?
We need to build an object
So I'm thinking we need to know the number of files we're going to produce ahead of time, then we can build an array with the links in it and that will be an object inside the msg.payload we return.
Ok. I told you. Lets take the number of files as 1 to 16. (fixed)
yeah, let me think on it...It's 9:30am where I live, so I am now juggling work with recreation. ๐
super nerdy recreation, but recreation nonetheless.
Ok no problem. take your time. Sorry for bothering. meanwhile i will search more
Pedro helped here to push as array, which i can use in future nodes
#general-archived message
Even though i still could not find how to push each from function node itself, but it might work
Here you go:
var BaseURL = "http://www.website.com/";
var today = new Date();
var year = today.getFullYear();
var yr = today.getFullYear().toString().slice(-2);
var months = { 1: "jan", 2: "feb", 3: "mar", 4: "apr", 5: "may", 6: "jun", 7: "jul", 8: "aug", 9: "sep", 10: "oct", 11: "nov", 12: "dec" };
var month = today.getMonth();
var monthName = months[month];
var date = today.getDate();
var file = "Image"
var ext = ".png"
var link = [""];
for (let i = 0; i < len; i++) {
if (i < 10) {
var x = "0" + i.toString()
} else {
var x = i.toString()
}
link[i] = BaseURL + year + "/" + date + monthName + yr + "/" + file + x + ext;
}
msg.payload = link;
return msg;```
If you want to see my test flow:
https://github.com/chrismaki/Node-RED-Flows/blob/main/Date Formatting and URL creator
Contribute to chrismaki/Node-RED-Flows development by creating an account on GitHub.
I added a tweak to pad the number in the file name with a 0 if it's less than 10, that way the files will sort properly.
Sorry i just saw your message but pedro solution (array) kind of worked and
Then i was also able to sort the loop issue (iterating output) using another node
See this solution
https://pastebin.com/e91ETv30
Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.
Thanks i will look at your flow too. to learn a thing more ๐
- I just noticed that the Month Variable is giving output of 1 month less. Current month Aug. It tells july Array count problem? array starts from 0 i think
FIXED :
var months = { 0: "jan", 1: "feb", 2: "mar", 3: "apr", 4: "may", 5: "jun", 6: "jul", 7: "aug", 8: "sep", 9: "oct", 10: "nov", 11: "dec" };
- Our output for date is http://website/2022/5aug22/1.png
But website seems to write date in 2 digits and puts 0 before single digit dates like http://website/2022/05aug22/1.png
Does it have an easy fix? Sorry if i am asking too much ๐
var BaseURL = "http://www.website.com/";
var today = new Date();
var year = today.getFullYear();
var yr = today.getFullYear().toString().slice(-2);
var months = { 0: "jan", 1: "feb", 2: "mar", 3: "apr", 4: "may", 5: "jun", 6: "jul", 7: "aug", 8: "sep", 9: "oct", 10: "nov", 11: "dec" };
var month = today.getMonth();
var monthName = months[month];
var date = today.getDate();
if (date < 10) {
var strDate = "0" + date.toString()
} else {
var strDate = date.toString()
}
var file = "Image"
var ext = ".png"
var link = [""];
for (let i = 0; i < len; i++) {
if (i < 10) {
var x = "0" + i.toString()
} else {
var x = i.toString()
}
link[i] = BaseURL + year + "/" + strDate + monthName + yr + "/" + file + x + ext;
}
msg.payload = link;
return msg;```
Thanks. works ๐ ๐๐ฝ
jumping in here for a moment. I'm noticing variables turning around and defining other variables. such as year and then yr. get rid of the yr variable, and then change the line towards the end to javascript link[i] = BaseURL + year + "/" + strDate + monthName + year.toString().slice(-2) + "/" + file + x + ext;
same with month and monthName. Also, file and ext. They're just extra lines or can be incorporated into the link string.
Perhaps something more like:
link[i] = BaseURL + today.getFullYear() + "/" + strDate + monthName + today.getFullYear().toString().slice(-2) + "/Image" + x + ".PNG";
the IF i < 10 sections, might be able to be simplied with a RegEx statement but I'd have to play around to try to figure that one out.
months, technically, could be simplified into an array can just reading the string based on the key. so something like var months = ["jan", "feb", "mar"...] or something like that. even better, could be letting the code derive the name without extra lines. Some examples can be found here https://stackoverflow.com/questions/1643320/get-month-name-from-date
consider using something like this for the 0 padding in the strDate: const time_now = time_now_hours + ":" + ('00' + time_now_minutes).slice(-2); (This is line from one of my functions and is used to ensure a 0 before a single digit minute such as 5.)
Thanks for the info Derek. ๐๐ฝ I kinda like the noob code earlier (the way i am). But i appreciate your input (slowly i will become a pro lol)
Yeah, thanks for the feedback @spring summit, I'm always looking to learn more.
Is defining variables from other variables considered bad form?
I wouldn't necessarily say that it is bad form. Generally, it's my understanding that a value should be defined as a variable if it is going to be referenced two or more times within a piece of code. So yr, for example, is only referenced once. Additionally, it is a modified version of the year variable. Defining variables also allocates memory in order to store that variable; not necessarily a big deal now-a-days but something to consider. I think, however, defining variables that can act as "settings" for your code should be defined, such as if the file and ext variables were used in such a way that the script could quickly be modified to serve a different function like Webpage and .html, respectively. To me, really, the less lines in a code makes it easier to ready and with my ideas (or v2.0 suggestions) the number of lines the above function could have would be cut in half.