-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdeezer-export.py
146 lines (118 loc) · 4.69 KB
/
deezer-export.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# -*- coding: utf-8 -*-
""" Export your Deezer playlists as JSON
This module spins up a server to perform authentication to a
Deezer app (see Readme) through oauth2 protocol and then uses
the official Deezer API to retrieve
and save as JSON file all your playlists.
"""
import os
import time
import webbrowser
import json
from http.server import BaseHTTPRequestHandler, HTTPServer
from threading import Thread
import requests
APP_ID = os.environ.get("APP_ID")
APP_SECRET = os.environ.get("APP_SECRET")
def get_playlist(token, playlist_id):
"""
Use Deezer API to get a playlist from its id.
The only information returned about each playlist is the name and tracklist (a track is described
by the name of the artist/band, the name of the album and the title.
Args:
token (string): The token given by Deezer to authenticate requests
playlist_id (string): The id of the playlist to get
Returns:
A dictionary representing the playlist
"""
req = requests.get("https://api.deezer.com/playlist/{}&access_token={}".format(playlist_id, token))
playlist_item = req.json()
playlist = dict([('name', playlist_item['title']), ('songs', list())])
for title in playlist_item['tracks']['data']:
song = dict(
[
('title', title['title']),
('artist', title['artist']['name']),
('album', title['album']['title'])
]
)
playlist['songs'].append(song)
return playlist
def save_all_playlists(token):
"""
Use Deezer API to get all user playlists and save each one in its own file as JSON.
The only information saved about each playlist is the name and tracklist (a track is described
by the name of the artist/band, the name of the album and the title.
Args:
token (string): The token given by Deezer to authenticate requests
Returns:
None
"""
req = requests.get("https://api.deezer.com/user/me/playlists&access_token={}".format(token))
playlists = list()
for item_playlist in req.json()['data']:
playlist_id = item_playlist['id']
playlist = get_playlist(token, playlist_id)
playlists.append(playlist)
directory = 'playlists_{}'.format(time.time())
if not os.path.exists(directory):
os.makedirs(directory)
for index, playlist in enumerate(playlists):
filename = os.path.join(directory, '{}.json'.format(index))
with open(filename, 'w') as file_descriptor:
json.dump(playlist, file_descriptor)
file_descriptor.close()
class Server(BaseHTTPRequestHandler):
"""Basic HTTP Server to serve as redirection URI and obtain the token from Deezer
"""
def _set_headers(self):
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
def do_GET(self):
if self.path.startswith('/authfinish?code='):
code = self.path.split('=')[1]
print("Found code: {}".format(code))
print('Attempting to obtain token...')
endpoint = "https://connect.deezer.com" + \
"/oauth/access_token.php?app_id={}&secret={}&code={}".format(APP_ID, APP_SECRET, code)
print(endpoint)
time.sleep(5)
req = requests.get(endpoint)
time.sleep(5)
print(req.text)
token = req.text.split('=')[1].split('&')[0]
save_all_playlists(token)
self._set_headers()
self.wfile.write(
"<html><body><h1>hi!</h1>" \
"<script>alert('You can now close this page')</script></body></html>".encode()
)
def do_HEAD(self):
self._set_headers()
def do_POST(self):
# Doesn't do anything with posted data
self._set_headers()
self.wfile.write("<html><body><h1>POST!</h1></body></html>".encode())
def run(port=7766):
""" Run a HTTP server and listen
Args:
port (int): The port on which to listen. Should match the port in your Deezer app config.
"""
server_address = ('', port)
httpd = HTTPServer(server_address, Server)
print('Starting httpd...')
httpd.serve_forever()
if __name__ == "__main__":
if not APP_ID or not APP_SECRET:
raise Exception(
"APP_ID and APP_SECRET environement variables must be defined!" \
"- See Readme for more information on this issue."
)
HTTP_SERVER = Thread(target=run, daemon=True)
HTTP_SERVER.start()
URL = "https://connect.deezer.com" \
"/oauth/auth.php?app_id={}&redirect_uri=http://127.0.0.1:7766/" \
"authfinish&perms=basic_access,email".format(APP_ID)
webbrowser.open(URL)
HTTP_SERVER.join()