Ejemplo n.º 1
0
def download(directory=".",
             oauth=os.environ['HOME'] + "/oauth",
             device_id=__DEFAULT_MAC__):
    logging.basicConfig(level=logging.INFO)
    logger = logging.getLogger(__name__)
    logger.info("Init Daemon - Press Ctrl+C to quit")
    api = Musicmanager()
    if not api.login(oauth, device_id):
        print("Error with oauth credentials")
        sys.exit(1)

    for song in api.get_uploaded_songs():
        folder_path = os.path.join(directory, song['album_artist'],
                                   song['album'])
        file_path = os.path.join(
            folder_path, '%d - %s.mp3' %
            (song['track_number'], song['title'].replace('/', '_')))
        file_path = file_path.encode('utf8')
        folder_path = folder_path.encode('utf8')
        if not os.path.exists(file_path):
            filename, audio = api.download_song(song['id'])
            if not os.path.exists(folder_path):
                os.makedirs(folder_path)
            with open(file_path, 'wb') as f:
                f.write(audio)
Ejemplo n.º 2
0
def index(request):
    args = {}
    if request.user.is_authenticated:
        credential = request.user.profile.google_oauth
        if not credential:
            return HttpResponse('No credentials found. Remake an account.')
        manager = Musicmanager()
        # TODO: Maybe change mac address for each user?
        login_success = manager.login(
            credential,
            uploader_name="GMusicManagerOnline - {}".format(
                request.user.username))
        args.update({'login_success': login_success})
        if login_success:
            quota = manager.get_quota()
            default_fields_form = DefaultFieldsForm(
                request.POST or None, instance=request.user.profile)
            if default_fields_form.is_valid():
                default_fields_form.save()
                return redirect('index')
            args.update({
                'currently_uploaded': quota[0],
                'upload_limit': quota[1],
                'default_fields_form': default_fields_form
            })
        manager.logout()
    return render(request, 'core/index.html', args)
Ejemplo n.º 3
0
 def __init__(self):
     self.api = Musicmanager(debug_logging=False)
     with open(path + "oauth.cred", 'w+') as tmp:
         tmp.write(settings['google']['musicmanager'])
         tmp.close()
         self.api.login(tmp.name)
         os.remove(tmp.name)
Ejemplo n.º 4
0
def _process_youtube_upload(youtube_url, ydl_opts, music, metadata_opts,
                            credential, uploader_name):
    manager = Musicmanager()
    manager.login(credential, uploader_name=uploader_name)
    with youtube_dl.YoutubeDL(ydl_opts) as ydl:
        info_dict = ydl.extract_info(youtube_url, download=False)
        if music.title == "":
            music.title = info_dict.get('title', None)
            music.save()
            metadata_opts = {
                i: getattr(music, i)
                for i in [
                    'title', 'album', 'composer', 'genre', 'language',
                    'artist', 'album_artist'
                ]
            }
            logger.debug('New metadata opts: ' + str(metadata_opts))
            ffmpeg_mp3_metadata_pp = FFmpegMP3MetadataPP(ydl, metadata_opts)
            ydl.add_post_processor(ffmpeg_mp3_metadata_pp)
            ydl.download([youtube_url])
            music_filepath = ydl.prepare_filename(info_dict)
            if not music_filepath.endswith('.mp3'):
                music_filepath = music_filepath.replace(
                    music_filepath[music_filepath.rfind('.'):], '.mp3')
                logger.debug("Music filepath: " + music_filepath)
    manager.upload(music_filepath,
                   enable_matching=True,
                   enable_transcoding=False)  # Already transcoding.
    if os.path.isfile(music_filepath):
        os.remove(music_filepath)
def upload(directory='.',
           oauth=os.environ['HOME'] + '/oauth',
           remove=False,
           uploader_id=__DEFAULT_MAC__):
    logging.basicConfig(level=logging.INFO)
    logger = logging.getLogger(__name__)
    logger.info("Init Daemon - Press Ctrl+C to quit")

    api = Musicmanager()
    event_handler = MusicToUpload()
    event_handler.api = api
    event_handler.path = directory
    event_handler.willDelete = remove
    event_handler.logger = logger
    if not api.login(oauth, uploader_id):
        print("Error with oauth credentials")
        sys.exit(1)
    files = [file for file in glob.glob(directory + '/**/*', recursive=True)]
    for file_path in files:
        if os.path.isfile(file_path):
            logger.info("Uploading : " + file_path)
            uploaded, matched, not_uploaded = api.upload(file_path, True)
            if remove and (uploaded or matched):
                os.remove(file_path)
    observer = Observer()
    observer.schedule(event_handler, directory, recursive=True)
    observer.start()
    try:
        while True:
            time.sleep(1)
    except KeyboardInterrupt:
        observer.stop()
    observer.join()
