LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-24-2024, 09:57 AM   #1
gilesaj001
Member
 
Registered: Apr 2017
Location: Australia
Distribution: Ubuntu
Posts: 79

Rep: Reputation: 0
YouTube script stopped working.


I am not sure if anyone here knows about Youtube scripting but the part of this script that might not be working is the this part
Code:
sed "s/.*\?v=\([^\"]*\)\".*/\1/g")
I have been using a script to find the last video uploaded to my account for a few years. For about the last week I have had to manually add the video id to am embed code because the code did not find the video ID.

Has there been a change that I missed. The code I use is below.

Code:
#!/bin/bash
#
# Script to find last video id from my channel and create an embed code to put in a file.
# Need to replace the Working directory and Your channel id for this to work
#

date

# work from
cd working directory
# Define RSS feed for your channel
URL="https://www.youtube.com/feeds/videos.xml?channel_id="Your Channel ID"
videoid=$(curl -s $URL | grep "/watch?v=" | head -1 | sed "s/.*\?v=\([^\"]*\)\".*/\1/g")
echo $videoid
echo Last Video ID = $videoid
s="<center><iframe width=\"640\" height=\"480\""
s="$s src=\"https://www.youtube.com/embed/${videoid}?feature=player_embedded\""
s="$s frameborder=\"0\" allowfullscreen></iframe></center>"

echo "$s" > youtube-iframe.php

echo Copy complete and Youtube-iframe file copied over to html
date
cp youtube-iframe.php "web site folder"
 
Old 03-24-2024, 10:29 AM   #2
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 3,604

Rep: Reputation: 2547Reputation: 2547Reputation: 2547Reputation: 2547Reputation: 2547Reputation: 2547Reputation: 2547Reputation: 2547Reputation: 2547Reputation: 2547Reputation: 2547
Quote:
Originally Posted by gilesaj001 View Post
Has there been a change that I missed.
No idea. Why don't you look at the source code for the feed you are downloading and find out?


Alternatively, throw that brittle buggy code away and use a tool which is continually maintained, like yt-dlp:
Code:
yt-dlp --skip-download --get-id --playlist-end=1 "$CHANNEL_URL"

Last edited by boughtonp; 03-24-2024 at 10:30 AM.
 
Old 03-24-2024, 07:19 PM   #3
gilesaj001
Member
 
Registered: Apr 2017
Location: Australia
Distribution: Ubuntu
Posts: 79

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by boughtonp View Post
No idea. Why don't you look at the source code for the feed you are downloading and find out?


Alternatively, throw that brittle buggy code away and use a tool which is continually maintained, like yt-dlp:
Code:
yt-dlp --skip-download --get-id --playlist-end=1 "$CHANNEL_URL"
I am not a programmer so forgive me if I got this wrong.

I tried to get this command to run but could not. This is what I did from command line.

Code:
URL="https://www.youtube.com/feeds/videos.xml?channel_id=My Channel ID
sudo yt-dlp --skip-download --get-id --playlist-end=1 "$URL"
This is the result.
Code:
Traceback (most recent call last):
  File "/usr/bin/yt-dlp", line 6, in <module>
    from pkg_resources import load_entry_point
  File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 3254, in <module>
    def _initialize_master_working_set():
  File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 3237, in _call_aside
    f(*args, **kwargs)
  File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 3266, in _initialize_master_working_set
    working_set = WorkingSet._build_master()
  File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 584, in _build_master
    ws.require(__requires__)
  File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 901, in require
    needed = self.resolve(parse_requirements(requirements))
  File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 787, in resolve
    raise DistributionNotFound(req, requirers)
pkg_resources.DistributionNotFound: The 'brotli' distribution was not found and is required by yt-dlp
I have brotli installed

Code:
sudo apt install brotli Reading package lists... Done
Building dependency tree
Reading state information... Done
brotli is already the newest version (1.0.7-6ubuntu0.1).
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
 
