当前位置: 首页>>代码示例>>Python>>正文


Python Webclient.add_songs_to_playlist方法代码示例

本文整理汇总了Python中gmusicapi.Webclient.add_songs_to_playlist方法的典型用法代码示例。如果您正苦于以下问题:Python Webclient.add_songs_to_playlist方法的具体用法?Python Webclient.add_songs_to_playlist怎么用?Python Webclient.add_songs_to_playlist使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在gmusicapi.Webclient的用法示例。


在下文中一共展示了Webclient.add_songs_to_playlist方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: MusicSync

# 需要导入模块: from gmusicapi import Webclient [as 别名]
# 或者: from gmusicapi.Webclient import add_songs_to_playlist [as 别名]
class MusicSync(object):
    def __init__(self, email=None, password=None):
        self.mm = Musicmanager()
        self.wc = Webclient()
        if not email:
            email = raw_input("Email: ")
        if not password:
            password = getpass()

        self.email = email
        self.password = password

        self.logged_in = self.auth()

        print "Fetching playlists from Google..."
        self.playlists = self.wc.get_all_playlist_ids(auto=False)
        print "Got %d playlists." % len(self.playlists['user'])
        print ""


    def auth(self):
        self.logged_in = self.wc.login(self.email, self.password)
        if not self.logged_in:
            print "Login failed..."
            exit()

        print ""
        print "Logged in as %s" % self.email
        print ""

        if not os.path.isfile(OAUTH_FILEPATH):
            print "First time login. Please follow the instructions below:"
            self.mm.perform_oauth()
        self.logged_in = self.mm.login()
        if not self.logged_in:
            print "OAuth failed... try deleting your %s file and trying again." % OAUTH_FILEPATH
            exit()

        print "Authenticated"
        print ""


    def sync_playlist(self, artist_title_array, playlist_title = -99):
        if playlist_title == -99:
            title = "GMusicSync Playlist %3d"%time.time()
        else: title = str(playlist_title)
        print "Synching playlist: %s" % title
        if title not in self.playlists['user']:
            print "   didn't exist... creating..."
            self.playlists['user'][title] = [self.wc.create_playlist(title)]
        print ""

        plid = self.playlists['user'][title][0]
        goog_songs = self.wc.get_playlist_songs(plid)
        print "%d songs already in Google Music playlist" % len(goog_songs)
        pc_songs = artist_title_array
        print "%d songs in local playlist" % len(pc_songs)

        # Sanity check max 1000 songs per playlist
        if len(pc_songs) > MAX_SONGS_IN_PLAYLIST:
            print "    Google music doesn't allow more than %d songs in a playlist..." % MAX_SONGS_IN_PLAYLIST
            print "    Will only attempt to sync the first %d songs." % MAX_SONGS_IN_PLAYLIST
            del pc_songs[MAX_SONGS_IN_PLAYLIST:]

        existing_files = 0
        added_files = 0
        failed_files = 0
        removed_files = 0
        fatal_count = 0

        for fn in pc_songs:
            if self.file_already_in_list(fn, goog_songs):
                existing_files += 1
                continue
            print ""
            try:
                print "Adding: %s - %s"%(fn[0],fn[1])
            except:
                print "Incorrect format for %r, expecting ('artist','title')"%fn
                continue
            online = self.find_song(fn)
            song_id = None
            if online:
                song_id = online['id']
                print "   already uploaded [%s]" % song_id
            else:
                print "   Sorry, can't find song."

            if not song_id:
                failed_files += 1
                continue

            added = self.wc.add_songs_to_playlist(plid, song_id)
            time.sleep(.3) # Don't spam the server too fast...
            print "   done adding to playlist"
            added_files += 1

        print ""
        print "---"
        print "%d songs unmodified" % existing_files
#.........这里部分代码省略.........
开发者ID:gaulinmp,项目名称:GoogleMusicPlaylistCreator,代码行数:103,代码来源:musicsync.py

示例2: MusicSync

# 需要导入模块: from gmusicapi import Webclient [as 别名]
# 或者: from gmusicapi.Webclient import add_songs_to_playlist [as 别名]

