Skip to content

Instantly share code, notes, and snippets.

@anisayari
Last active April 21, 2019 16:45
Show Gist options
  • Save anisayari/44b16fdd806ea600219527c8fc7ff3e1 to your computer and use it in GitHub Desktop.
Save anisayari/44b16fdd806ea600219527c8fc7ff3e1 to your computer and use it in GitHub Desktop.
get_video_id_from_youtube
import csv
import pandas as pd
import googleapiclient
from tqdm import tqdm
DEVELOPER_KEY_GCP = "key_here"
def get_video_id_from_youtube(df):
# Disable OAuthlib's HTTPS verification when running locally.
# *DO NOT* leave this option enabled in production.
os.environ["OAUTHLIB_INSECURE_TRANSPORT"] = "1"
api_service_name = "youtube"
api_version = "v3"
youtube = googleapiclient.discovery.build(
api_service_name, api_version, developerKey = DEVELOPER_KEY_GCP)
def get_info_from_youtube(row):
print(str(row['artist'] +' '+row['title_music']))
if row['videoID_youtube'] == "missing":
print('tic')
query = str(row['artist'] +' '+row['title_music'])
request = youtube.search().list(
part="snippet",
maxResults=1,
q= query
)
try:
response = request.execute()
time.sleep(1)
except :
return 'missing'
videoID = response['items'][0]['id']['videoId']
print(videoID)
row['videoID_youtube'] = videoID
with open('data/videoid.csv', 'a',newline='') as writeFile:
writer = csv.writer(writeFile)
writer.writerow([row['artist'], row['title_music'], videoID])
else:
print('tac')
return row
tqdm.pandas()
df = df.progress_apply(get_info_from_youtube,axis=1)
return df
output_file = 'data/trackes_list.csv'
df = pd.read_csv(output_file,sep=";", header=0)
df = get_video_id_from_youtube(df)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment