-
Notifications
You must be signed in to change notification settings - Fork 0
/
ytmusicHousekeeping.py
273 lines (251 loc) · 8.66 KB
/
ytmusicHousekeeping.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
from bson.objectid import ObjectId
from datetime import datetime
from os import chdir
from pymongo.collection import Collection, ReturnDocument
from pymongo.database import Database
from pymongo import DESCENDING
from pymongo import MongoClient
import requests
import schedule
from secretsFile import mongoString, homeassistantToken, homeassistantUrl
from threading import Thread
import time
from types import FunctionType
from ytmusicapi import YTMusic
from ytmusicFunctions import GetSongId
from ytmusicPlaylistDoubleYouMixAreShe import MixChicagoRadioStations
from ytmusicPlaylistWDVX import UpdateWDVXYesterday
from ytmusicPlaylistWKLQyesterday import UpdateWKLQYesterday
from ytmusicPlaylistWXRTyesterday import UpdateWXRTYesterday
from ytmusicPlaylistWSHEyesterday import UpdateWSHEYesterday
from ytmusicPlaylistWTMXyesterday import UpdateWTMXYesterday
from WDVXCollectPlaylist import WDVXCollectPlaylistData
def runThreaded(function: FunctionType, name: str):
# print(f"{datetime.now().isoformat()} " f"Running: {name}")
job = Thread(target=function, name=name)
job.start()
# @dataclasses
# class scrobble:
# videoId = str
# title = str
# artists = list
# album = str
def KeyCheck(key, documentData, ytmusicData):
if key not in documentData:
if key in ytmusicData:
return True
else:
return False
else:
return False
def BuildRequest(
ytmusicHistoryItem: dict, user: str, scrobbleTime: datetime = None
) -> dict:
if scrobbleTime is None:
scrobbleTime = datetime.utcnow()
artists = [artist["name"] for artist in ytmusicHistoryItem["artists"]]
if "duration" in ytmusicHistoryItem:
duration = ytmusicHistoryItem["duration"]
elif "length" in ytmusicHistoryItem:
duration = ytmusicHistoryItem["length"]
else:
duration = None
try:
album = ytmusicHistoryItem["album"]["name"]
except Exception:
album = None
return {
"time": scrobbleTime,
"videoId": ytmusicHistoryItem["videoId"],
"title": ytmusicHistoryItem["title"],
"artists": artists,
"album": album,
"duration": duration,
"user": user,
}
def PostScrobble(db: Database, request: dict) -> dict:
result = db["scrobbles"].insert_one(request)
return result.inserted_id
def LinkScrobblerSong(ytmusic: YTMusic, db: Database, scrobbleId: ObjectId) -> ObjectId:
scrobbleDocument = db["scrobbles"].find_one({"_id": scrobbleId})
songId = GetSongId(ytmusic, db, scrobbleDocument["videoId"])
if songId:
db["scrobbles"].update_one(
{"_id": scrobbleDocument["_id"]}, {"$set": {"songId": songId}}
)
return songId
def GetLocationFromHomeAssistant(
homeassistantUrl: str,
homeassistantToken: str,
locationEntity: str,
timePoint: datetime = None,
) -> dict:
location = {}
if timePoint is None:
url = f"{homeassistantUrl}/api/states/{locationEntity}"
else:
homeassistantTime = timePoint.strftime("%Y-%m-%dT%H:%M:%S")
url = (
f"{homeassistantUrl}/api/history/period/{homeassistantTime}"
f"?filter_entity_id={locationEntity}&minimal_response"
)
try:
homeassistantResult = requests.get(
url,
headers={
"Authorization": "Bearer " + homeassistantToken,
"Content-Type": "application/json",
},
)
homeassistantData = homeassistantResult.json()
if timePoint is not None:
homeassistantData = homeassistantData[0][0]
location = {
"type": "Point",
"coordinates": [
homeassistantData["attributes"]["longitude"],
homeassistantData["attributes"]["latitude"],
],
}
except Exception:
location = None
return location
def ScrobbleAddLocation(
dbCollection: Collection, scrobbleId: str, location: dict
) -> ReturnDocument:
return dbCollection.update_one(
{"_id": scrobbleId}, {"$set": {"location": location}}
)
def YTMusicScrobble(ytmusic: YTMusic, connectionString: str, user: str) -> None:
mongoClient = MongoClient(connectionString)
db = mongoClient["scrobble"]
scrobblerData = {
"scrobblerId": "607f962eeafb5500062a4a68",
"scrobblerUser": "michael",
"queuedRequests": [],
"requestAttempts": 0,
}
lastScrobbles = (
db["scrobbles"]
.find({"user": scrobblerData["scrobblerUser"]}, projection={"videoId": 1})
.sort("time", DESCENDING)
.limit(1)
)
lastScrobbleIds = [scrobble["videoId"] for scrobble in lastScrobbles]
for i in range(4):
ytmusicHistory = ytmusic.get_history()
if len(ytmusicHistory) == 0:
return
historyVideoIds = [track["videoId"] for track in ytmusicHistory]
matchStartIndex = len(historyVideoIds)
endIndex = len(lastScrobbleIds)
for i in range(len(historyVideoIds)):
historySublist = historyVideoIds[i : i + endIndex]
lastScrobbleSublist = lastScrobbleIds[0 : len(historySublist)]
if historySublist == lastScrobbleSublist:
matchStartIndex = i
break
if matchStartIndex < 1:
return
elif matchStartIndex < 2:
break
else:
print(f"Large number of scrobbles: {matchStartIndex}")
# because ytmusic is so inconsistent with how they report history,
# sometimes songs just disappear and we get the whole 200 songs in the
# history at onces. So, if the length equals 200, just get the last song
matchStartIndex = 1 if matchStartIndex == 200 else matchStartIndex
location = None
for i in reversed(range(matchStartIndex)):
request = BuildRequest(ytmusicHistory[i], user)
scrobbleId = PostScrobble(db, request)
if scrobbleId:
if location is None:
location = GetLocationFromHomeAssistant(
homeassistantUrl,
homeassistantToken,
"person.michael",
)
ScrobbleAddLocation(db["scrobbles"], scrobbleId, location)
LinkScrobblerSong(ytmusic, db, scrobbleId)
print(f"{datetime.now().isoformat()} Posted Scrobble")
return
if __name__ == "__main__":
print("YouTube Music Housekeeping")
chdir("/ytmusic")
ytmusic = None
while not ytmusic:
try:
ytmusic = YTMusic("oauth.json")
except Exception as e:
print(f"{type(e)}: {e}")
time.sleep(10)
user = "michael"
schedule.every().day.at("00:00").do(
runThreaded,
lambda: UpdateWSHEYesterday(
ytmusic, "PLJpUfuX6t6dTyEfFJmvVlIGzcXR1dKFt5", mongoString
),
"WSHE_Yesterday",
)
schedule.every().day.at("00:15").do(
runThreaded,
lambda: UpdateWTMXYesterday(
ytmusic, "PLJpUfuX6t6dS__5F2qgcopS26s5MbO8Yd", mongoString
),
"WSHE_Yesterday",
)
schedule.every().day.at("00:30").do(
runThreaded,
lambda: UpdateWXRTYesterday(
ytmusic, "PLJpUfuX6t6dSaHuu1oeQHWhmMTM6G_hKw", mongoString
),
"WXRT_Yesterday",
)
schedule.every().day.at("00:45").do(
runThreaded,
lambda: UpdateWKLQYesterday(
ytmusic, "PLJpUfuX6t6dRg0mxM5fEufwZOd5eu_DmU", mongoString
),
"WKLQ_Yesterday",
)
schedule.every().day.at("01:30").do(
runThreaded,
lambda: UpdateWDVXYesterday(
ytmusic=ytmusic,
playlistId="PLJpUfuX6t6dTz7hGKVztERi0YnE_azCg1",
dbConnectionString=mongoString,
),
"WDVX Yesterday",
)
schedule.every().day.at("01:00").do(
runThreaded,
lambda: MixChicagoRadioStations(ytmusic=ytmusic),
"Double_You_Mix_Are_She_Playlist",
)
schedule.every().minute.at(":00").do(
runThreaded,
lambda: YTMusicScrobble(ytmusic, mongoString, user),
"Scrobble YTMusic",
)
schedule.every().hour.at("00:30").do(
runThreaded,
lambda: WDVXCollectPlaylistData(
"https://wdvx.com/listen/playlist/", "WDVX-Playlist.json"
),
"Collect WDVX Data",
)
schedule.every().hour.at("30:30").do(
runThreaded,
lambda: WDVXCollectPlaylistData(
"https://wdvx.com/listen/playlist/", "WDVX-Playlist.json"
),
"Collect WDVX Data",
)
YTMusicScrobble(ytmusic, mongoString, user)
while True:
try:
schedule.run_pending()
except Exception as e:
print(f"{datetime.now().isoformat()} " f"Schedule error: {e}")
time.sleep(1)