-
Notifications
You must be signed in to change notification settings - Fork 0
/
liked_songs.py
185 lines (166 loc) · 6.47 KB
/
liked_songs.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import json
import logging
import os
import sys
import requests
from ytmusicapi import YTMusic
NOTION_API_KEY = os.environ["NOTION_API_KEY"]
NOTION_DATABASE_ID = os.environ["NOTION_DATABASE_ID"]
NOTION_VERSION = "2022-06-28"
YOUTUBE_OAUTH_JSON = os.environ["YOUTUBE_OAUTH_JSON"]
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger("")
def retrieve_liked_songs_list(oauth_json):
ytmusic = YTMusic(oauth_json)
return ytmusic.get_liked_songs(limit=None)
def retrieve_notion_songs(database_id, notion_version, notion_api_key):
page_data = []
start_cursor = None
while True:
data = {"page_size": 100}
if start_cursor:
data["start_cursor"] = start_cursor
response = requests.post(
f"https://api.notion.com/v1/databases/{database_id}/query",
json=data,
headers={
"Accept": "application/json",
"Content-Type": "application/json",
"Notion-Version": notion_version,
"Authorization": f"Bearer {notion_api_key}",
},
)
response_data = response.json()
page_data.extend(response_data["results"])
if response_data["next_cursor"]:
start_cursor = response_data["next_cursor"]
else:
break
return page_data
def determine_new_songs(liked_songs, notion_pages):
new_songs = []
existing_songs = {
page["properties"]["video_id"]["rich_text"][0]["text"]["content"]
for page in notion_pages
}
for song in liked_songs:
if song["videoId"] not in existing_songs:
new_songs.append(song)
return new_songs
def add_new_songs(new_songs, database_id, notion_version, notion_api_key):
for song in new_songs:
response = requests.post(
"https://api.notion.com/v1/pages",
headers={
"Accept": "application/json",
"Notion-Version": notion_version,
"Content-Type": "application/json",
"Authorization": f"Bearer {notion_api_key}",
},
json={
"parent": {"database_id": database_id},
"properties": {
"Title": {
"title": [
{
"type": "text",
"text": {"content": song["title"], "link": None},
}
]
},
"Artist": {
"rich_text": [
{
"type": "text",
"text": {
"content": ",".join(
artist["name"] for artist in song["artists"]
),
"link": None,
},
}
]
},
"Album": {
"rich_text": [
{
"type": "text",
"text": {
"content": (
song.get("album", {}).get("name", "")
if song["album"]
else ""
),
"link": None,
},
}
]
},
"Cover": {
"files": [
{
"name": "Cover Art",
"external": {
"url": song["thumbnails"][-1]["url"],
},
}
]
},
"Duration": {
"rich_text": [
{
"type": "text",
"text": {
"content": song.get("duration", ""),
"link": None,
},
}
]
},
"duration_seconds": {"number": song.get("duration_seconds", 0)},
"video_id": {
"rich_text": [
{
"type": "text",
"text": {
"content": song["videoId"],
"link": None,
},
}
]
},
"Video": {"url": f"https://youtube.com/watch?v={song['videoId']}"},
},
"icon": {
"type": "external",
"external": {"url": song["thumbnails"][0]["url"]},
},
"cover": {
"type": "external",
"external": {"url": song["thumbnails"][-1]["url"]},
},
},
)
response_data = response.json()
if response_data.get("object") == "error":
logger.error(f"Error creating page. Response: {response_data}")
def main(args):
logger.info("Retrieving liked songs playlist from YouTube...")
auth_json = json.loads(YOUTUBE_OAUTH_JSON)
liked_songs = retrieve_liked_songs_list(auth_json)
logger.debug(f"Payload: {liked_songs}")
logger.info(f"Success. Found {len(liked_songs['tracks'])} songs.")
logger.info("Retrieving Notion songs database...")
notion_pages = retrieve_notion_songs(
NOTION_DATABASE_ID, NOTION_VERSION, NOTION_API_KEY
)
logger.debug(f"Notion Pages: {notion_pages}")
logger.info(f"Success. Found {len(notion_pages)} notion pages.")
logger.info("Determining new songs to add...")
songs_to_add = determine_new_songs(liked_songs["tracks"], notion_pages)
logger.info(f"{len(songs_to_add)} new songs to add.")
logger.info("Adding new songs...")
add_new_songs(songs_to_add, NOTION_DATABASE_ID, NOTION_VERSION, NOTION_API_KEY)
logger.info("Success adding new songs.")
if __name__ == "__main__":
sys.exit(main(sys.argv))