· 5 years ago · Jun 30, 2020, 09:20 PM
1import os
2import requests, dataset, base64
3from time import sleep
4from datetime import datetime
5
6
7#connect to database so we can add the songs
8db = dataset.connect('sqlite:///songlist.db')
9
10
11tagquestion = input('do you want to grab genre tags along with your scrobbler history? (takes about 2x as long) y/n: ')
12if tagquestion == 'y':
13 gettags = True
14else:
15 gettags= False
16
17userquestion = input('username to search?: ')
18songtable = db[userquestion]
19pagetable = db["pages"]
20
21
22#build the request sent to last.fm
23def get_recent_tracks(page):
24 # set vars
25 base_url = "http://ws.audioscrobbler.com/2.0/"
26 #refer to last.fm's API documentation, there are tons of different methods you can use
27 method = "user.getrecenttracks"
28 user = userquestion
29 #https://www.last.fm/api/account/create
30 key = "YOUR KEY HERE"
31 #exclude data_format if you want to parse XML instead
32 data_format = "json"
33 #extended gives the date it was scrobbled, if the user has loved the track, etc.
34 extended = '1'
35 limit = '200'
36 #check if page number is specified. if not, get the initial data. this is used to find the total # of pages there are, total # of tracks scrobbled, etc.
37 if page == None:
38 print ("getting # of pages")
39 payload = {"method": method,
40 "user": user,
41 "api_key": key,
42 "format": data_format,
43 "extended": extended,
44 "limit": limit}
45 else:
46 payload = {"method": method,
47 "user": user,
48 "api_key": key,
49 "format": data_format,
50 "extended": extended,
51 "limit": limit,
52 "page": page}
53 print ("getting request for", user)
54 #put it all together and use requests to get the json
55 r = requests.get(base_url, payload)
56 data = r.json()
57 print ("http status:", r.status_code)
58 return data
59
60
61
62def get_tags(artist,track):
63 # set vars
64 base_url = "http://ws.audioscrobbler.com/2.0/"
65 #refer to last.fm's API documentation, there are tons of different methods you can use
66 method = "track.getTopTags"
67 #https://www.last.fm/api/account/create
68 key = "YOUR KEY HERE"
69 #exclude data_format if you want to parse XML instead
70 data_format = "json"
71 #extended gives the date it was scrobbled, if the user has loved the track, etc.
72 payload = {"method": method,
73 "artist": artist,
74 "api_key": key,
75 "format": data_format,
76 "track": track,
77 #autocorrect uses last.fm to find the info you are looking for even if mis-spelled
78 "autocorrect": 1}
79 tagr = requests.get(base_url, payload)
80 tagdata = tagr.json()
81 songtags = []
82
83 for x in range(0,5):
84 try:
85 songtags.append(tagdata['toptags']['tag'][x]['name'])
86 except:
87 pass
88 return (songtags)
89
90def get_history():
91 data = get_recent_tracks(None)
92 pages = data['recenttracks']['@attr']['totalPages']
93 songs = data['recenttracks']['@attr']['total']
94 #add current page to database: to keep track of where it was stopped at
95
96 print (pages,"pages total", songs,"songs scrobbled")
97 try:
98 pages = pagetable.find_one(pages)["pages"]
99 print ("last run got to page {}".format(pages))
100 except:
101 print ("First run: grabbing all pages")
102 for page in range (int(pages),0,-1):
103 try:
104 #reason I step it backwards from highest to lowest is because I want the item ID's in the database to look nice :)
105 #1st item in database is the oldest song in the scrobble history
106 data = get_recent_tracks(page)
107 print ("Checking page {}...".format(page))
108 items = (data['recenttracks']['track'])
109 inlist = int(len(items))
110 #last.fm uses 1 as first number instead of 0
111 print(inlist - 1 ,"tracks on this page")
112
113 #same reason here
114 for x in range(inlist-1, 0,-1):
115 #print('checking song', x)
116 #pull all the information out that we want from the json data
117 tracks = data['recenttracks']['track'][int(x)]
118 time= (tracks['date']['uts'])
119 timezone_corrected_time= (datetime.fromtimestamp(int(time)).strftime('%Y-%m-%d %H:%M:%S'))
120 img = tracks['image'][-1]['#text']
121 artistmbid = str(tracks['artist']['mbid'])
122 albumbid = str(tracks['album']['mbid'])
123 artist = tracks['artist']['name']
124 song = tracks['name']
125 album = tracks['album']['#text']
126 url = tracks['url']
127
128 search = songtable.find_one(time=timezone_corrected_time)
129 #find if this song has been added in the past, by looking at its scrobble timestamp
130 if search:
131 pass
132 else:
133 if gettags:
134 tags = ', '.join(get_tags(artist,song))
135 else:
136 tags = ''
137 songtable.insert(dict(artist=artist, artistmbid=artistmbid, album=album, albumbid=albumbid,img=img, song=song,time=timezone_corrected_time,url=url, tags=tags) )
138 data = dict(id=1,pages = page,song = x)
139 pagetable.upsert(data, ["id"])
140 print ("New Song! page{0}.song{1}: adding {2} by {3}, scrobbled at {4}".format(page,x,song,artist,timezone_corrected_time))
141 print("finished page {}".format(page))
142 data = dict(id=1,pages = page)
143 pagetable.upsert(data, ["id"])
144 except Exception as e:
145 print (e)
146 print ("Timeout....waiting 1 min...")
147 sleep(60)
148running = True
149while running:
150 get_history()