Google Play Music Sync CLI with gmusicapi on Linux


1. 개요

-. NAS 있는 음악파일을 google music sync하기 위한 command line interface program 찾았으나
 
없어서 python script 작성함.

 

2. gmusicapi : https://github.com/simon-weber/gmusicapi

-. unofficial API for Google Play Music

-. 파일, playlist, radio 등에서  음악을 플레이하고 불필요한 파일을 지우기 위한 Mobileclient upload
 
등의 관리를 위한 Mobilemanager class 있음.

-. Mobileclient google play music 등록되어 있는 e-mail password 로그인하고,
   Mobilemanager
oauth 로그인한다.

-. Mobilemanager oauth 방법

  1. Musicmanager().perform_oauth(oauth_filepath) 실행
  2. 출력된 key 포함된 링크를 복사하여 웹브라우저로 접속함
  3. 인증과정을 거치면 google play music 에서 인증키를 출력함
  4. 출력된 인증키를 복사하여 실행 코드 창에 붙여넣고 enter 하면 인증완료

 

3. python code 작성

-. 디렉토리 sync 위해서

  1. google play music 파일 리스트를 가져오고 : Mobilemanager

def make_rmt_song_list(owner):

    # return list of dictionary which has 'title', 'id'

    result =[]


    mm = Musicmanager()

    mm.login(oauth_credentials=oauth_filepath(owner),  uploader_id=USERS[owner]['UPID'])


    for fn in mm.get_uploaded_songs():

        result.append({'title':fn['title'],'id':fn['id']})


    mm.logout()


    return result


  1. local 파일 리스트와 비교하여
  2. local 없는 파일은 google play music 에서 파일을 지움 : Mobileclient
    이때 지우는 것은 google play music  song id


def make_rmt_song_list(owner):

    result =[]

    mc = Mobileclient()

    mc.login(USERS[owner][‘e-mail'],USERS[owner]['pwd'], Mobileclient.FROM_MAC_ADDRESS)


    for song in delete_list:

        print song['title'] + " is being deleted now... "

        result.append({'title': song['title'], 'result' mc.delete_songs(song['id'])})


    mc.logout()


    return result


  1. google play music 없는 파일은 업로드함 : Mobilemanager

def __upload(upload_list, owner):

    result =[]

    mm = Musicmanager()

    mm.login(oauth_credentials=oauth_filepath(owner),  uploader_id=USERS[owner]['UPID'])


    for song in upload_list:

        print song + " is being uploaded now... "

        result.append({'title':song, 'result'mm.upload(song)})


    mm.logout()


    return result


  1. User정보는 dictionary 관리함

USERS = { u’id : {u’UPID':u'mac address', u’e-mail’:u'your email', u’pwd’:u'your password}}


4. Trouble Shooting

-. UPID Issue

: UPID 정보를 자신의 mac address 하기도 하고 비워두기도

: 계정이 여러 개인 경우 기기 하나를 모든 google play music 계정에 접속해서 기기 등록을 하고,
   UPID
등록한 기긱의 MAC adress 하였으나, 일부 계정은 인증이 안됨.

: 비워두는 경우, mac address 넣는 경우 등을 계정별로 시도해보아서 결정함.


    5. 스크립트 용법

    -. 필요한 module 설치

    : sudo pip install gmusicapi

    : sudo pip install argparse

    -. gmManager.py 파일 내에 configuration 부분에 사용자 정보를 추가함.

     :  DEST = u'인증파일을 저장할 디렉토리' #ex DEST = u'~/.config/gmusic/'

     : USERS = { u'사용자'  : {u'UPID':u'mac 주소', u’e-mail’:u’로그인용 이메일’, u’pwd’:u’암호(평문)'}}

       (예) DEST = u’~/.config/gmusic/‘

            USERS = {u’lisa’: {u’UPID’:u’00:00:00:00:00:00’, u’e-mail’:u’somewhere@on.mars’, u’pwd’:u’imsipasswd’}}

    -. google play music 에서 사용자 인증 키를 받음.

    : gmManager.py -o dummy_director 사용자  (ex) ./gmManager.py -o ./ lisa

    : 출력된 key 포함된 링크를 복사하여 웹브라우저로 접속하여 인증과정을 거침

    : google play music에서 인증키를 출력하면 복사하여 실행 코드 창에 붙여넣고 enter 하여 인증

    -. 명령어 실행

    : local directory google play music sync. google play music 파일 local 없으면 지움

     

    ./gmManager.py -s local_directory 사용자 (ex) ./gmManager.py -s ./Mp3s lisa

     

    : local directory 파일   google play music 없는  파일만 upload . google play music

      파일 local 없는 파일도 지우지 않음

     

    ./gmManager.py -u local_directory 사용자 (ex) ./gmManager.py -u ./Mp3s lisa

     

    : local directory 파일을 체그없이 무조건 google play music 올림. 중복된 파일의 경우

     google play music에서 에러

     

    ./gmManager.py local_directory 사용자 (ex) ./gmManager.py ./Mp3s lisa


    -. 스크립트 파일 : gmManager.py


    Posted by 옴팡진
    ,