
import threading, subprocess
import os, sys, urllib, urllib2, re
import json
import requests



s = requests.Session()

def getApi(url):  
    s = requests.Session()
    proxies = {"http": "http://azzwrktm-1:iiandhpizjdh@p.webshare.io:80",
              "https": "http://azzwrktm-1:iiandhpizjdh@p.webshare.io:80",
              }
    s.headers.update({'Accept': '*/*',
'Accept-Encoding': 'gzip, deflate, br',
'Accept-Language': 'eng',
'Connection': 'keep-alive',
'hotstarauth': 'st=1600183406~exp=1600189406~acl=/*~hmac=f9cbbae1065bb08162a6cfa79c7925231cbc5a85ae36bda762f672391198c47e',
'Origin': 'https://www.hotstar.com',
'Referer': 'https://www.hotstar.com/in/tv/yeh-rishta/586',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:80.0) Gecko/20100101 Firefox/80.0',
'x-client-code': 'LR',
'x-country-code': 'IN',
'x-platform-code': 'PCTV',
'x-region-code': 'ON'})
    r = s.get(url, proxies=proxies)
    resp = r.text
    #print resp
    return resp

def gettoken():
    return 'st=1600181047~exp=1600187047~acl=/*~hmac=e2e30c66acf2e974fba3083e0a8be1bb33f213c9db74d0d3c91cfbc5823f634f'
    

def retriveVoD1(url, fname): 
    
    global proc  
    cheadertag = "-headers"
    cheader = "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:80.0) Gecko/20100101 Firefox/80.0\r\nAccept: */*\r\nAccept-Language: en-US,en;q=0.5\r\nAccept-Encoding: gzip, deflate, br\r\nOrigin: https://www.hotstar.com\r\nReferer: https://www.hotstar.com/\r\nConnection: keep-alive\r\nx-country-code:IN\r\nx-platform-code:PCTV\r\nx-region-code:undefined\r\nx-client-code:LR"  
    #cmd = 'ffmpeg -y -user_agent "" -i '+vUrl+' -c copy '+vodData+'/'+fName+'.mp4'   
    cmd = 'ffmpeg -y -user_agent "" -i '+url+' -c copy -bsf:a aac_adtstoasc '+fname+'.mp4 -v trace' 
    cmd = cmd.split()
    cmd[2]= cheadertag
    cmd[3]= cheader
    proc = subprocess.Popen(cmd,shell=False)
    proc.wait() 
    
    

#html = json.loads(getApi("https://api.hotstar.com/o/v1/tray/g/1/items?eid=43&etid=0&tao=0&tas=2"))['body']['results']
#print html
retriveVoD1("https://hses4.hotstar.com/videos/hotstarint/1260000603/1100041429/1600108827540/1b43d12d79357282d35790a95f58d60e/master-tv.m3u8?hdnea=st=1600268631~exp=1600269231~acl=/videos/hotstarint/1260000603/1100041429/1600108827540/1b43d12d79357282d35790a95f58d60e/*~data=ip=070287a78f2a3e2b1d5db95a122d4e516d6cc507873c01395940a8bab0-userid=076b35386e575ff7e016744fd2a3ff09c9f8cc0f25ce1ec224a938b792837bdc~hmac=9fa81222ffadbe2a059ccda35382ba92f2df089e5de615e33491d9cf52dd9251", "test")




