This repository has been archived by the owner on Feb 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathradiorock2spotify.py
executable file
·93 lines (83 loc) · 3.67 KB
/
radiorock2spotify.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
#!/usr/bin/env python
# encoding: utf-8
"""
Parse radiorock.fi playlogs into spotify playlists
"""
import sys
import spotipy
import spotipy.util as util
import logging
import argparse
import os.path
from any2spotify import *
def main(argv=None):
parser = argparse.ArgumentParser(description='Sync feed with spotify playlist')
parser.add_argument('username',help='spotify username/account to sync the playlist to',default="aarnoaukia")
parser.add_argument('feed',help='playlog URL, e.g. http://www.radiorock.fi/api/programdata/getlatest',default="http://www.radiorock.fi/api/programdata/getlatest")
#parser.add_argument('--id',required=False,default=None,help='spotify playlist ID to sync to. overrides the playlist name below.')
#parser.add_argument('--name',required=False,default=None,help='spotify playlist name to sync to. the playlist is created if there is no such playlist for the user. the feed title is used if omitted.')
parser.add_argument('-v','--verbose',help='output debug logging',action='store_true',default=False)
parser.add_argument('-a','--add',help='only add songs to the playlist, dont remove them if missing from the feed',action='store_true',default=False)
parser.add_argument('-l','--limit',help='limit the total number of tracks in the playist (useful with --add, default=0=no limit)',default=0)
args = parser.parse_args()
logformat = '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
if args.verbose:
logging.basicConfig(level=logging.DEBUG, format=logformat)
else:
logging.basicConfig(level=logging.INFO, format=logformat)
logging.getLogger('requests.packages.urllib3.connectionpool').setLevel(logging.WARNING)
logging.debug("got args: %s" % args)
token = spotipy.util.prompt_for_user_token(args.username,'playlist-modify-public')
logging.debug("go auth token")
spotify = spotipy.Spotify(auth=token)
logging.debug("logged in to spotify")
# from the feed we get about 72h of playlogs, but no indication what program was running at the time to split it up
# so here is the map how to split them up
playlistmap = [
{
'weekday': 2, #ke
'from': datetime.time(19),
'to': datetime.time(21),
'playlist': 'spotify:user:aarnoaukia:playlist:6jkZf1uscq2AwGWKf7To0w',
},
{
'weekday': 3, #to
'from': datetime.time(19),
'to': datetime.time(21),
'playlist': 'spotify:user:aarnoaukia:playlist:4OdqoyHT3mVndTwwrEhMjp',
},
{
'weekday': 4, #pe
'from': datetime.time(19),
'to': datetime.time(21),
'playlist': 'spotify:user:aarnoaukia:playlist:5JmDRoineh4NuGjYD5zOvb',
},
{
'weekday': 5, #la
'from': datetime.time(18),
'to': datetime.time(19),
'playlist': 'spotify:user:aarnoaukia:playlist:3PObJM8VTktdZE4kBH79xo',
},
{
'weekday': '*', #kaikki muut päivät
'from': datetime.time(0),
'to': datetime.time(23,59,59),
'playlist': 'spotify:user:aarnoaukia:playlist:5BEhl8eSv2l1DRECv8DXAH',
},
]
songmap = {}
songlog = get_radiorock_songlog()
logging.info("got {0} songs".format(len(songlog)))
logging.debug(songlog)
for song in songlog:
playlist = get_playlist_for_timestamp(song['timestamp'],playlistmap)
if playlist not in songmap:
songmap[playlist] = []
songmap[playlist].append(song)
logging.debug("all songs assigned, syncing playlists")
for playlist in songmap.keys():
logging.debug("syncing playlist {0}".format(x for x in songmap if x['playlist'] == playlist))
sync_tracks(spotify_search_songs(songmap[playlist],spotify),playlist,args.username,spotify,addonly=args.add,limit=args.limit)
if __name__ == "__main__":
"""Boilerplate main function call"""
sys.exit(main(sys.argv))