#๐Ÿ”’ SyntaxError: invalid syntax (Google API)

330 messages ยท Page 1 of 1 (latest)

wheat wharf
#

I'm new to Python, I want to run Google's API but it doesn't work:

warm windBOT
#

@wheat wharf

Python help channel opened

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.

sly osprey
#

what have you tried?

#

what code do you have so far?

#

we can't help you if you don't respond

willow raven
#

@wheat wharf this is the thread you opened.

wheat wharf
#

Yes

#

@rose marten @sly osprey

#python-discussion message

#

I used VSC's terminal to get the API hooked up

rose marten
wheat wharf
#

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.
stiff ivy
#

ok...

wheat wharf
#

Fortune teller, what does it say?

stiff ivy
#

it's more for the other people in this thread

#

but please do take a look

wheat wharf
#

Oh so you're helping the helpers

#

That's on some different level

#

Sure will

stiff ivy
stiff ivy
wheat wharf
stiff ivy
#

we know

#

we are asking you: what is the problem you face

wheat wharf
#

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

stiff ivy
#

you dont run scripts in github

#

you have to download the script to your computer to run it

warm windBOT
#

download-transcripts.py lines 29 to 30

# Replace with the channel ID you're interested in
channel_id = 'UCRVjl9pOdgYAF3HBFyj36mg'```
stiff ivy
warm windBOT
#

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.")```
wheat wharf
#

?

stiff ivy
#

the instructions doesnt say it, but you have to set your API key in your environment variable too

wheat wharf
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

stiff ivy
wheat wharf
#

Okay so what now?

stiff ivy
#

hold on

#

are you using windows?

wheat wharf
#

Win10

#

Pro

stiff ivy
#

do you know what is cmd or Powershell?

wheat wharf
#

Yes

stiff ivy
#

which one are you using?

wheat wharf
#

CMD

stiff ivy
#

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

wheat wharf
#

On CMD?

stiff ivy
#

yea

#

you will need to do this before you run the script

#

bruh

#

delete this

wheat wharf
#

It's partial

stiff ivy
#

even if it's partial, it reduces the entropy needed to brute force, significantly

wheat wharf
#

Crazy world we live in

#

Ok

stiff ivy
#

you really should create a new one

wheat wharf
#

We'll pretend it didn't happen

stiff ivy
#

at least we know the command works

wheat wharf
#

It doesn't give any feedback whatsoever

stiff ivy
#

try following the instructions for the script now

wheat wharf
#

On CMD?

stiff ivy
#

yea

wheat wharf
#

Where to start from?

stiff ivy
#

wdym?

warm windBOT
#

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.")```
wheat wharf
#

This?

stiff ivy
#

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

warm windBOT
#

download-transcripts.py line 16

def get_video_ids(channel_id):```
wheat wharf
#

This?

warm windBOT
#

download-transcripts.py lines 29 to 30

# Replace with the channel ID you're interested in
channel_id = 'UCRVjl9pOdgYAF3HBFyj36mg'```
wheat wharf
#

Sorry I don't speak code

stiff ivy
#

yea we usually help people who are trying to learn code, not just run them

stiff ivy
wheat wharf
#

Yessir

prime tangle
stiff ivy
#

where did you download the script to, in your computer?

wheat wharf
stiff ivy
#

again, you need to download the script to your computer to run it

wheat wharf
#

Terminal?

stiff ivy
#

do you know what is downloading a file?

wheat wharf
#

Yes, where to download it from? GitHub?

stiff ivy
#

yea

wheat wharf
#

How do I do that?

stiff ivy
#

you will want to unzip it to some folder

wheat wharf
stiff ivy
#

in your cmd window, you want to navigate to this directory

wheat wharf
#

D:\download-yt-transcripts-main\download-yt-transcripts-main

stiff ivy
#

cd D:\download-yt-transcripts-main\download-yt-transcripts-main

wheat wharf
stiff ivy
#

hmm

#

hold on

#

you need to run D: first

stiff ivy
wheat wharf
#

Okay

stiff ivy
#

set your API key first

#

like what you need just now

wheat wharf
stiff ivy
#

dont show the API key

#

just show the command without the key

wheat wharf
#

pc>set YOUTUBE_API_KEY=

stiff ivy
#

what is pc>?

wheat wharf
#

Good question, just got rid of that

stiff ivy
#

if you right click download-transcripts.py does it show Open with... >

#

can you open with vscode?

wheat wharf
#

Terminal?

stiff ivy
#

right click in your folder in file explorer

wheat wharf
#

Same directory?

stiff ivy
#

you want to open download-transcripts.py and replace the channel ID with the ID of the channel you want

wheat wharf
stiff ivy
#

yes replace the ID on line 30

wheat wharf
#

Done

stiff ivy
#

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

wheat wharf
stiff ivy
wheat wharf
#
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

stiff ivy
#

hmm

#

py -m pip install python-dotenv

wheat wharf
#

Done

wheat wharf
#

Done

#

D:\download-yt-transcripts-main\download-yt-transcripts-main

stiff ivy
#

no, the file should only be .env

#

no name

wheat wharf
#

Ok

#

Run via CMD?

#

Or VSC?

#

Or Python??

stiff ivy
#

either one is fine

#

you run python via a terminal (CMD)

#

your VSC has a built-in terminal

wheat wharf
#

Let's use VSC

stiff ivy
#

sure

wheat wharf
#

That's some great practice for you

#

I'm sure it'll help out probably sometime in the future

stiff ivy
#

anyway

wheat wharf
#

I don't think I'd retain a lot of knowledge from it but having that API running would be awesome

stiff ivy
#

is the script working?

wheat wharf
#

How do I tell? I don't even remember if I included the API key in the script