Old 03-24-2024, 07:31 PM   #4
gilesaj001
Member
 
Registered: Apr 2017
Location: Australia
Distribution: Ubuntu
Posts: 79

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by boughtonp View Post
No idea. Why don't you look at the source code for the feed you are downloading and find out?


Alternatively, throw that brittle buggy code away and use a tool which is continually maintained, like yt-dlp:
Code:
yt-dlp --skip-download --get-id --playlist-end=1 "$CHANNEL_URL"
OK I looked at the output xml page in my browser. In the page that comes up if I search for "/watch?v=" then this line is returned.
Code:
<link rel="alternate" href="https://www.youtube.com/watch?v=77huZZ415vg"/>
The "77huZZ415vg" is what I am after as the $videoid. I do not understand what
Code:
sed "s/.*\?v=\([^"]*\)".*/\1/g
does.

I am a 74 year old fart and the brain cells aren't what they used to be.

Last edited by gilesaj001; 03-24-2024 at 07:41 PM.
 
Old 03-24-2024, 07:47 PM   #5
gilesaj001
Member
 
Registered: Apr 2017
Location: Australia
Distribution: Ubuntu
Posts: 79

Original Poster
Rep: Reputation: 0
This is a YouTube Problem

I ran the script today and it works again. If there is a better way to get the videoid than the "sed" command I would like to try it.
 
Old 03-24-2024, 09:17 PM   #6
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,782

Rep: Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082
Quote:
Originally Posted by gilesaj001 View Post
The "77huZZ415vg" is what I am after as the $videoid. I do not understand what
Code:
sed "s/.*\?v=\([^"]*\)".*/\1/g
does.

I am a 74 year old fart and the brain cells aren't what they used to be.

Turns out LLM's can do this now:

Quote:
Don't worry, I'll break down the command for you:

Code:
sed "s/.*\?v=\([^"]*\)".*/\1/g
This is a command using `sed`, which stands for stream editor. It's commonly used to perform text transformations on an input stream (a file or data passed through a pipeline). Let's break down the command step by step:

1. `s/`: This indicates that we are performing a substitution operation.
2. `.*\?v=`: This part matches any characters (`.*`) followed by `?v=`. The `.*` matches any character (except for a newline) zero or more times. `\?` matches the literal question mark character. So, `.*\?v=` together matches any characters followed by `?v=`.
3. `\([^"]*\)`: This part is enclosed in `\(` and `\)` which means it's a capture group. It captures the string between `=` and `"`. `[^"]*` matches any character except for `"`, zero or more times.
4. `.*`: This matches any remaining characters after the part we're interested in.
5. `\1`: This refers to the first capture group, which is the string between `=` and `"`.
6. `g`: This flag stands for "global" and ensures that all occurrences in each line are substituted, not just the first one.

So, in essence, the command is searching for a pattern that looks like `?v=...` in the input text and extracts the characters following `?v=` up to the next `"`. Then it replaces the whole line with just the extracted characters.
(it seems to have forgot the closing " after the capture group in the step by step part, but is otherwise correct.)
 
  


Reply

Tags
sed bash, youtube



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: How To Cast YouTube Videos From Your Phone To Raspberry Pi Using YouTube On TV (youtube.com/tv) LXer Syndicated Linux News 0 04-16-2021 01:30 AM
LXer: Youtube Downloader (youtube-dl) Helper Script LXer Syndicated Linux News 0 09-28-2013 01:50 PM
[SOLVED] clive and youtube-dl have stopped working Nutria Linux - General 6 11-25-2012 05:12 PM
LXer: Search YouTube videos on Ubuntu with the YouTube Lens LXer Syndicated Linux News 0 12-30-2011 08:40 PM
LXer: Youtube-dl - Download videos from Youtube in openSUSE LXer Syndicated Linux News 1 08-14-2008 08:10 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:39 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration