Urlfetch website with no API end point

I am currently trying to create a custom command called !tides. What I would like this command to do is to post the current ocean tide in our location (like the weather command, but for tide conditions). I found the urlfetch command and put the website in there, but it came back with the 400 count error. Then I found on google’s snippet - EXACTLY what I want the command to post and it’s under 400 characters. Since I am not a coder, I have had no luck getting this to work. I may be over my head, but I thought I would reach out to see if anyone would be able to offer a suggestion. Thanks.

Hey @Deerfield_Beach!

It would be nice of you to post the API URL you’re trying to access with $(urlfetch) so we can help, we can’t guess what you’re talking about.

Thanks for replying. Here is what I currently have:

$(urlfetch http://dfb.city/286/Beach-Conditions)

Like I said, I am not a coder and I clearly do not know what I am doing.

Right, so you’re trying to pull an entire web page, it’s normal to get this error.
Here’s a fraction of what you’re pulling:

The website doesn’t seem to have an API so exploiting the data will be tricky, usually we simply don’t, since the web page shape can change a lot, but I can give it a try if you tell me precisely which data you’re trying to pull. Best thing would be to take a screenshot and showcase exactly what you want with arrows. Keep in mind that whatever success I might get in creating the command, it could break at any time in the future.

I have 2 screen shots, one with a google snippet which is exactly what I want it to say. But I am not sure if you can get a snippet to work.

The other is a screenshot from our website, see the highlighted area. This website I can control, so if there are any changes is based on what I change.

Either one will work. Thank you for your help!

EDIT: as of 2020/07/25 this isn’t working anymore after an update on how $(urlfetch) behaves.

Alright, so here’s the command:

!addcom !conditions $(eval w=`$(urlfetch json http://dfb.city/286/Beach-Conditions)`;r=[];s=[`High Tide`,`Low Tide`,`Water Temperature`,`Water Conditions`,`Air Temperature`,`Wind Direction`,`Current Flags`,`Sea Pests`];s.forEach(e=>r.push(w.slice(w.search(new RegExp(e,`i`)),w.indexOf(`</tr>`,w.search(new RegExp(e,`i`)))).replace(/&nbsp;/g,` `).replace(/<\/td>/g,``).replace(/<td>/g,``).replace(/<td.+>/g,``).replace(/<\/span>/g,``).replace(/<span.+>/g,``)));`Conditions: ${r.join(` | `)}`;)

Just copy it in your chat, it’ll create a !conditions command.

If there are informations you don’t want to be displayed, remove them in s:
Here’s what it looks like by default:

s=[`High Tide`,`Low Tide`,`Water Temperature`,`Water Conditions`,`Air Temperature`,`Wind Direction`,`Current Flags`,`Sea Pests`]

Let’s say you don’t want to display the flags, remove Current Flags, like so:

s=[`High Tide`,`Low Tide`,`Water Temperature`,`Water Conditions`,`Air Temperature`,`Wind Direction`,`Sea Pests`]

If you want to change the order of the information, just switch s around:
Example, I switched Wind Direction and Air Temperature:

s=[`High Tide`,`Low Tide`,`Water Temperature`,`Water Conditions`,`Wind Direction`,`Air Temperature`,`Current Flags`,`Sea Pests`]

Or, if you want to get the data from the other website, the Google-snippet-like, here’s the command:

!addcom !tides $(eval w=`$(urlfetch json https://www.usharbors.com/harbor/florida/deerfield-beach-hillsboro-river-fl/tides/#harbor-nav)`;r=w.slice(w.search(/next-tide/gi),w.indexOf(`<table`,w.search(/next-tilde/gi))).slice(11).replace(/\//g,``).replace(/<b>/g,``);)

Just copy it in your chat, it’ll create a !tides command.

And if you want to cut the message before Sunset today...:

!addcom !tides $(eval w=`$(urlfetch json https://www.usharbors.com/harbor/florida/deerfield-beach-hillsboro-river-fl/tides/#harbor-nav)`;r=w.slice(w.search(/next-tide/gi),w.indexOf(`Sunset`,w.search(/next-tilde/gi))).slice(11).replace(/\//g,``).replace(/<b>/g,``);)

This is fantastic. Appreciate this, however, I can not get it to work because YouTube only allows 200 characters in the chat. I have spent the last 2 hours trying to research and attempt it on my own but have had no luck. Am I doing something wrong? I am sorry to keep bugging you.

OMG, you’re on Youtube… When will they raise the character limit on that website?

Ok, then we’ll need to use Pastebin, make an account to come back later in case you need to edit the commands.

Whichever command you decide to go with, you’ll need to create a paste, set its expiration date to Never and then paste the code of the command in it, the part after !addcom !command_name starting with $(eval) to the end.

I also think you’ll have to go with shorter versions of the commands, so for !conditions remove at least one thing you don’t really care about, and for !tides you’ll have to go with the shorter version. Feel free to test if the longer versions work before shortening them if you want to.

Then the commands will look like this:

!addcom !command_name $(urlfetch json https://pastebin.com/raw/XXXXXXXX)

Replace XXXXXXXX by the paste ID.

That sounded easy, except this is what it came back as.
Screen Shot 2020-07-09 at 12.38.08 PM

Right, I’m stupid, it was a totally avoidable mistake, I’ve done that so many times before, yet I still get it wrong lmao… For some reason I thought I remembered it this way, sorry about that, YouTube isn’t my platform of choice for livestreaming so I tend to forget.


So, for the first command, !conditions, here’s how to do it:
Command:

!addcom !conditions $(eval w=`$(urlfetch json http://dfb.city/286/Beach-Conditions)`;$(urlfetch json https://pastebin.com/raw/XXXXXXXX))

Pastebin:

r=[];
s=[`High Tide`,`Low Tide`,`Water Temperature`,`Water Conditions`,`Air Temperature`,`Wind Direction`,`Current Flags`,`Sea Pests`];
s.forEach(e=>r.push(w.slice(w.search(new RegExp(e,`i`)),w.indexOf(`</tr>`,w.search(new RegExp(e,`i`)))).replace(/&nbsp;/g,` `).replace(/<\/td>/g,``).replace(/<td>/g,``).replace(/<td.+>/g,``).replace(/<\/span>/g,``).replace(/<span.+>/g,``)));
`Conditions: ${r.join(` | `)}`;

And for the second command, !tides, here’s how to do it:
Command:

!addcom !tides $(eval w=`$(urlfetch json https://www.usharbors.com/harbor/florida/deerfield-beach-hillsboro-river-fl/tides/#harbor-nav)`;$(urlfetch json https://pastebin.com/raw/XXXXXXXX))

Pastebin:

r=w.slice(w.search(/next-tide/gi),w.indexOf(`Sunset`,w.search(/next-tilde/gi))).slice(11).replace(/\//g,``).replace(/<b>/g,``);

As I said before, replace XXXXXXXX with the paste ID.

You are AMAZING! Thank you so much. It works perfectly.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.