#.........这里部分代码省略.........
        print "Starting Playlist Sync with Google music..."
        for song in pc_songs:
            # print song

            plid = ""

            print "--------------------------------"
            print ""
            print "Playlist: %s" % song["playlist"]
            print "Artist: %s" % song["artist"]
            print "Song: %s" % song["title"]
            print "Album: %s" % song["album"]

            playlist_title = song["playlist"]

            plid = self.playlists["user"][playlist_title][0]

            goog_songs = self.wc.get_playlist_songs(plid)

            if self.song_already_in_list(song, goog_songs):
                existing_files += 1
                print "Result: Song Already Added"
                continue
            print "Total %d songs in Google playlist: %s" % (len(goog_songs), playlist_title)
            print "%s - %s,   didn't exist...Will try to add..." % (song["artist"], song["title"])

            print ""
            print "--------------------------------"
            results = self.mc.search_all_access(song["title"], max_results=50)
            nid = self.filter_search_results(results, song)
            print "AA nId: %s " % nid
            if nid:
                song_id = self.mc.add_aa_track(nid)
                added = self.wc.add_songs_to_playlist(plid, song_id)

                print "Playlist UUid: %s" % plid
                print "Song ID: %s" % song_id
                time.sleep(0.3)  # Don't spam the server too fast...
                print "Result: done adding to playlist"
                added_files += 1
                continue
            else:
                query = "%s %s" % (song["artist"], song["title"].split(" ")[0])
                print "Query %s" % query
                results = self.mc.search_all_access(query, max_results=50)
                nid = self.filter_search_results(results, song)
                if nid:
                    song_id = self.mc.add_aa_track(nid)
                    added = self.wc.add_songs_to_playlist(plid, song_id)

                    print "Playlist UUid: %s" % plid
                    print "Song ID: %s" % song_id
                    time.sleep(0.3)  # Don't spam the server too fast...
                    print " -- done adding to playlist"
                    added_files += 1
                    continue
            print "Result: NID Blank, Song not Found in All Access"

        print ""
        print "---"
        print "%d songs unmodified" % existing_files
        print "%d songs added" % added_files
        print "%d songs failed" % failed_files
        print "%d songs removed" % removed_files

    def get_songs_from_file(self, filename):
开发者ID:MrZaius,项目名称:Rhapsody-to-Google-Music,代码行数:70,代码来源:musicsync.py

示例3: MusicSync

# 需要导入模块: from gmusicapi import Webclient [as 别名]
# 或者: from gmusicapi.Webclient import add_songs_to_playlist [as 别名]

#.........这里部分代码省略.........
                        attempts += 1
                        try:
                            result = self.mm.upload(fn)
                        except (BadStatusLine, CannotSendRequest):
                            # Bail out if we're getting too many disconnects
                            if fatal_count >= MAX_CONNECTION_ERRORS_BEFORE_QUIT:
                               print "Too many disconnections - quitting. Please try running the script again."
                               exit()
    
                            print "Connection Error -- Reattempting login"
                            fatal_count += 1
                            self.wc.logout()
                            self.mm.logout()
                            result = []
                            time.sleep(STANDARD_SLEEP)
    
                        except:
                            result = []
                            time.sleep(STANDARD_SLEEP)
    
                    try:
                        if result[0]:
                            song_id = result[0].itervalues().next()
                        else:
                            song_id = result[1].itervalues().next()
                        print "  upload complete [%s]" % song_id
                    except:
                        print "  upload failed - skipping"
    
                if not song_id:
                    failed_files += 1
                    continue
    
                added = self.wc.add_songs_to_playlist(plid, song_id)
                time.sleep(.3) # Don't spam the server too fast...
                print "  done adding to playlist"
                added_files += 1
    
            if remove_missing:
                for s in goog_songs:
                    print "  removing: %s" % s['title']
                    self.wc.remove_songs_from_playlist(plid, s.id)
                    time.sleep(.3) # Don't spam the server too fast...
                    removed_files += 1
                
            print "%d songs unmodified" % existing_files
            print "%d songs added" % added_files
            print "%d songs failed" % failed_files
            print "%d songs removed" % removed_files
            print "Ended: %s" % filename
        else:
            print "  playlist %s didn't exist - skipping" % filename

    def get_files_from_playlist(self, filename):
        files = []
        f = codecs.open(filename, encoding='utf-8')
        for line in f:
            line = line.rstrip().replace(u'\ufeff',u'')
            if line == "" or line[0] == "#":
                continue
            path  = os.path.abspath(self.get_platform_path(line))
            if not os.path.exists(path):
                print "  file not found: %s" % line
                continue
            files.append(path)
        f.close()
开发者ID:bmjsmith,项目名称:m3uGoogleMusicSync,代码行数:70,代码来源:musicsync.py


注:本文中的gmusicapi.Webclient.add_songs_to_playlist方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。