Ejemplo n.º 6
0
 def __init__(self, device_id: str, cred_mc: oauth2client.client.OAuth2Credentials, cred_mm: oauth2client.client.OAuth2Credentials):
     self._mc = Mobileclient()
     if not self._mc.oauth_login(device_id, cred_mc):
         raise RuntimeError('Mobileclient login failed')
     self._mm = Musicmanager()
     if not self._mm.login(cred_mm, _UPLOADER_ID, _UPLOADER_NAME):
         raise RuntimeError('Musicmanager login failed')
Ejemplo n.º 7
0
 def __init__(self):
     self.api = Musicmanager(debug_logging=False)
     refresh_token = json.loads(
         settings['google']['musicmanager'])['refresh_token']
     credentials = session.credentials_from_refresh_token(
         refresh_token, session.Mobileclient.oauth)
     self.api.login(credentials)
Ejemplo n.º 8
0
def machineautn():

    mm = Mobileclient()
    mm.perform_oauth()

    mm2 = Musicmanager()
    mm2.perform_oauth()
Ejemplo n.º 9
0
def main():
    args, config = init()

    if not os.path.isdir(config['PROG']['DownloadPath']):
        os.mkdir(config['PROG']['DownloadPath'])

    sc_download(
        config['SOUNDCLOUD']['PlaylistUrl'],
        config['PROG']['DownloadPath']
    )

    mc = Mobileclient()
    mc.login(config['GMUSIC']['User'], config['GMUSIC']['Password'], Mobileclient.FROM_MAC_ADDRESS)

    mm = Musicmanager()
    if not (os.path.exists(gmclients.OAUTH_FILEPATH) and mm.login(gmclients.OAUTH_FILEPATH)):
        mm.perform_oauth(gmclients.OAUTH_FILEPATH, open_browser=True)
        if not mm.login(gmclients.OAUTH_FILEPATH):
            sys.stderr.write('Musicmanager could not authenticate')

    if config['GMUSIC']['TargetPlaylist'] not in set([item['name'] for item in mc.get_all_playlists() if not item['deleted']]):
        mc.create_playlist(name=config['GMUSIC']['TargetPlaylist'], description='Tracks synchronized using {}'.format(__prog__), public=False)

    playlist_id, current_members = gm_get_current_pl_member(mc, config['GMUSIC']['TargetPlaylist'])

    for track in os.listdir(config['PROG']['DownloadPath']):
        print('Uploading {}'.format(track))
        uploaded_id = gm_extract_id(mm.upload('{}{}'.format(config['PROG']['DownloadPath'], track)))
        mc.add_songs_to_playlist(playlist_id, uploaded_id)
Ejemplo n.º 10
0
    def __init__(self):
        # Aplying patch to session.Musicmanager
        session.Musicmanager.login = MethodType(patched_musicmanaer_login,
                                                None, session.Musicmanager)
        self.man = Musicmanager(verify_ssl=False)

        self.all_songs = None
Ejemplo n.º 11
0
def download(mode, url, num):
    print(f"Downloading for {mode}")

    #Options
    ydl_opts = {
        'format':
        'bestaudio/best',
        'postprocessors': [{
            'key': 'FFmpegExtractAudio',
            'preferredcodec': 'mp3',
            'preferredquality': '320',
        }],
        'logger':
        MyLogger(),
        'progress_hooks': [my_hook],
        'outtmpl':
        folders[num][mode][0] + '/%(title)s.%(ext)s'
    }

    #Download the video and extract all the metadata
    with youtube_dl.YoutubeDL(ydl_opts) as ydl:
        info_dict = ydl.extract_info(url, download=True)
        video_title = info_dict.get('title', None)
        video_filename = '.'.join(
            ydl.prepare_filename(info_dict).split('.')[:-1]) + '.mp3'
    print(video_filename)

    #Edit mp3 tag.
    try:
        print(f"Editing artist tag to {mode.capitalize()}...")
        mp3 = MP3File(video_filename)
        mp3.artist = mode.title()
        mp3.save()
        insert_album_art(video_filename, info_dict['id'])
        print("Done!\n")
    except Exception as e:
        print("Error at editing mp3 tag.\n")
        print(e)

    #Backup
    if num != 3:
        try:
            print(f"Making a backup of {video_title}...")
            copy(video_filename, folders[num][mode][1])
            print("Done!\n")
        except:
            print("Error at doing backup.\n")

    #Upload to google
    if num != 3:
        try:
            print(f"Uploading {video_title} to Google Music\n")
            print(f'With url {url}')
            mm = Musicmanager()
            mm.login(uploader_id='D0:50:99:83:B0:0C')
            mm.upload(video_filename)
            print("Done!\n")
        except Exception as e:
            print("Error at uploading the song to google:\n" + e)
