forked from ModalSeoul/Ripfm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ripfm.py
81 lines (64 loc) · 2.27 KB
/
ripfm.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
import requests
import json
from time import sleep
# Don't touch these
Auth = 'https://modal.moe/api/api-token-auth/'
Scrobble = 'https://modal.moe/api/scrobbles/'
Key = '7d25207fefda375df7db633eae91ef88'
Secret = 'be6ad97265f13b44efba96859d75c01d'
# Aliases
post = requests.post
get = requests.get
class Wilt:
def __init__(self):
self.user = input('Username: ')
self.password = input('Password: ')
self.last_fm = input('Last.fm Username: ')
self.logged_in = False
self.header = {'Authorization': 'Token {}'.format(self.login())}
self.last_played = '' # Clarity
def login(self):
r = post(Auth, data={'username': self.user, 'password': self.password})
if 'token' in r.text:
self.logged_in = True
else:
print('Something went wrong - Not loggined in!')
return None
return json.loads(r.text)['token']
def check_last(self, song):
song = song.decode()
try:
r = get('{}?active={}'.format(Scrobble, self.user))
resp = json.loads(r.text)[0]
if resp['song_name'].lower() == song.lower():
return True
else:
return False
except Exception as e:
print('Exception, is the backend down? {}'.format(e))
def scrobble(self, scrobble):
if scrobble['song'] != self.last_played:
if not self.check_last(scrobble['song']):
r = post(Scrobble, data=scrobble, headers=self.header)
self.last_played = scrobble['song']
else:
print('This song was the last song scrobbled.')
else:
return None
# Instantiating our Wilt class
Wilt = Wilt()
def recent_url(user):
return ('http://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks'
'&user={}&api_key={}&format=json'.format(user, Key))
def rip():
r = get(recent_url(Wilt.last_fm))
track_info = []
response = json.loads(r.text)['recenttracks']['track'][0]
# Encoding for the weebs
song = response['name'].encode('utf-8')
artist = response['artist']['#text'].encode('utf-8')
Wilt.scrobble({'song': song, 'artist': artist})
if __name__ == '__main__':
while 1:
rip()
sleep(15)