stiff ivy
stiff ivy
stiff ivy
#

i dont get it

wheat wharf
#

You're mentioning if it works, how do I tell? I have to run it, right?

stiff ivy
#

have you not run the command py download-transcripts.py?

wheat wharf
#

Not yet

stiff ivy
#

you will need to navigate to the folder where you unzipped it

wheat wharf
#
PS C:\Users\pc>```
#

Yeah

stiff ivy
#

i think the command is also cd

#

cd D:\download-yt-transcripts-main\download-yt-transcripts-main

wheat wharf
#

It switched

#

Now run it?

stiff ivy
#

yea

wheat wharf
#
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>```
stiff ivy
#

hmm

#

you will need to install that too
py -m pip install youtube-transcript-api

wheat wharf
#

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>```
stiff ivy
#

you run it with py download-transcripts.py

#

not just download-transcripts.py

wheat wharf
#

Nothing happens?

stiff ivy
#

are there any new files in your folder?

wheat wharf
#

Oh yes

#

I see the channel's key

stiff ivy
#

i just realized something

wheat wharf
#

As a .txt file

stiff ivy
#

the author of the script speaks spanish

warm windBOT
#

download-transcripts.py line 37

transcript = YouTubeTranscriptApi.get_transcript(video_id, languages=['es'])```
wheat wharf
#

Oh ffs

#

Good spot

stiff ivy
#

you have to change es to en if you want english

wheat wharf
#

So where's that going back?

#

Changed

#

Okay next

stiff ivy
#

re-run the script

#

you may want to delete the txt files that it saved just now

#

but it's fine

wheat wharf
stiff ivy
#

delete the files it saved just now if you want to avoid confusion

wheat wharf
#

Done

wheat wharf
stiff ivy
#

yea

wheat wharf
#

New file

#

What now

stiff ivy
#

what is the content of the file

wheat wharf
stiff ivy
#

did it work?

wheat wharf
#

Channel ID is in there down

wheat wharf
stiff ivy
#

yea, what is inside the file? is it what you wanted?

stiff ivy
#

hmm

wheat wharf
#

Yeah no it didn't work

stiff ivy
#

was there no error messages?

#

what's the channel ID

wheat wharf
prime tangle
wheat wharf
stiff ivy
#

hmm does youtube not provide the auto generated transcripts...

wheat wharf
#

So the script is faulty?

stiff ivy
#

idk i will have to check

stiff ivy
#

on line 39, you want to replace it with
transcript = YouTubeTranscriptApi().fetch(video_id, languages=["en"])

wheat wharf
#

Old:

file.write(f"Transcript for Video ID: {video_id}\n")

New:

transcript = YouTubeTranscriptApi().fetch(video_id, languages=["en"])

?

stiff ivy
#

sorry

#

37

wheat wharf
#

You are forgiven

stiff ivy
#

try running the script now

wheat wharf
#

Same result

#

If you think this script can't be fixed, we can move onto another.

stiff ivy
#

what's the error

wheat wharf
#

If you think it can, we can give it a try.

#

No visible error. Just an empty .txt

stiff ivy
#

no

#

im not asking for the content of the file

#

i am asking for the error in your terminal when you run it

wheat wharf
#

Nothing happens

#

The only visible change is the .txt generation

#

Nothing else

stiff ivy
#

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

wheat wharf
#

How much is it rate limited to?

stiff ivy
#

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

wheat wharf
#

I set it to 1 in line 18

stiff ivy
#

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

wheat wharf
#

So what'd be the new line 42?

stiff ivy
#

file.write(f"{line.text}\n")

wheat wharf
#

Where do I put "1"?

#

Instead of n?

stiff ivy
#

sorry?

#

im not asking you to change n to 1

stiff ivy
wheat wharf
#

Ah

#

Script seems to be bad

stiff ivy
#

what is the error

wheat wharf
#

Blank

stiff ivy
#

can you try running it in cmd instead of vsc?

wheat wharf
#

Same directory?

stiff ivy
#

yes

wheat wharf
#
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>```
stiff ivy
#

Details: "[{'message': 'API key not valid. Please pass a valid API key.', 'domain': 'global', 'reason': 'badRequest'}]">

#

how did you get your API key?

wheat wharf
stiff ivy
#

ok...

#

and it doesnt work?

wheat wharf
#

Yeah

#

Should I make a new API key?

stiff ivy
#

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

wheat wharf
#

It was pinged?

stiff ivy
#

so you will need someone else to help you for this

stiff ivy
wheat wharf
#

Didn't have this enabled

wheat wharf
stiff ivy
wheat wharf
stiff ivy
#

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

wheat wharf
#

There's an interval between each rate limit?

stiff ivy
#

usually it's time-based, yes

wheat wharf
#

Or could it be pulled one after another?

#

Ah

stiff ivy
#

it might be like

#

idk

#

once a minute

wheat wharf
#
GitHub

A high-performance CLI tool for mass downloading subtitles (captions) from YouTube channels, playlists, or individual videos. - roundyyy/yt-bulk-subtitles-downloader

Gist

Download youtube transcripts in bulk for Google takeout youtube data (supports both csv and json) - yt_transcripts_bulk.py

stiff ivy
#

i have no idea

wheat wharf
stiff ivy
#

idk

#

it can be automated, but you need to figure out what is a safe limit

wheat wharf
#

Unrelated to integration, yeah, needs testing too

#

This is like hacking into NASA

stiff ivy
#

this is quite typical

#

for most applications

#

it's not just youtube

wheat wharf
#

But why didn't my API work?

#

I don't get that part..

stiff ivy
#

i have no idea

#

sorry

warm windBOT
#
Python help channel closed for inactivity

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.