Ejemplo n.º 12
0
def initializeClient():
    client = Musicmanager()

    if (client.login()):
        return client
    else:
        client.perform_oauth(open_browser=True)
        initializeClient()
Ejemplo n.º 13
0
def init():
    mm = Musicmanager()
    try:
        script_path = os.path.dirname(os.path.abspath(__file__))
    except NameError:
        script_path = os.getcwd()
    mm.login(oauth_credentials=os.path.join(script_path, "oauth.cred"))
    return mm
Ejemplo n.º 14
0
    def authenticatewithgoogle(self, option):

        self.mm = Musicmanager()
        self.mm.login()  # currently named oauth_login for the Mobileclient

        if option == 0:
            self.library = self.mm.get_purchased_songs()
        elif option == 1:
            self.library = self.mm.get_uploaded_songs()
Ejemplo n.º 15
0
 def __init__(self):
     self.api = Musicmanager()
     try:
         print(os.getcwd())
         if not self.api.login(oauth_credentials=config.auth):
             self.api.perform_oauth(storage_filepath=config.auth, open_browser=False)
             self.api.login(oauth_credentials=config.auth)
     except:
         pass
Ejemplo n.º 16
0
 def __init__(self):
     super(Gmusic, self).__init__()
     # Checks for OAuth2 credentials,
     # if they don't exist - performs authorization
     self.m = Musicmanager()
     if os.path.isfile(gmusicapi.clients.OAUTH_FILEPATH):
         self.m.login()
     else:
         self.m.perform_oauth()
Ejemplo n.º 17
0
def download(base_dir: str = ".",
             creds: str = __DEFAULT_CREDS__,
             device_id: str = __DEFAULT_MAC__) -> None:
    api = Musicmanager()
    if not api.login(creds, device_id):
        logger.error("Error with oauth credentials")
        sys.exit(1)

    Song = namedtuple('Song', ['artist', 'album', 'track', 'title', 'id'])

    def _download(song: Song, downloader: Callable) -> None:
        logger.debug(f"Downloading song '{song.title}'")
        f, audio = downloader(song.id)

        folder = os.path.join(base_dir, song.artist, song.album)
        if not os.path.exists(folder):
            logger.debug(f"Creating folder '{folder}'")
            os.makedirs(folder)
        file = os.path.join(folder, f'{song.track}-{song.title}.mp3')
        with open(file, 'wb') as f:
            logger.debug(f"Writing file '{file}'")
            f.write(audio)

    songs = api.get_uploaded_songs()
    n = len(songs)
    logger.debug(f"Downloading '{n}' to folder '{base_dir}'")
    future_to_song = {}
    with ThreadPoolExecutor() as executor:
        for song in songs:
            artist = song['album_artist']
            album = song['album']
            track = song['track_number']
            title = song['title'].replace('/', '_').replace('?', '_')
            iden = song['id']
            s = Song(artist=artist,
                     album=album,
                     track=track,
                     title=title,
                     id=iden)
            future = executor.submit(_download,
                                     song=s,
                                     downloader=api.download_song)
            future_to_song[future] = s

        succeeded = 0
        failed = 0
        for future in concurrent.futures.as_completed(future_to_song):
            s = future_to_song[future]
            if future.exception():
                logger.warning(
                    f"Failed to download song '{s.title}' because '{future.exception()}'"
                )
                failed += 1
                continue
            succeeded += 1
        logger.debug(
            f"Completed. Total {n} succeded {succeeded} failed {failed}")
