Skip to content

Commit

Permalink
flake8
Browse files Browse the repository at this point in the history
  • Loading branch information
rckclmbr committed Nov 25, 2017
1 parent c7c4e1d commit 3cf9c3d
Show file tree
Hide file tree
Showing 16 changed files with 182 additions and 235 deletions.
2 changes: 1 addition & 1 deletion pyportify/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '0.3.17'
__version__ = '0.4.0'
81 changes: 31 additions & 50 deletions pyportify/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,10 @@ async def google_login(request):
return json_response(dict(
status=400,
message="login failed.",
))
))
user_scope.google_token = token
return json_response(dict(
status=200,
message="login successful."
))
return json_response({"status": 200,
"message": "login successful."})


async def spotify_login(request):
Expand All @@ -68,16 +66,12 @@ async def spotify_login(request):
c = SpotifyClient(session, oauth_token)
logged_in = await c.loggedin()
if not logged_in:
return json_response(dict(
status=400,
message="login failed.",
))
return json_response({"status": 400,
"message": "login failed."})
user_scope.spotify_token = oauth_token

return json_response(dict(
status=200,
message="login successful."
))
return json_response({"status": 200,
"message": "login successful."})


async def transfer_start(request):
Expand All @@ -86,22 +80,18 @@ async def transfer_start(request):
lists = [l['uri'] for l in lists]

if not user_scope.google_token:
return json_response({
"status": 401,
"message": "Google: not logged in.",
})
return json_response({"status": 401,
"message": "Google: not logged in."})

if not user_scope.spotify_token:
return json_response({
"status": 402,
"message": "Spotify: not logged in.",
})
return json_response({"status": 402,
"message": "Spotify: not logged in."})

if not lists:
return json_response({
"status": 403,
"message": "Please select at least one playlist.",
})
})

sslcontext = ssl.create_default_context(cafile=certifi.where())
conn = aiohttp.TCPConnector(ssl_context=sslcontext)
Expand All @@ -111,21 +101,17 @@ async def transfer_start(request):
s = SpotifyClient(session, user_scope.spotify_token)

await transfer_playlists(request, s, g, lists)
return json_response({
"status": 200,
"message": "transfer will start.",
})
return json_response({"status": 200,
"message": "transfer will start."})


async def spotify_playlists(request):
with ClientSession() as session:
c = SpotifyClient(session, user_scope.spotify_token)
ret_playlists = await c.fetch_spotify_playlists()
return json_response({
"status": 200,
"message": "ok",
"data": ret_playlists
})
return json_response({"status": 200,
"message": "ok",
"data": ret_playlists})


async def transfer_playlists(request, s, g, sp_playlist_uris):
Expand All @@ -138,14 +124,12 @@ async def transfer_playlists(request, s, g, sp_playlist_uris):
uprint(
"Gathering tracks for playlist %s (%s)" %
(sp_playlist['name'], track_count)
)
)
playlist_json = {
"playlist": {
"uri": sp_playlist_uri,
"name": sp_playlist['name'],
},
"playlist": {"uri": sp_playlist_uri,
"name": sp_playlist['name']},
"name": sp_playlist['name'],
}
}

await emit_playlist_length(request, track_count)
await emit_playlist_started(request, playlist_json)
Expand Down Expand Up @@ -192,29 +176,26 @@ async def emit(request, event, data):
async def emit_added_event(request, found, sp_playlist_uri, search_query):
await emit(request, "gmusic", {
"type": "added" if found else "not_added",
"data": {
"spotify_track_uri": sp_playlist_uri,
"spotify_track_name": search_query,
"found": found,
"karaoke": False,
}
})
"data": {"spotify_track_uri": sp_playlist_uri,
"spotify_track_name": search_query,
"found": found,
"karaoke": False}})


async def emit_playlist_length(request, track_count):
await emit(request, "portify",
{"type": "playlist_length",
"data": {"length": track_count}})
{"type": "playlist_length",
"data": {"length": track_count}})


async def emit_playlist_started(request, playlist_json):
await emit(request, "portify",
{"type": "playlist_started", "data": playlist_json})
{"type": "playlist_started", "data": playlist_json})


async def emit_playlist_ended(request, playlist_json):
await emit(request, "portify",
{"type": "playlist_ended", "data": playlist_json})
{"type": "playlist_ended", "data": playlist_json})


async def emit_all_done(request):
Expand All @@ -230,12 +211,12 @@ async def search_gm_track(request, g, sp_query):
if track:
gm_log_found(sp_query)
await emit_added_event(request, True,
sp_query.playlist_uri, search_query)
sp_query.playlist_uri, search_query)
return track.get('storeId')

gm_log_not_found(sp_query)
await emit_added_event(request, False,
sp_query.playlist_uri, search_query)
sp_query.playlist_uri, search_query)
return None


Expand Down
96 changes: 36 additions & 60 deletions pyportify/google.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import urllib
from uuid import getnode as getmac

import asyncio
from pyportify import gpsoauth

SJ_DOMAIN = "mclients.googleapis.com"
Expand Down Expand Up @@ -40,11 +39,9 @@ async def login(self, username, password):
return self.token

async def search_all_access(self, search_query, max_results=30):
data = await self._http_get("/query", {
"q": search_query,
"max-results": max_results,
'ct': '1,2,3,4,6,7,8,9',
})
data = await self._http_get("/query", {"q": search_query,
"max-results": max_results,
"ct": "1,2,3,4,6,7,8,9"})
return data

async def find_best_track(self, search_query):
Expand All @@ -71,61 +68,47 @@ async def fetch_playlists(self):

