Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No more 400 song limit. Added argument playlist / song length / json pretty print #3

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 29 additions & 10 deletions deezerexport.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.

import json
import time
try:
from http.client import HTTPSConnection
except ImportError: # python2
Expand All @@ -39,35 +40,53 @@ def get_user_playlists(userid):
return get_data(url)

def get_playlist(playlistid):
url = deezer_api + "playlist/%d" % playlistid
return get_data(url)
url = deezer_api + "playlist/%d/tracks" % playlistid
playlist = get_data(url)
while "next" in playlist:
time.sleep(.1)
more_tracks = get_data(playlist["next"])
if "data" in more_tracks:
playlist["data"].extend(more_tracks["data"])
if "next" in more_tracks:
playlist["next"] = more_tracks["next"]
else:
playlist.pop("next")
if playlist["total"] != len(playlist["data"]):
print("WARNING! Not all tracks were loaded for %d" % playlistid)
return playlist

if __name__ == "__main__":
parser = argparse.ArgumentParser(description = 'Deezer export script')
parser.add_argument('uid', type = int, help = "User identifier")
parser.add_argument('-j', '--json', '--export', dest = "export",
help = "Export playlists in a simple json format"
parser.add_argument('-j', '--json', '--export', dest = "export", help = "Export playlists in a simple json format"
"to the specified file")
parser.add_argument('-pl', '--playlist', dest = "playlist", help = "Export only this list")
args = parser.parse_args()
userdata = get_user_info(args.uid)
print("Playlists for user %s" % userdata["name"])
playlists = get_user_playlists(args.uid)
playlists_data = []
for row in playlists["data"]:
if args.playlist is not None and args.playlist != row["title"]:
continue
playlistid = row["id"]
playlist = get_playlist(playlistid)
playlist["title"] = row["title"]
playlists_data.append(playlist)
print(" Playlist %s" % playlist["title"])
for track in playlist["tracks"]["data"]:
print(" Playlist %s" % row["title"])
for track in playlist["data"]:
print(" %s -- by %s" % (track["title"], track["artist"]["name"]))
if args.playlist is not None and not playlists_data:
print(" Playlist %s not found." % args.playlist)
if args.export is not None:
data = {"uid": args.uid, "user": userdata["name"], "playlists": []}
for playlist in playlists_data:
tracks = [{"title": track["title"],
"artist": track["artist"]["name"],
"album": track["album"]["title"]}
for track in playlist["tracks"]["data"]]
cur_playlist = {"title": playlist["title"], "tracks": tracks}
"album": track["album"]["title"],
"length": track["duration"]}
for track in playlist["data"]]
cur_playlist = {"title": playlist["title"] + " (imported from Deezer)", "tracks": tracks}
data["playlists"].append(cur_playlist)
with open(args.export, "w") as f:
json.dump(data, f)
json.dump(data, f, indent=2)