本文整理汇总了Python中gmusicapi.Musicmanager.is_authenticated方法的典型用法代码示例。如果您正苦于以下问题:Python Musicmanager.is_authenticated方法的具体用法?Python Musicmanager.is_authenticated怎么用?Python Musicmanager.is_authenticated使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类gmusicapi.Musicmanager
的用法示例。
在下文中一共展示了Musicmanager.is_authenticated方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: GoogleClient
# 需要导入模块: from gmusicapi import Musicmanager [as 别名]
# 或者: from gmusicapi.Musicmanager import is_authenticated [as 别名]
class GoogleClient():
def __init__(self, username=None, password=None):
self._username = username
self._password = password
# Initiates the oAuth
def Authenticate(self):
self.MusicManager = Musicmanager(debug_logging=False)
attempts = 0
# Attempt to login. Perform oauth only when necessary.
while attempts < 3:
if self.MusicManager.login():
break
self.MusicManager.perform_oauth()
attempts += 1
if not self.MusicManager.is_authenticated():
print("Sorry, login failed.")
return False
print("OAuth successfull\n")
username = self._username
password = self._password
if not username or not password:
cred_path = os.path.join(os.path.expanduser('~'), '.gmusicfs')
if not os.path.isfile(cred_path):
raise Exception(
'No username/password was specified. No config file could '
'be found either. Try creating %s and specifying your '
'username/password there. Make sure to chmod 600.'
% cred_path)
if not oct(os.stat(cred_path)[os.path.stat.ST_MODE]).endswith('00'):
raise Exception(
'Config file is not protected. Please run: '
'chmod 600 %s' % cred_path)
self.config = ConfigParser.ConfigParser()
self.config.read(cred_path)
username = self.config.get('credentials','username')
password = self.config.get('credentials','password')
if not username or not password:
raise Exception(
'No username/password could be read from config file'
': %s' % cred_path)
self.Api = Mobileclient(debug_logging = False)
if not self.Api.login(username, password, Mobileclient.FROM_MAC_ADDRESS):
raise Exception('login failed for %s' % username)
return True
示例2: GoogleClient
# 需要导入模块: from gmusicapi import Musicmanager [as 别名]
# 或者: from gmusicapi.Musicmanager import is_authenticated [as 别名]
class GoogleClient():
# Initiates the oAuth
def Authenticate(self):
self.MusicManager = Musicmanager(debug_logging=False)
attempts = 0
# Attempt to login. Perform oauth only when necessary.
while attempts < 3:
if self.MusicManager.login():
break
self.MusicManager.perform_oauth()
attempts += 1
if not self.MusicManager.is_authenticated():
print "Sorry, login failed."
return False
print "Successfully logged in.\n"
return True
示例3: Gmusic
# 需要导入模块: from gmusicapi import Musicmanager [as 别名]
# 或者: from gmusicapi.Musicmanager import is_authenticated [as 别名]
class Gmusic(BeetsPlugin):
def __init__(self):
super(Gmusic, self).__init__()
self.m = Musicmanager()
self.config.add({
u'auto': False,
u'uploader_id': '',
u'uploader_name': '',
u'device_id': '',
u'oauth_file': gmusicapi.clients.OAUTH_FILEPATH,
})
if self.config['auto']:
self.import_stages = [self.autoupload]
def commands(self):
gupload = Subcommand('gmusic-upload',
help=u'upload your tracks to Google Play Music')
gupload.func = self.upload
search = Subcommand('gmusic-songs',
help=u'list of songs in Google Play Music library')
search.parser.add_option('-t', '--track', dest='track',
action='store_true',
help='Search by track name')
search.parser.add_option('-a', '--artist', dest='artist',
action='store_true',
help='Search by artist')
search.func = self.search
return [gupload, search]
def authenticate(self):
if self.m.is_authenticated():
return
# Checks for OAuth2 credentials,
# if they don't exist - performs authorization
oauth_file = self.config['oauth_file'].as_str()
if os.path.isfile(oauth_file):
uploader_id = self.config['uploader_id']
uploader_name = self.config['uploader_name']
self.m.login(oauth_credentials=oauth_file,
uploader_id=uploader_id.as_str().upper() or None,
uploader_name=uploader_name.as_str() or None)
else:
self.m.perform_oauth(oauth_file)
def upload(self, lib, opts, args):
items = lib.items(ui.decargs(args))
files = self.getpaths(items)
self.authenticate()
ui.print_(u'Uploading your files...')
self.m.upload(filepaths=files)
ui.print_(u'Your files were successfully added to library')
def autoupload(self, session, task):
items = task.imported_items()
files = self.getpaths(items)
self.authenticate()
self._log.info(u'Uploading files to Google Play Music...', files)
self.m.upload(filepaths=files)
self._log.info(u'Your files were successfully added to your '
+ 'Google Play Music library')
def getpaths(self, items):
return [x.path for x in items]
def search(self, lib, opts, args):
password = config['gmusic']['password']
email = config['gmusic']['email']
uploader_id = config['gmusic']['uploader_id']
device_id = config['gmusic']['device_id']
password.redact = True
email.redact = True
# Since Musicmanager doesn't support library management
# we need to use mobileclient interface
mobile = Mobileclient()
try:
new_device_id = (device_id.as_str()
or uploader_id.as_str().replace(':', '')
or Mobileclient.FROM_MAC_ADDRESS).upper()
mobile.login(email.as_str(), password.as_str(), new_device_id)
files = mobile.get_all_songs()
except NotLoggedIn:
ui.print_(
u'Authentication error. Please check your email and password.'
)
return
if not args:
for i, file in enumerate(files, start=1):
print(i, ui.colorize('blue', file['artist']),
file['title'], ui.colorize('red', file['album']))
else:
if opts.track:
self.match(files, args, 'title')
else:
self.match(files, args, 'artist')
@staticmethod
def match(files, args, search_by):
for file in files:
if ' '.join(ui.decargs(args)) in file[search_by]:
#.........这里部分代码省略.........