async def create_playlist(self, name, public=False):
mutations = build_create_playlist(name, public)
data = await self._http_post("/playlistbatch", {
"mutations": mutations,
})
data = await self._http_post("/playlistbatch",
{"mutations": mutations})
res = data["mutate_response"]
playlist_id = res[0]["id"]
return playlist_id

async def add_songs_to_playlist(self, playlist_id, track_ids):
data = {
"mutations": build_add_tracks(playlist_id, track_ids),
}
data = {"mutations": build_add_tracks(playlist_id, track_ids)}
res = await self._http_post('/plentriesbatch', data)
added_ids = [e['id'] for e in res['mutate_response']]
return added_ids

async def _http_get(self, url, params):
headers = {
"Authorization": "GoogleLogin auth={0}".format(self.token),
"Content-type": "application/json",
}
headers = {"Authorization": "GoogleLogin auth={0}".format(self.token),
"Content-type": "application/json"}

merged_params = params.copy()
merged_params.update({
'tier': 'aa',
'hl': 'en_US',
'dv': 0,
})

res = await self.session.request(
'GET',
FULL_SJ_URL + url,
headers=headers,
params=merged_params,
)
merged_params.update({'tier': 'aa',
'hl': 'en_US',
'dv': 0})

res = await self.session.request('GET',
FULL_SJ_URL + url,
headers=headers,
params=merged_params)
data = await res.json()
return data

async def _http_post(self, url, data):
data = json.dumps(data)
headers = {
"Authorization": "GoogleLogin auth={0}".format(self.token),
"Content-type": "application/json",
}
headers = {"Authorization": "GoogleLogin auth={0}".format(self.token),
"Content-type": "application/json"}
res = await self.session.request(
'POST',
FULL_SJ_URL + url,
data=data,
headers=headers,
params={
'tier': 'aa',
'hl': 'en_US',
'dv': 0,
'alt': 'json',
}
)
params={'tier': 'aa',
'hl': 'en_US',
'dv': 0,
'alt': 'json'})
ret = await res.json()
return ret

Expand All @@ -137,17 +120,13 @@ def build_add_tracks(playlist_id, track_ids):
next_id = str(uuid.uuid1())

for i, track_id in enumerate(track_ids):
details = {
"create": {
"clientId": cur_id,
"creationTimestamp": "-1",
"deleted": False,
"lastModifiedTimestamp": "0",
"playlistId": playlist_id,
"source": 1,
"trackId": track_id,
}
}
details = {"create": {"clientId": cur_id,
"creationTimestamp": "-1",
"deleted": False,
"lastModifiedTimestamp": "0",
"playlistId": playlist_id,
"source": 1,
"trackId": track_id}}

if track_id.startswith("T"):
details["create"]["source"] = 2 # AA track
Expand All @@ -168,16 +147,13 @@ def build_add_tracks(playlist_id, track_ids):

def build_create_playlist(name, public):
return [{
"create": {
"creationTimestamp": "-1",
"deleted": False,
"lastModifiedTimestamp": 0,
"name": name,
"description": "",
"type": "USER_GENERATED",
"shareState": "PUBLIC" if public else "PRIVATE",
}
}]
"create": {"creationTimestamp": "-1",
"deleted": False,
"lastModifiedTimestamp": 0,
"name": name,
"description": "",
"type": "USER_GENERATED",
"shareState": "PUBLIC" if public else "PRIVATE"}}]


def parse_auth_response(s):
Expand Down
22 changes: 13 additions & 9 deletions pyportify/gpsoauth/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@ def _perform_auth_request(data):


def perform_master_login(email, password, android_id,
service='ac2dm', device_country='us', operatorCountry='us',
lang='en', sdk_version=17):
service='ac2dm', device_country='us',
operatorCountry='us', lang='en', sdk_version=17):
"""
Perform a master login, which is what Android does when you first add a Google account.
Perform a master login, which is what Android does when you first add a
Google account.
Return a dict, eg::
Expand All @@ -52,23 +53,26 @@ def perform_master_login(email, password, android_id,
'Email': email,
'has_permission': 1,
'add_account': 1,
'EncryptedPasswd': google.signature(email, password, android_key_7_3_29),
'EncryptedPasswd':
google.signature(email, password, android_key_7_3_29),
'service': service,
'source': 'android',
'androidId': android_id,
'device_country': device_country,
'operatorCountry': device_country,
'lang': lang,
'sdk_version': sdk_version
}
'sdk_version': sdk_version,
}

return _perform_auth_request(data)


def perform_oauth(email, master_token, android_id, service, app, client_sig,
device_country='us', operatorCountry='us', lang='en', sdk_version=17):
device_country='us', operatorCountry='us', lang='en',
sdk_version=17):
"""
Use a master token from master_login to perform OAuth to a specific Google service.
Use a master token from master_login to perform OAuth to a specific Google
service.
Return a dict, eg::
Expand Down Expand Up @@ -98,6 +102,6 @@ def perform_oauth(email, master_token, android_id, service, app, client_sig,
'operatorCountry': device_country,
'lang': lang,
'sdk_version': sdk_version
}
}

return _perform_auth_request(data)
4 changes: 1 addition & 3 deletions pyportify/gpsoauth/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@


def bytes_to_long(s):
if PY3:
return int.from_bytes(s, "big")
return long(s.encode('hex'), 16)
return int.from_bytes(s, "big")


def long_to_bytes(lnum, padmultiple=1):
Expand Down
2 changes: 0 additions & 2 deletions pyportify/middlewares.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import asyncio


def IndexMiddleware(index='index.html'):
async def middleware_factory(app, handler):
Expand Down
Loading

0 comments on commit 3cf9c3d

Please sign in to comment.