-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathyoutube.py
75 lines (62 loc) · 2.46 KB
/
youtube.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
from youtube_title_parse import get_artist_title
from googleapiclient.discovery import build
from dataclasses import dataclass
from dotenv import load_dotenv
import os
import re
load_dotenv()
@dataclass
class Song:
artist: str
title: str
def clean_song_info(song: Song) -> Song:
artist, title = song.artist, song.title
# Remove everything after '(' including '('
title = re.sub('\(.*', '', title)
# Remove everything after 'ft' including 'ft'
title = re.sub('ft.*', '', title)
# Remove everything after ',' including ','
title = re.sub(',.*', '', title)
# Remove everything after ' x ' including ' x '
artist = re.sub('\sx\s.*', '', artist)
# Remove everything after '(' including '('
artist = re.sub('\(.*', '', artist)
# Remove everything after 'ft' including 'ft'
artist = re.sub('ft.*', '', artist)
# Remove everything after ',' including ','
artist = re.sub(',.*', '', artist)
# Remove whitespaces from start and end
return Song(artist.strip(), title.strip())
class Youtube:
API_KEY = str(os.getenv('API_KEY'))
API_SERVICE_NAME = "youtube"
API_VERSION = "v3"
def __init__(self) -> None:
self.songs = []
self.api = build(Youtube.API_SERVICE_NAME,
Youtube.API_VERSION, developerKey=Youtube.API_KEY)
# changed name due to spotify name collison
def __fetch_songs(self, playList_id, page_token=None):
result = self.api.playlistItems().list(part="snippet",
playlistId=playList_id,
maxResults="300",
pageToken=page_token
).execute()
for item in result['items']:
songInfo = item['snippet']['title']
try:
artist, title = get_artist_title(songInfo)
self.songs.append(clean_song_info(Song(artist, title)))
except:
print(f'Error parsing {songInfo}')
return result
def get_songs_from_playlist(self, playList_id):
result = self.__fetch_songs(playList_id)
while 'nextPageToken' in result:
page_token = result['nextPageToken']
result = self.__fetch_songs(playList_id, page_token)
return self.songs
if __name__ == "__main__":
yt = Youtube()
print(yt.get_songs_from_playlist(
'PLc2sKlfFc01pesmB9YIp_THyWcq2rZxFc'))