-
Notifications
You must be signed in to change notification settings - Fork 0
/
mongodbDateCleanup.py
105 lines (88 loc) · 3.47 KB
/
mongodbDateCleanup.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
from pymongo import MongoClient
from secretsFile import mongoString, homeassistantToken, homeassistantUrl
from pprint import pprint
from bson.objectid import ObjectId
from datetime import date, datetime, timedelta
from dateutil import parser, tz
from requests import get
def GrabTime(scrobble):
scrobbleTime = scrobble['time']
fromTZ = tz.gettz("UTC")
toTZ = tz.gettz("America/Detroit")
mongoClient = MongoClient(mongoString)
db = mongoClient['scrobble']
songs = db['scrobbles']
# homeassistantResult = get(homeassistantUrl + "/api/states/person.michael",
# headers={
# "Authorization": "Bearer " + homeassistantToken,
# "Content-Type": "application/json"
# })
# homeassistantData = homeassistantResult.json()
# print(homeassistantData['attributes']['latitude'],
# homeassistantData['attributes']['longitude'])
# songCount = db['scrobbles'].delete_many({'time': {
# "$gte": parser.parse("2021-04-22T04:22:00.000Z"),
# "$lt": parser.parse("2021-04-22T04:24:00.000Z")
# }})
# print(songCount)
# exit()
# scrobbles with unformatted dates
songCount = songs.count_documents({'time': {"$not": {"$type": "date"}}})
print("scrobbles with unformatted dates:", songCount)
# dateless = songs.find({'time': {"$not": {"$type": "date"}}})
# for song in dateless:
# newDate = parser.parse(song['time'])
# result = db['scrobbles'].update_one({"_id": song['_id']}, {"$set": {"time": newDate}})
# print(result.modified_count)
def PrintScrobble(scrobble, toTZ):
scrobbleTimeUTC = scrobble['time'].replace(tzinfo=tz.gettz("UTC"))
scrobbleTimeLocal = scrobbleTimeUTC.astimezone(toTZ)
print(scrobbleTimeLocal.strftime("%Y-%m-%d %H:%M"),
" ",
scrobble['title'],
"-",
", ".join(scrobble['artists']))
# today's scrobbles
today = datetime.combine(date.today(), datetime.min.time())
todayUTC = today.astimezone()
print(todayUTC.isoformat())
songCount = songs.count_documents({"time": {"$gte": todayUTC}})
print("scrobbles from today:", songCount)
for scrobble in songs.find({"time": {"$gte": todayUTC,}})\
.sort("time", -1):
scrobbleTimeUTC = scrobble['time'].replace(tzinfo=fromTZ)
scrobbleTimeLocal = scrobbleTimeUTC.astimezone(toTZ)
print(scrobbleTimeLocal.strftime("%Y-%m-%d %H:%M"),
" ",
scrobble['title'],
"-",
", ".join(scrobble['artists']))
# yesterday's scrobbles
yesterdayUTC = todayUTC - timedelta(days=1)
songCount = songs.count_documents(
{"time": {"$lt": todayUTC, "$gte": yesterdayUTC}}
)
print("scrobbles from yesterday:", songCount)
# last 7 days scrobbles
last7UTC = todayUTC - timedelta(days=7)
songCount = songs.count_documents(
{"time": {"$gte": last7UTC}}
)
print("scrobbles from last 7 days:", songCount)
# search by artist
artist = "Roxy Music"
artistQuery = {"artists": artist}
artistCount = songs.count_documents(artistQuery)
if artistCount == 1:
descriptor = "once"
else:
descriptor = str(artistCount) + " times"
print(artist, "played", descriptor)
firstPlayed = songs.find_one(artistQuery, sort=[("time", 1)])
lastPlayed = songs.find_one(artistQuery, sort=[("time", -1)])
print("first played", firstPlayed["title"], "on",
firstPlayed["time"].strftime("%Y-%m-%d at %H:%M"))
print("last played", lastPlayed["title"], "on",
lastPlayed["time"].strftime("%Y-%m-%d at %H:%M"))
allSongs = songs.find(artistQuery, sort=[("time", -1)])
[PrintScrobble(song, toTZ) for song in allSongs]