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

Updating to Python 3 and upgrading login to use OAuth #61

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions ExportLists.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def playlist_handler(playlist_name, playlist_description, playlist_tracks):

log('')
log('============================================================')
log(u'Exporting '+ unicode(len(playlist_tracks)) +u' tracks from '
log(u'Exporting '+ str(len(playlist_tracks)) +u' tracks from '
+playlist_name)
log('============================================================')

Expand Down Expand Up @@ -81,7 +81,7 @@ def playlist_handler(playlist_name, playlist_description, playlist_tracks):
# output the stats to the log
log('')
log_stats(stats_results)
log(u'export skipped: '+unicode(export_skipped))
log(u'export skipped: ' + str(export_skipped))

# close the files
close_log()
Expand Down
12 changes: 6 additions & 6 deletions ImportList.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,12 +168,12 @@ def score_track(details,result_details,top_score = 200):


# setup the input and output filenames and derive the playlist name
input_filename = sys.argv[1].decode('utf-8')
input_filename = sys.argv[1]
output_filename = os.path.splitext(input_filename)[0]
output_filename = re.compile('_\d{14}$').sub(u'',output_filename)
playlist_name = os.path.basename(output_filename)

output_filename += u'_' + unicode(datetime.datetime.now().strftime(
output_filename += u'_' + str(datetime.datetime.now().strftime(
'%Y%m%d%H%M%S'))
log_filename = output_filename + u'.log'
csv_filename = output_filename + u'.csv'
Expand Down Expand Up @@ -292,7 +292,7 @@ def score_track(details,result_details,top_score = 200):
total_time = time.time() - start_time

log('===============================================================')
log(u'Adding '+unicode(len(song_ids))+' found songs to: '+playlist_name)
log(u'Adding ' + str(len(song_ids))+' found songs to: '+playlist_name)
log('===============================================================')

# add the songs to the playlist(s)
Expand All @@ -303,7 +303,7 @@ def score_track(details,result_details,top_score = 200):
# build the playlist name, add part number if needed
current_playlist_name = playlist_name
if total_playlists_needed > 1:
current_playlist_name += u' Part ' + unicode(current_playlist)
current_playlist_name += u' Part ' + str(current_playlist)

# create the playlist and add the songs
playlist_id = api.create_playlist(current_playlist_name)
Expand All @@ -313,8 +313,8 @@ def score_track(details,result_details,top_score = 200):

added_songs = api.add_songs_to_playlist(playlist_id,current_songs)

log(u' + '+current_playlist_name+u' - '+unicode(len(added_songs))+
u'/'+unicode(len(current_songs))+' songs')
log(u' + ' + current_playlist_name+u' - ' + str(len(added_songs))+
u'/' + str(len(current_songs))+' songs')

# go to the next playlist section
current_playlist += 1
Expand Down
32 changes: 18 additions & 14 deletions common.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def close_log():
def log(message, nl = True):
if nl:
message += os.linesep
sys.stdout.write(message.encode(sys.stdout.encoding, errors='replace'))
print(str(message))
if logfile:
logfile.write(message)

Expand Down Expand Up @@ -105,7 +105,7 @@ def get_google_track_details(sample_song = 'one u2'):
# creates result details from the given track
def create_result_details(track):
result_details = {}
for key, value in track.iteritems():
for key, value in track.items():
result_details[key] = value
result_details['songid'] = (track.get('storeId')
if track.get('storeId') else track.get('id'))
Expand Down Expand Up @@ -166,7 +166,7 @@ def create_details_string(details_dict, skip_id = False):
if len(out_string) != 0:
out_string += track_info_separator
try:
out_string += handle_quote_output(unicode(details_dict[nfo]))
out_string += handle_quote_output(str(details_dict[nfo]))
except KeyError:
# some songs don't have info like year, genre, etc
pass
Expand All @@ -176,18 +176,22 @@ def create_details_string(details_dict, skip_id = False):
def open_api():
global api
log('Logging into google music...')
# get the password each time so that it isn't stored in plain text
password = getpass.getpass(username + '\'s password: ')

# Attempt to login
api = Mobileclient()
if not api.login(username, password, Mobileclient.FROM_MAC_ADDRESS):
log('ERROR unable to login')
time.sleep(3)
exit()

password = None
log('Login Successful.')
dlog(u'Available track details: '+str(get_google_track_details()))
logged_in = api.oauth_login(Mobileclient.FROM_MAC_ADDRESS)

if not logged_in:
log('No oauth credentials found, please authenticate your account!')

# Performs oauth and stores generated credentials to Appdirs
# 'user_data_dir' by default. oauth only needs to be performed once per
# machine if the credentials are stored, which is the default behavior.
authenticated = api.perform_oauth(open_browser=True)
else:
log('Successfully logged in!')

dlog(u'Available track details: ' + str(get_google_track_details()))
return api

# logs out of the google music api
Expand Down Expand Up @@ -226,7 +230,7 @@ def log_stats(results):
log(u'top 3 genres: '+repr(results['genres'].most_common(3)))
log(u'top 3 artists: '+repr(results['artists'].most_common(3)))
log(u'top 3 years: '+repr(results['years'].most_common(3)))
log(u'playlist playback ratio: '+unicode(results['playback_ratio']))
log(u'playlist playback ratio: ' + str(results['playback_ratio']))

# display version and check prerequisites
log("gmusic-playlist: "+__version__)
Expand Down
4 changes: 0 additions & 4 deletions preferences.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@

# the username to use
username = '[email protected]'

# the separator to use for detailed track information
track_info_separator = u','
#track_info_separator = u'\\'
Expand Down