Ejemplo n.º 18
0
def youtube_upload(request):
    # TODO: Async status updates?
    if not request.user.is_authenticated:
        return HttpResponse('log in first')
    credential = request.user.profile.google_oauth
    if not credential:
        return HttpResponse('no creds')
    manager = Musicmanager()
    # TODO: Maybe change mac address for each user?
    login_success = manager.login(
        credential,
        uploader_name="GMusicManagerOnline - {}".format(request.user.username))
    form = YoutubeUploadForm()
    args = {'can_login': login_success, 'form': form, 'success': False}
    if request.method == "POST":
        form = YoutubeUploadForm(request.POST)
        if form.is_valid():
            music = form.save()
            for i in [
                    'title', 'album', 'composer', 'genre', 'language',
                    'artist', 'album_artist'
            ]:
                if getattr(music, i) == "":
                    setattr(music, i,
                            getattr(request.user.profile, "default_" + i))
            music.save()
            youtube_url = request.POST.get('youtube_url')
            metadata_opts = {
                i: getattr(music, i)
                for i in [
                    'title', 'album', 'composer', 'genre', 'language',
                    'artist', 'album_artist'
                ] if getattr(music, i) != ""
            }
            ydl_opts = {
                'format':
                'bestaudio/best',
                'outtmpl':
                settings.MEDIA_ROOT + "/{}{}.%(ext)s".format(
                    request.user.username, int(time.time())),
                'postprocessors': [{
                    'key': 'FFmpegExtractAudio',
                    'preferredcodec': 'mp3',
                    'preferredquality': '320',
                }],
            }
            upload_args = (youtube_url, ydl_opts, music, metadata_opts,
                           credential, "GMusicManagerOnline - {}".format(
                               request.user.username))
            upload_thread = threading.Thread(target=_process_youtube_upload,
                                             args=upload_args)
            upload_thread.start()
            args.update({'success': True})
        args.update({'form': form})
    manager.logout()
    return render(request, 'core/youtube.html', args)
Ejemplo n.º 19
0
def upload(path):
    mm = Musicmanager()
    mm.login()

    for root, dirs, files in os.walk(path):
        for file_ in files:
            mp3FilePath = os.path.join(root, file_)
            print( "upload : " + file_ )
            mm.upload(mp3FilePath)
            os.remove(mp3FilePath)
Ejemplo n.º 20
0
    def __init__(self, config):
        self.credential_file = ''

        if len(config.googleplay_credential_file) > 0:
            self.credential_file = config.googleplay_credential_file
        else:
            self.credential_file = clients.OAUTH_FILEPATH

        self.mac_address = config.mac_address_for_gplay
        self.manager = Musicmanager(False)
        self.logger = logging.getLogger(__name__)
def download(directory: str = ".",
             oauth: str = __DEFAULT_OAUTH_PATH__,
             device_id: str = __DEFAULT_MAC__,
             down_logger: logging.Logger = logger) -> None:
    api = Musicmanager()
    if not api.login(oauth, device_id):
        if down_logger:
            down_logger.error("Error with oauth credentials")
        sys.exit(1)

    if down_logger:
        down_logger.info("Init Daemon - Press Ctrl+C to quit")

    songs = api.get_uploaded_songs()
    songs_total = len(songs)
    if down_logger:
        logger.debug("Downloading '%d' to folder '%s'" %
                     (songs_total, directory))
    future_to_song = {}
    with ThreadPoolExecutor() as executor:
        for song in songs:
            artist = song['album_artist']
            album = song['album']
            track_number = song['track_number']
            title = song['title'].replace('/', '_').replace('?', '_')
            track_id = song['id']
            song_object = Song(artist=artist,
                               album=album,
                               track_number=track_number,
                               title=title,
                               id=track_id)
            future = executor.submit(_download,
                                     song=song_object,
                                     api=api,
                                     base_dir=directory,
                                     thread_logger=down_logger)
            future_to_song[future] = song

        succeeded = 0
        failed = 0
        for future in concurrent.futures.as_completed(future_to_song):
            song = future_to_song[future]
            if future.exception():
                if down_logger:
                    down_logger.warning(
                        "Failed to download song '%s' because '%s'" %
                        (song.title, future.exception()))
                failed += 1
                continue
            succeeded += 1
        if down_logger:
            down_logger.debug(
                "Completed. Total %d | %d succeeded | %d failed" %
                (songs_total, succeeded, failed))
Ejemplo n.º 22
0
 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]
