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

Python 2 and 3 compatibility #46

Closed
wants to merge 4 commits into from
Closed
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
5 changes: 3 additions & 2 deletions ExportLists.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Author: John Elkins <[email protected]>
# License: MIT <LICENSE>

from builtins import str
from common import *

if len(sys.argv) < 2:
Expand Down Expand Up @@ -35,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 @@ -80,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
20 changes: 13 additions & 7 deletions ImportList.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import datetime
import math
import time
from builtins import str
from common import *

# the file for outputing the information google has one each song
Expand Down Expand Up @@ -117,12 +118,14 @@ def search_for_track(details):
duplicates = 0

# score the match against the query
def score_track(details,result_details,top_score = 200):
def score_track(details,result_details,top_score=None):
global low_scores
global low_titles
global low_artists
global duplicates

if not top_score: top_score = 200

# check for low quality matches
result_score = u' + '
score_reason = u' '
Expand Down Expand Up @@ -168,12 +171,15 @@ 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')
if isinstance(sys.argv[1], str):
input_filename = sys.argv[1]
else:
input_filename = sys.argv[1].decode('utf-8')
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 +298,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 +309,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 +319,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
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,19 @@ gmusic-playlist

playlist scripts for gmusic

## Prerequisites
## Dependencies

- python 2.7 - https://www.python.org
- python v2.7 or v3.x - https://www.python.org
- future - https://pypi.python.org/pypi/future
- gmusicapi - https://github.com/simon-weber/Unofficial-Google-Music-API

To install these dependencies using pip, type:
```
pip install -r requirements.txt
```

## Configuration

Before using the scripts, open up the preferences.py file and change the username.

When the scripts are run they will prompt for your password. If you use two factor authentication you will need to create and use an application password.
Expand Down
20 changes: 15 additions & 5 deletions common.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@

__required_gmusicapi_version__ = '10.0.0'

from builtins import str
from collections import Counter
from future.utils import iteritems
from gmusicapi import __version__ as gmusicapi_version
from gmusicapi import Mobileclient
from gmusicapi.exceptions import CallFailure
Expand Down Expand Up @@ -68,9 +70,17 @@ def close_log():
def log(message, nl = True):
if nl:
message += os.linesep
sys.stdout.write(message.encode(sys.stdout.encoding, errors='replace'))
message = message.encode('utf-8')
try:
sys.stdout.buffer.write(message)
except AttributeError:
sys.stdout.write(message)
sys.stdout.flush()
if logfile:
logfile.write(message)
try:
logfile.write(message)
except TypeError:
logfile.write(message.decode('utf-8'))

# logs a message if debug is true
def dlog(message):
Expand Down Expand Up @@ -105,7 +115,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 iteritems(track):
result_details[key] = value
result_details['songid'] = (track.get('storeId')
if track.get('storeId') else track.get('id'))
Expand Down Expand Up @@ -164,7 +174,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 Down Expand Up @@ -224,7 +234,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
31 changes: 31 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
appdirs==1.4.0
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can this be slimmed down so that it has what the playlist scripts directly depends on?

looks like for now it may just be gmusicapi and future.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's as slim as it gets. That's the output of pip freeze having installed only gmusicapi and future.

On Jul 27, 2016, at 8:28 PM, John Elkins [email protected] wrote:

In requirements.txt:

@@ -0,0 +1,31 @@
+appdirs==1.4.0
can this be slimmed down so that it has what the playlist scripts directly depends on?

looks like for now it may just be gmusicapi and future.


You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub, or mute the thread.

beautifulsoup4==4.5.0
cffi==1.7.0
cryptography==1.4
decorator==4.0.10
enum34==1.1.6
funcsigs==1.0.2
future==0.15.2
gmusicapi==10.0.1
gpsoauth==0.3.0
httplib2==0.9.2
idna==2.1
ipaddress==1.0.16
MechanicalSoup==0.4.0
mock==2.0.0
mutagen==1.34
ndg-httpsclient==0.4.2
oauth2client==2.2.0
pbr==1.10.0
proboscis==1.2.6.0
protobuf==3.0.0b2
pyasn1==0.1.9
pyasn1-modules==0.0.8
pycparser==2.14
pycryptodomex==3.4.2
pyOpenSSL==16.0.0
python-dateutil==2.5.3
requests==2.10.0
rsa==3.4.2
six==1.10.0
validictory==1.0.2