665bf4860afd3

665bf4860b9d1
2 Guests are here.
 

Topic: A script for checking youtube videos? Read 810 times  

665bf4860c1beunn_atropos

665bf4860c233
Once or twice a year I go through all of my let's play posts in the runtime section by clicking the option "show posts" in my profile.
That lists about 24 post on every page and then I spent hours (!) by going through 172 pages to check for deleted youtube links.

I'm wondering if there is an easier way. It would be nice to have a script that checks the links for me and reports if a video is gone.

As a first step I would create a list of the links I already have and put them in a file (mabye even excel if it can't be avoided).
Then I would need help of someone to write this script for me, because I have no knowledge of programming whatsoever.

For example:
Bob Witherspoon-Jerkward LP's     link  | ok
theIdiotwhowascreatedforthislist  link  | video not found
MrTestman                                         link  | ok
xx!sausage                                         link  | ok

There would be separate lists for Sys/Sys2 videos that are still going on, finished, speedruns and I would add new LPs whenever I find them.

I'm aware that few people are looking into the runtime section and almost noone is using the alphabetical lists, but over the years I've grown fond of the ritual of posting LPs, and sometimes I just wish that maintaining the lists could be a bit easier.

So, if you can help me that would be awesome  :)
665bf4860c3cf
I can do that. I think I can even go trough the posts and parse the links.

665bf4860c4a1unn_atropos

665bf4860c4f4
Wohoo! :awesome:
The A-Z list are html pages. Maybe that can be helpful?
665bf4860c5e8
Do you want to make lists or should I try to parse trough the forum?
HTML, textfile, google doc (spreadsheet) etc. is fine.

665bf4860c84eunn_atropos

665bf4860c899
Nice!

So that means If I want to do the check, I can use the A-Z list instead of maintaining an additional list?
Acknowledged by: Gawain
665bf4860cb72
Code: [Select]
    <script>
      const checkLinks = operation => {
        const youtubeLinks = Array.from(document.querySelectorAll('li > a[href*=youtu]'))
        for (const link of youtubeLinks) {
          const url = link.getAttribute('href')
          fetch(`https://www.youtube.com/oembed?format=json&url=${encodeURIComponent(url)}`)
            .then(async response => {
              const payload = await response.text()
              if (!response.ok) {
                if (response.status === 401) return // Video is available. Embedding not allowed.

                if (response.status === 404) {
                  if (operation === 'mark')
                    link.parentElement.style.backgroundColor = '#8B0000'
                  else if (operation === 'remove')
                    link.parentElement.remove()
                } else if (response.status === 403) {
                  if (operation === 'mark')
                    link.parentElement.style.backgroundColor = '#CC5500'
                  else if (operation === 'remove')
                    link.parentElement.remove()
                } else if (response.status === 400) {
                  console.info(`${response.status}:${response.statusText} ${url}`)
                  link.parentElement.style.backgroundColor = '#D4AF37'
                } else {
                  console.warn(`${response.status}:${response.statusText} ${url}`)
                }
              }
            })
        }
      }

      const createButtons = () => {
        const buttonContainer = document.createElement('div')
        document.body.prepend(buttonContainer)

        const markButton = document.createElement('button')
        markButton.textContent = 'Mark'
        markButton.addEventListener('click', () => checkLinks('mark'))
        const removeButton = document.createElement('button')
        removeButton.textContent = 'Remove'
        removeButton.addEventListener('click', () => checkLinks('remove'))

        buttonContainer.append(markButton, removeButton)
      }

      if (document.readyState === 'loading')
        document.addEventListener('DOMContentLoaded', createButtons)
      else
        createButtons()
    </script>

Throw that into header or body.
It will create two buttons on top of the page.
"Mark" will mark missing, private or somehow broken/nonworking links (will also print the nonworking urls into browser console).
"Remove" will remove missing and private links.

You can for example press remove and then copy the page source and replace the .html file with that (just remember to remove the buttons on top of the body that was added by the script).

If you need more features or find bugs let me know.
665bf4860cc69
I tested against Sys1_finished.html and Sys2_sgo.html
665bf4860cdce
Haven't tried it, but the code looks fine.
I guess I would prepend the buttonContainer after appending the buttons to it.

665bf4860cea0unn_atropos

665bf4860ceeb
 :stroke: Woah! Thanks! It's working!

What's does the gold/brown/#D4AF37 response.status === 400) stand for?

For example Sys 2:
DragonAgeArchmage & Predation: fine
Fairycage Reviews: ah, links to another entry
UltraNova5000: same
gwm9797, blind: fine
665bf4860cfef
It means there is something strange with the youtube url. They need to be manually checked for now...
665bf4860d0ef
About DragonAgeArchmage; it seems youtube automatically fixes the url by removing last 8 from the video id...
665bf4860d1f3
I got some improvement ideas for next version:
* Remove buttons when "remove" is pressed.
* const payload = await response.text() is not needed
* Show progress (x/total) and message when all is done.
2 Guests are here.
Galette des Rois: „Le roi boit!“
Contact SMF 2.0.19 | SMF © 2016, Simple Machines | Terms and Policies
FEEP
665bf48610536