Skip to content

Commit

Permalink
Merge branch 'release/v0.3'
Browse files Browse the repository at this point in the history
  • Loading branch information
aartek committed Sep 25, 2015
2 parents ed5e11d + 4516319 commit 2a09683
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 4 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#CHANGELOG

**0.3**
- Added possibility to copy saved tracks from "Your Music/Songs"
- Fixed following collaborative playlists if they was created by user other than 'old user'.
4 changes: 3 additions & 1 deletion Playlist.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ def __init__(self, item):
self.name = item["name"]
self.public = item["public"]
self.collaborative = item["collaborative"]
self.owner = item["owner"]["id"]

def follow_collaborative(self):
payload = {
Expand All @@ -23,8 +24,9 @@ def follow_collaborative(self):
'Content-Type': 'application/json',
'Accept': 'application/json'
}
url = 'https://api.spotify.com/v1/users/'+AppContext.olduser.user_id+'/playlists/'+self.id+'/followers'
url = 'https://api.spotify.com/v1/users/'+self.owner+'/playlists/'+self.id+'/followers'
response = requests.put(url, data=payload, headers=headers, verify=False)
print response

def copy_playlist(self):
try:
Expand Down
2 changes: 1 addition & 1 deletion User.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def _set_initial_params(self):

def login(self):
redirect_uri="http://localhost:7878/user_auth_callback"
scope="playlist-read-private%20playlist-read-collaborative%20playlist-modify-public%20playlist-modify-private%20user-follow-read"
scope="user-library-read%20user-library-modify%20playlist-read-private%20playlist-read-collaborative%20playlist-modify-public%20playlist-modify-private%20user-follow-read"
show_dialog="true"
url = 'https://accounts.spotify.com/authorize?client_id='+AppContext.clientID+'&response_type=code&redirect_uri='+redirect_uri+'&client_secret='+AppContext.clientSecret+'&show_dialog=true&state='+self.authorization_id+'&scope='+scope
webbrowser.open(url)
Expand Down
58 changes: 58 additions & 0 deletions UserTracks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import logging
import AppContext
import requests
import sys
import json
import math
__all__ = ['UserTracks']


class UserTracks:
def __init__(self):
pass

def copy_tracks(self, old_user, new_user):
tracks = self.get_tracks(old_user)
self._save_tracks_for_user(tracks, new_user)

def get_tracks(self, user):
items = []
items = self._fetch_tracks(user.user_id, items, 0)
return items

def _fetch_tracks(self, user_id, items, offset):
headers = {
'Authorization': 'Bearer ' + AppContext.olduser.access_token,
'Content-Type': 'application/json',
'Accept': 'application/json'
}
payload = {
'fields': 'total,items(track.uri)',
'offset': offset,
'limit': 50
}
url = 'https://api.spotify.com/v1/me/tracks'
response = requests.get(url, params=payload, headers=headers, verify=False)
response = response.json()

for item in response['items']:
items.append(item['track']['id'])

total = response['total']
if len(items) < total:
self._fetch_tracks(user_id, items, offset + 50)

return items

def _save_tracks_for_user(self, tracks, user):
url = 'https://api.spotify.com/v1/me/tracks'

headers = {
'Authorization': 'Bearer ' + user.access_token,
'Content-Type': 'application/json',
'Accept': 'application/json'
}
pages = int(math.ceil(float(len(tracks)) / 50))
for i in range(0, pages):
payload = tracks[i*50:(i*50)+50]
requests.put(url, data=json.dumps(payload), headers=headers, verify=False)
16 changes: 15 additions & 1 deletion request_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,19 @@
import json
import requests
import sys
from UserTracks import UserTracks

__all__= ['Hello','Checkusers','UserAuthorized','LoginPrevious','LoginCurrent','SignOutPrevious','SignOutCurrent',
'Playlists','Transfer']

render = web.template.render(AppContext.templates, base='layout')


class Hello:
def GET(self):
return render.index(AppContext.olduser, AppContext.newuser, AppContext.pagination)


class Checkusers:
def GET(self):
currentLoggedIn = True if AppContext.newuser.access_token else False
Expand All @@ -23,6 +26,7 @@ def GET(self):
web.header('Content-Type', 'application/json')
return json.dumps(response)


class UserAuthorized:
def GET(self):
params = web.input()
Expand Down Expand Up @@ -56,26 +60,31 @@ def GET(self):
else:
return render.loginUnsuccessful()


class LoginPrevious:
def GET(self):
AppContext.olduser.login()
raise web.seeother('/')


class LoginCurrent:
def GET(self):
AppContext.newuser.login()
raise web.seeother('/')


class SignOutPrevious:
def GET(self):
AppContext.olduser.logout()
raise web.seeother('/')


class SignOutCurrent:
def GET(self):
AppContext.newuser.logout()
raise web.seeother('/')


class Playlists:
def GET(self):
params = web.input()
Expand All @@ -84,9 +93,10 @@ def GET(self):
AppContext.olduser.loadPlaylists()
raise web.seeother('/')


class Transfer:
def POST(self):
params = web.input(pid = [])
params = web.input(pid=[], copy_tracks=False)
print params

if not AppContext.newuser.access_token or not AppContext.olduser.access_token:
Expand All @@ -100,6 +110,10 @@ def POST(self):
else:
playlist.copy_playlist()

if params['copy_tracks'] == 'true':
user_tracks = UserTracks()
user_tracks.copy_tracks(AppContext.olduser, AppContext.newuser)

raise web.seeother('/')


Expand Down
2 changes: 2 additions & 0 deletions templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ <h1><i class="uk-icon-spotify uk-margin-small-right"></i>Trikatuka <span class="
$if len(olduser.playlists):
<div class="uk-width-1-1">
<form name="playlists" action="transfer" method="post">
<input type="checkbox" name="copy_tracks" value="true" id="copy_tracks"/>
<label for="copy_tracks">Copy songs from "Your music" collection</label>
<table class="uk-table uk-table-hover">
<tr>
<th><input type="checkbox" onclick="selectAll(this)"/></th>
Expand Down
2 changes: 1 addition & 1 deletion templates/layout.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<div class="uk-container uk-container-center">
$:content
<div class="uk-text-small uk-text-muted">
Spotify migration tool v 0.2.2 <a href="http://aknowakowski.blogspot.com">akn</a>
Spotify migration tool v0.3 <a href="http://aknowakowski.blogspot.com">akn</a>
</div>
</div>
</body>
Expand Down

0 comments on commit 2a09683

Please sign in to comment.