import rpyc
import os
import random
import string
import requests
from bs4 import BeautifulSoup
import time


def randomString(stringLength=10):
    letters = string.ascii_lowercase
    return ''.join(random.choice(letters) for i in range(stringLength))


def mp3_to_anchor(fname):

    conn = rpyc.classic.connect("192.168.192.221",18812)
    fr=open(fname,'rb')
    ropen = conn.builtins.open
    randname=randomString(10)
    finalname=randomString(10)

    fw=ropen('/tmp/'+randname+'.mp3','wb')
    fw.write(fr.read())
    fw.close()
    ros = conn.modules.os
    ros.system('/root/to_video/p9.sh '+randname+".mp3 "+finalname+".mp4")
    return 'http://192.168.192.221/video/'+finalname+'.mp4'
#    conn.execute('import os')


def download_mp4(url):
    headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36'}
    with open('ai_spokesgirl.mp4','wb') as f:
        r = requests.get(url, headers=headers, stream=True)
        if r.status_code == 404:
            return False
        for chunk in r.iter_content(chunk_size=1024):
            if chunk:
                f.write(chunk)
    return True


if __name__ == '__main__':
    fname=mp3_to_anchor('/Users/zooeytsai/kw_tools/openshot/input/movie_main/Q8.mp3')
    print(fname)
    time.sleep(240)
    while True:
        result = download_mp4(fname)
        if result:
            break
        print('等待...')
        time.sleep(60)

    

