<commit_before>﻿#!/usr/bin/python
# -*- coding: utf_8 -*-

## Command line client for Google Play Music
## Copyright: <NAME> 2012-13
## dan-nixon.com
## Version: 0.3.5
## Date: 23/08/2013

import thread, time, shlex, random, sys
from gmusicapi import Webclient
from operator import itemgetter
from getpass import getpass
import gobject, glib, pygst
import gst

m_client = None
m_player = None
last_fm = None
clh = None
run = True

class switch(object):
	def __init__(self, value):
		self.value = value
		self.fall = False

	def __iter__(self):
		yield self.match
		raise StopIteration

	def match(self, *args):
		if self.fall or not args:
			return True
		elif self.value in args:
			self.fall = True
			return True
		else:
			return False

class gMusicClient(object):
	logged_in = False
	api = None
	playlists = dict()
	library = dict()

	def __init__(self, email, password):
		self.api = Webclient()
		logged_in = False
		attempts = 0
		if len(password) is 0:
			password = getpass("<PASSWORD>:")
		while not self.logged_in and attempts < 3:
			self.logged_in = self.api.login(email, password)
			attempts += 1

	def __del__(self):
		self.api.logout()

	def updateLocalLib(self):
		songs = list()
		self.library = dict()
		self.playlists = dict()
		songs = self.api.get_all_songs()
		for song in songs:
			song_title = song["title"]
			if song["artist"] == "":
				song_artist = "Unknown Artist"
			else:
				song_artist = song["artist"]
			if song["album"] == "":
				song_album = "Unknown Album"
			else:
				song_album = song["album"]
			if not (song_artist in self.library):
				albums_dict = dict()
				self.library[song_artist] = albums_dict
			if not (song_album in self.library[song_artist]):
				song_list = list()
				self.library[song_artist][song_album] = song_list
			self.library[song_artist][song_album].append(song)
		plists = self.api.get_all_playlist_ids(auto=True, user=True)
		for u_playlist, u_playlist_id in plists["user"].iteritems():
			self.playlists[u_playlist] = self.api.get_playlist_songs(u_playlist_id[0])
		self.playlists["Thumbs Up"] = [song for song in songs if song['rating'] == 5]

	def getSongStream(self, song):
		url = self.api.get_stream_urls(song["id"])[0]
		return url

	def thumbsUp(self, song):
		try:
			song["rating"] = 5
			song_list = [song]
			self.api.change_song_metadata(song_list)
			print "Gave a Thumbs Up to {0} by {1} on Google Play.".format(song["title"].encode("utf-8"), song["artist"].encode("utf-8"))
		except:
			print "Error giving a Thumbs Up on Google Play."

class mediaPlayer(object):
	player = None
	now_playing_song = None
	queue = list()
	queue_index = -1
	play_mode = 0

	def __init__(self):
		thread.start_new_thread(self.playerThread, ())

	def __del__(self):
		self.now_playing_song = None
		self.player.set_state(gst.STATE_NULL)

	def clear_queue(self):
		self.stopPlayback()<commit_msg>Updated to work with All Access. Currently All Access songs require full download up front. This is because Google puposefully obfuscates AA songs in a series of URLs. The chunks all need to be downloaded and then spliced.<commit_after>﻿#!/usr/bin/python
# -*- coding: utf_8 -*-

## Command line client for Google Play Music
## Copyright: <NAME> 2012-13
## dan-nixon.com
## Version: 0.3.5
## Date: 23/08/2013

import thread, time, shlex, random, sys
from gmusicapi import Webclient
from operator import itemgetter
from getpass import getpass
import gobject, glib, pygst
import gst

m_client = None
m_player = None
last_fm = None
clh = None
run = True

class switch(object):
	def __init__(self, value):
		self.value = value
		self.fall = False

	def __iter__(self):
		yield self.match
		raise StopIteration

	def match(self, *args):
		if self.fall or not args:
			return True
		elif self.value in args:
			self.fall = True
			return True
		else:
			return False

class gMusicClient(object):
	logged_in = False
	api = None
	playlists = dict()
	library = dict()

	def __init__(self, email, password):
		self.api = Webclient()
		logged_in = False
		attempts = 0
		if len(password) is 0:
			password = getpass("<PASSWORD>:")
		while not self.logged_in and attempts < 3:
			self.logged_in = self.api.login(email, password)
			attempts += 1

	def __del__(self):
		self.api.logout()

	def updateLocalLib(self):
		songs = list()
		self.library = dict()
		self.playlists = dict()
		songs = self.api.get_all_songs()
		for song in songs:
			song_title = song["title"]
			if song["artist"] == "":
				song_artist = "Unknown Artist"
			else:
				song_artist = song["artist"]
			if song["album"] == "":
				song_album = "Unknown Album"
			else:
				song_album = song["album"]
			if not (song_artist in self.library):
				albums_dict = dict()
				self.library[song_artist] = albums_dict
			if not (song_album in self.library[song_artist]):
				song_list = list()
				self.library[song_artist][song_album] = song_list
			self.library[song_artist][song_album].append(song)
		plists = self.api.get_all_playlist_ids(auto=True, user=True)
		for u_playlist, u_playlist_id in plists["user"].iteritems():
			self.playlists[u_playlist] = self.api.get_playlist_songs(u_playlist_id[0])
		self.playlists["Thumbs Up"] = [song for song in songs if song['rating'] == 5]

	def getSongStream(self, song):
		urls = self.api.get_stream_urls(song["id"])
		if len(urls) > 1:
			print "Retrieving audio for %s" % (song["title"])
			audio = self.api.get_stream_audio(song["id"])
			with open("/tmp/audio.mp3",'wb') as output:
				output.write(audio)
			return "file:///tmp/audio.mp3"
		else:
			return urls[0]
		return "file:///tmp/audio.mp3"

	def thumbsUp(self, song):
		try:
			song["rating"] = 5
			song_list = [song]
			self.api.change_song_metadata(song_list)
			print "Gave a Thumbs Up to {0} by {1} on Google Play.".format(song["title"].encode("utf-8"), song["artist"].encode("utf-8"))
		except:
			print "Error giving a Thumbs Up on Google Play."

class mediaPlayer(object):
	player = None
	now_playing_song = None
	queue = list()
	queue_index = -1
	play_mode = 0

	def __init__(self):
		thread.start_new_thread(self.playerThread, ())

	def __del__(self):
		self.now_playing_song = None
		self.player.set_state(gst.STATE_NULL)

	def clear_queue(self):
		self.stopPlayback()
{'fraction_non_alphanumeric': 0.1282629713180793, 'fraction_numerical': 0.010957138253303255, 'mean_word_length': 3.985542168674699, 'pattern_counts': {'":': 4, '<': 9, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 5, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1722052', 'n_tokens_mistral': 2525, 'n_tokens_neox': 2189, 'n_words': 579}