#๐ SyntaxError: invalid syntax (Google API)
330 messages ยท Page 1 of 1 (latest)
@wheat wharf
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
what have you tried?
what code do you have so far?
we can't help you if you don't respond
@wheat wharf this is the thread you opened.
Still need help?
Yes
@rose marten @sly osprey
#python-discussion message
I used VSC's terminal to get the API hooked up
Can you share the code? Without exposing the API keys of course
Basically this is the GitHub article:
https://github.com/luismartinezs/download-yt-transcripts
Then:
I already got my API key thing from Google, Brave AI told me this:
Step-by-Step Implementation:
Get the Key: Navigate to the Google Cloud Console, enable the YouTube Data API v3, and create an API key under the Credentials section.
Install Library: Install the necessary package via terminal using pip install --upgrade google-api-python-client.
Authenticate in Code: Initialize the YouTube service object by calling build('youtube', 'v3', developerKey='YOUR_API_KEY') from the googleapiclient.discovery module. ```
Following:
```from googleapiclient.discovery import build
# Replace 'YOUR_API_KEY' with the actual key obtained from Google Cloud Console
api_key = 'YOUR_API_KEY'
api_service_name = "youtube"
api_version = "v3"
# Build the service object
youtube = build(api_service_name, api_version, developerKey=api_key)
# Example: Retrieve channel statistics
request = youtube.channels().list(
part="snippet,contentDetails,statistics",
id="UC_x5XG1OV2P6uZZ5FSM9Ttw"
)
response = request.execute()
print(response)```
And then somehow links back to the link I sent above, I can pull channel IDs from YT.
ok...
ive just been reading this https://developers.google.com/youtube/v3/code_samples/code_snippets?apix=true
Fortune teller, what does it say?
nah, im just referencing where im getting info from
ok so whats the problem?
I wanna do this:
https://github.com/luismartinezs/download-yt-transcripts
The problem is that I don't know how
So far, I got the API run in the VSC terminal
I got the API key
I got the channel ID
Now I don't know how to run the script in the GitHub thing
you dont run scripts in github
you have to download the script to your computer to run it
https://github.com/luismartinezs/download-yt-transcripts/blob/main/download-transcripts.py#L29-L30 this tells you where to put the channel ID
download-transcripts.py lines 29 to 30
# Replace with the channel ID you're interested in
channel_id = 'UCRVjl9pOdgYAF3HBFyj36mg'```
https://github.com/luismartinezs/download-yt-transcripts/blob/main/download-transcripts.py#L9-L11 you will need to run the script with an environment variable
download-transcripts.py lines 9 to 11
api_key = os.getenv('YOUTUBE_API_KEY')
if not api_key:
raise ValueError("Please set the YOUTUBE_API_KEY environment variable.")```
Next steps are:
Paste channel id into file download-transcripts.py
Change the language tag in languages=['en'] if you need to
Run script py download-transcripts.py, all transcripts will be appended to a new file
?
the instructions doesnt say it, but you have to set your API key in your environment variable too
What's an environment variable? :l
Do these say it? @stiff ivy
essentially it's a list of keys and value, which your code can use to get some info
usually it's used for secret codes/API keys that you should never give to anyone else
no, it doesnt say it
Okay so what now?
do you know what is cmd or Powershell?
Yes
which one are you using?
CMD
i believe the command is
set YOUTUBE_API_KEY="replace_this_with_your_youtube_api_key"
im not on windows so i cant test it
On CMD?
It's partial
even if it's partial, it reduces the entropy needed to brute force, significantly
you really should create a new one
We'll pretend it didn't happen
it's like telling people the first 10 characters of your password, it's going to make it much easier to guess
at least we know the command works
It doesn't give any feedback whatsoever
try following the instructions for the script now
On CMD?
yea
Where to start from?
wdym?
download-transcripts.py lines 9 to 11
api_key = os.getenv('YOUTUBE_API_KEY')
if not api_key:
raise ValueError("Please set the YOUTUBE_API_KEY environment variable.")```
This?
no
we already set that
you need to replace the channel id in the script
with the channel you are trying to get transcripts from
download-transcripts.py line 16
def get_video_ids(channel_id):```
This?
download-transcripts.py lines 29 to 30
# Replace with the channel ID you're interested in
channel_id = 'UCRVjl9pOdgYAF3HBFyj36mg'```
Sorry I don't speak code
yea we usually help people who are trying to learn code, not just run them
have you replaced the channel_id here?
Yessir
never too late to start learning ๐
where did you download the script to, in your computer?
I didn't download anything
again, you need to download the script to your computer to run it
Like put it into VSC?
Terminal?
do you know what is downloading a file?
Yes, where to download it from? GitHub?
yea
in your cmd window, you want to navigate to this directory
D:\download-yt-transcripts-main\download-yt-transcripts-main
cd D:\download-yt-transcripts-main\download-yt-transcripts-main
before you run this
what command did you run?
dont show the API key
just show the command without the key
pc>set YOUTUBE_API_KEY=
what is pc>?
if you right click download-transcripts.py does it show Open with... >
can you open with vscode?
Terminal?
right click in your folder in file explorer
Same directory?
it's here right?
you want to open download-transcripts.py and replace the channel ID with the ID of the channel you want
yes replace the ID on line 30
Done
sorry i just realized i missed something
have you installed dotenv?
i didnt see it earlier, but this script actually uses dotenv files to store the environment variables
you will want to create a new file in vsc, write YOUTUBE_API_KEY="replace_this_with_your_youtube_api_key" inside, and then save it as .env in the same folder as the script
Uhh I don't know what that is
pip install python-dotenv
pip : The term 'pip' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling
of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ pip install python-dotenv
+ ~~~
+ CategoryInfo : ObjectNotFound: (pip:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException```
Terminal VSC
Done
^
either one is fine
you run python via a terminal (CMD)
your VSC has a built-in terminal
Let's use VSC
sure
That's some great practice for you
I'm sure it'll help out probably sometime in the future
anyway
I don't think I'd retain a lot of knowledge from it but having that API running would be awesome
is the script working?
How do I tell? I don't even remember if I included the API key in the script
you are supposed to put this in your .env file
are there any error messages in vsc?
i dont get it
You're mentioning if it works, how do I tell? I have to run it, right?
have you not run the command py download-transcripts.py?
Not yet
you will need to navigate to the folder where you unzipped it
i think the command is also cd
cd D:\download-yt-transcripts-main\download-yt-transcripts-main
yea
Traceback (most recent call last):
File "D:\download-yt-transcripts-main\download-yt-transcripts-main\download-transcripts.py", line 4, in <module>
from youtube_transcript_api import YouTubeTranscriptApi
ModuleNotFoundError: No module named 'youtube_transcript_api'
PS D:\download-yt-transcripts-main\download-yt-transcripts-main>```
Done
Now re-running
Have another.
download-transcripts.py : The term 'download-transcripts.py' is not recognized as the name of a cmdlet, function, script file,
or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:2
+ download-transcripts.py
+ ~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (download-transcripts.py:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Suggestion [3,General]: The command download-transcripts.py was not found, but does exist in the current location. Windows PowerShell does not load commands from the current location by default. If you trust this command, instead type: ".\download-transcripts.py". See "get-help about_Command_Precedence" for more details.
PS D:\download-yt-transcripts-main\download-yt-transcripts-main>```
are there any new files in your folder?
i just realized something
As a .txt file
download-transcripts.py line 37
transcript = YouTubeTranscriptApi.get_transcript(video_id, languages=['es'])```
you have to change es to en if you want english
re-run the script
you may want to delete the txt files that it saved just now
but it's fine
py download-transcripts.py?
delete the files it saved just now if you want to avoid confusion
Done
Confirm
yea
what is the content of the file
These things are wild, I'd never be like zehata
did it work?
I assume I need to run it in terminal VSC? Right?
yea, what is inside the file? is it what you wanted?
run what?
0kb
hmm
Oh so that script downloads the video subtitles?
Yeah no it didn't work
No
you don't need to be, and there is no use to comparing yourself to others
UC0fDV9ojOuKTrntPRlgKkcQ
hmm does youtube not provide the auto generated transcripts...
So the script is faulty?
idk i will have to check
yes, the script is faulty
on line 39, you want to replace it with
transcript = YouTubeTranscriptApi().fetch(video_id, languages=["en"])
Old:
file.write(f"Transcript for Video ID: {video_id}\n")
New:
transcript = YouTubeTranscriptApi().fetch(video_id, languages=["en"])
?
You are forgiven
try running the script now
what's the error
no
im not asking for the content of the file
i am asking for the error in your terminal when you run it
hmm
in any case, this script is not accounting for youtube rate limiting
so it wouldnt work for this many videos
even if you could get it running
How much is it rate limited to?
idk
if youtube told people, people would abuse it
and of course, i cannot help you with circumventing youtube's rate limit
because that would be against server rules
sorry about that
I set it to 1 in line 18
if you want to try 1 video, then change line 42 to
file.write(f"{line.text}\n")
```as well
the script is outdated, so it's still using old functions and type definition
So what'd be the new line 42?
file.write(f"{line.text}\n")
you change it to 1 in line 18, not on line 42
what is the error
Blank
can you try running it in cmd instead of vsc?
Same directory?
yes
Traceback (most recent call last):
File "D:\download-yt-transcripts-main\download-yt-transcripts-main\download-transcripts.py", line 31, in <module>
video_ids = get_video_ids(channel_id)
File "D:\download-yt-transcripts-main\download-yt-transcripts-main\download-transcripts.py", line 19, in get_video_ids
response = request.execute()
File "C:\Users\pc\AppData\Local\Programs\Python\Python313\Lib\site-packages\googleapiclient\_helpers.py", line 130, in positional_wrapper
return wrapped(*args, **kwargs)
File "C:\Users\pc\AppData\Local\Programs\Python\Python313\Lib\site-packages\googleapiclient\http.py", line 938, in execute
raise HttpError(resp, content, uri=self.uri)
googleapiclient.errors.HttpError: <HttpError 400 when requesting https://youtube.googleapis.com/youtube/v3/search?part=id&channelId=UCRVjl9pOdgYAF3HBFyj36mg&maxResults=1&type=video&key=%22AIzaSyBv_PoZtbqgDgYfQ-KRHIqLC2Xd7fR_Lqo%22&alt=json returned "API key not valid. Please pass a valid API key.". Details: "[{'message': 'API key not valid. Please pass a valid API key.', 'domain': 'global', 'reason': 'badRequest'}]">
D:\download-yt-transcripts-main\download-yt-transcripts-main>```
Details: "[{'message': 'API key not valid. Please pass a valid API key.', 'domain': 'global', 'reason': 'badRequest'}]">
how did you get your API key?
Google Cloud Platform lets you build, deploy, and scale applications, websites, and services on the same infrastructure as Google.
that one i cant help you with
if you previous API key doesnt work, why would creating a new one fix it?
and uh
mine does work
It was pinged?
so you will need someone else to help you for this
well, that's just what the error message says
Didn't have this enabled
So the script worked on your end?
but i got rate limited
At how many requests?
2
even if it works
you are not going to get very far
because the script doesnt account for rate limiting
typically you would want to retry only after some time
There's an interval between each rate limit?
usually it's time-based, yes
https://github.com/roundyyy/yt-bulk-subtitles-downloader
https://gist.github.com/udaylunawat/6c3cf79abbcffd0029905cc253c28869
Any of these could work?
A high-performance CLI tool for mass downloading subtitles (captions) from YouTube channels, playlists, or individual videos. - roundyyy/yt-bulk-subtitles-downloader
i have no idea
Could it be automated? And even if, how would it take other videos?
This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.