Ejemplo n.º 23
0
 def __init__(self):
     """
     Log into Musicmanager and get the library, either by loading an
       existing library file, or by generating a new one.
     """
     self.kind = 'free'
     self.mm = Musicmanager()
     self.mm.login()
     self.songs = []
     self.load_library()
     if not self.songs:
         self.gen_library()
Ejemplo n.º 24
0
    def __init__(self):
        super(Gmusic, self).__init__()
        # Checks for OAuth2 credentials,
        # if they don't exist - performs authorization
        self.m = Musicmanager()
        if os.path.isfile(gmusicapi.clients.OAUTH_FILEPATH):
            self.m.login()
        else:
            self.m.perform_oauth()

        self.config.add({
            u'auto': False,
        })
        if self.config['auto']:
            self.import_stages = [self.autoupload]
Ejemplo n.º 25
0
    def __init__(self, creds_dir=DEFAULT_CREDS_DIR):
        self.creds_dir = os.path.expanduser(creds_dir)
        logger.info("Creating TheGoogs from creds at {}".format(
            self.creds_dir))
        self.mobile_creds = os.path.join(self.creds_dir, "mobile.creds")
        self.manager_creds = os.path.join(self.creds_dir, "manager.creds")

        self.mobile = Mobileclient()
        self.manager = Musicmanager()

        logger.debug("Logging in")
        self.mobile.oauth_login(device_id=self.DEFAULT_MOBILE_DEVICE_ID,
                                oauth_credentials=self.mobile_creds)
        self.manager.login(uploader_id=self.DEFAULT_MANAGER_MAC_ADDRESS,
                           oauth_credentials=self.manager_creds)
Ejemplo n.º 26
0
def main():
    cli = Musicmanager(debug_logging=False)

    if credfile.is_file():
        if cli.login(oauth_credentials=str(credfile)):
            print("Login successful. Don't need to perform oauth.")
            return
        else:
            os.remove(credfile)

    cli.perform_oauth(storage_filepath=str(credfile), open_browser=True)

    if cli.login(oauth_credentials=str(credfile)):
        print("Login successful. Restart the bot!")
    else:
        print("Failed to auth. Try again later.")
Ejemplo n.º 27
0
def authorize_manager(parameters):
    # api = Webclient()
    # api = Mobileclient()
    api = Musicmanager()
    # after running api.perform_oauth() once:
    # api.oauth_login('<a previously-registered device id>')
    # api.login(email=parameters.username, password=parameters.password)
    # => True
    if not os.path.exists(authorize_file + '_manager'):
        api.perform_oauth(authorize_file + '_manager', True)

    api.login(authorize_file + '_manager')
    if not api.is_authenticated():
        return None

    return api
Ejemplo n.º 28
0
def upload(request):
    # TODO: Async status updates?
    if not request.user.is_authenticated:
        return HttpResponse('log in first')
    credential = request.user.profile.google_oauth
    if not credential:
        return HttpResponse('no creds')
    manager = Musicmanager()
    # TODO: Maybe change mac address for each user?
    login_success = manager.login(
        credential,
        uploader_name="GMusicManagerOnline - {}".format(request.user.username))
    form = MusicUploadForm()
    args = {'can_login': login_success, 'form': form, 'success': False}
    if request.method == "POST":
        form = MusicUploadForm(request.POST, request.FILES)
        if form.is_valid():
            music = form.save()
            music_file = request.FILES.get('music_file')
            ext = music_file.name[music_file.name.rfind('.'):]
            fs = FileSystemStorage()
            filename = fs.save("{0}{1}".format(request.user.username, ext),
                               music_file)
            music_filepath = fs.path(filename)
            post_filepath = music_filepath + ".mp3"
            options = {
                i: getattr(music, i)
                for i in [
                    'title', 'album', 'composer', 'genre', 'language',
                    'artist', 'album_artist'
                ] if getattr(music, i) != ""
            }
            logger.info("Transcoding metadata: " + str(options))
            options.update({'quality': '320k'})
            _transcode(music_filepath, options, post_filepath)
            if os.path.isfile(music_filepath):
                os.remove(music_filepath)
            success, _, _ = manager.upload(  # Already transcoding.
                post_filepath,
                enable_matching=True,
                enable_transcoding=False)
            if os.path.isfile(post_filepath):
                os.remove(post_filepath)
            args.update({'success': True})
        args.update({'form': form})
    manager.logout()
    return render(request, 'core/upload.html', args)
Ejemplo n.º 29
0
    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
    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 ""