initial commit
This commit is contained in:
commit
fcd403044d
3
.gitignore
vendored
Normal file
3
.gitignore
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
env
|
||||
__pycache__
|
||||
*.csv
|
23
backup.py
Normal file
23
backup.py
Normal file
@ -0,0 +1,23 @@
|
||||
import fmframework.io.csv as csvwrite
|
||||
import fmframework.net.user as user
|
||||
|
||||
import sys, datetime, os
|
||||
|
||||
def backupScrobbles():
|
||||
userobj = user.User('sarsoo')
|
||||
|
||||
scrobbles = userobj.getRecentTracks(pagelimit = 2)
|
||||
|
||||
path = sys.argv[1]
|
||||
|
||||
datepath = str(datetime.datetime.now()).split(' ')[0].replace('-', '/')
|
||||
|
||||
totalpath = os.path.join(path, datepath)
|
||||
pathdir = os.path.dirname(totalpath)
|
||||
if not os.path.exists(totalpath):
|
||||
os.makedirs(totalpath)
|
||||
|
||||
csvwrite.exportScrobbles(scrobbles)
|
||||
|
||||
if __name__ == '__main__':
|
||||
backupScrobbles()
|
0
fmframework/__init__.py
Normal file
0
fmframework/__init__.py
Normal file
0
fmframework/io/__init__.py
Normal file
0
fmframework/io/__init__.py
Normal file
26
fmframework/io/csv.py
Normal file
26
fmframework/io/csv.py
Normal file
@ -0,0 +1,26 @@
|
||||
import csv
|
||||
import datetime
|
||||
|
||||
headers = ['track', 'album', 'artist', 'time', 'track id', 'album id', 'artist id']
|
||||
|
||||
def exportScrobbles(scrobbles):
|
||||
|
||||
date = str(datetime.datetime.now())
|
||||
|
||||
with open('scrobbles.csv', 'w') as fileobj:
|
||||
|
||||
writer = csv.DictWriter(fileobj, fieldnames = headers)
|
||||
writer.writeheader()
|
||||
|
||||
for track in scrobbles:
|
||||
|
||||
trackdict = {
|
||||
'track':track['name'],
|
||||
'album':track['album']['#text'],
|
||||
'artist':track['artist']['#text'],
|
||||
'time': datetime.datetime.fromtimestamp(int(track['date']['uts'])),
|
||||
'track id':track['mbid'],
|
||||
'album id':track['album']['mbid'],
|
||||
'artist id':track['artist']['mbid']}
|
||||
|
||||
writer.writerow(trackdict)
|
0
fmframework/net/__init__.py
Normal file
0
fmframework/net/__init__.py
Normal file
48
fmframework/net/user.py
Normal file
48
fmframework/net/user.py
Normal file
@ -0,0 +1,48 @@
|
||||
import os
|
||||
import requests
|
||||
|
||||
class User:
|
||||
|
||||
def __init__(self, username, pagesize = 200):
|
||||
self.api_key = os.environ['FMKEY']
|
||||
|
||||
self.username = username
|
||||
self.pagesize = pagesize
|
||||
|
||||
def __makeRequest(self, method, extra = {}, page = 1):
|
||||
|
||||
data = {
|
||||
"format": 'json',
|
||||
"method": method,
|
||||
"limit": self.pagesize,
|
||||
"page": page,
|
||||
"user": self.username,
|
||||
"api_key": self.api_key
|
||||
}
|
||||
data.update(extra)
|
||||
|
||||
req = requests.post('http://ws.audioscrobbler.com/2.0/', data = data)
|
||||
|
||||
if req.status_code < 200 or req.status_code > 299:
|
||||
|
||||
raise ValueError('HTTP Error Raised: ' + str(req.json()['error']) + ' ' + req.json()['message'])
|
||||
|
||||
return req.json()
|
||||
|
||||
def getRecentTracks(self, offset = 1, pagelimit = 0):
|
||||
|
||||
scrobbles = []
|
||||
|
||||
print(str(offset) + ' offset')
|
||||
|
||||
json = self.__makeRequest('user.getrecenttracks', page = offset)
|
||||
scrobbles += json['recenttracks']['track']
|
||||
|
||||
if pagelimit > 0:
|
||||
if offset < pagelimit and offset < int(json['recenttracks']['@attr']['totalPages']):
|
||||
scrobbles += self.getRecentTracks(offset = offset + 1, pagelimit = pagelimit)
|
||||
else:
|
||||
if offset < int(json['recenttracks']['@attr']['totalPages']):
|
||||
scrobbles += self.getRecentTracks(offset = offset + 1, pagelimit = pagelimit)
|
||||
|
||||
return scrobbles
|
Loading…
Reference in New Issue
Block a user