import urllib2, re,json, urllib

def make_p(dir):
    if not os.path.isdir(dir):
        os.makedirs(dir,0777)
        return True
    else:
        return False
def makefileW(fpath, data):    
    text_file = open(fpath, "w")
    os.chmod(fpath, 0777)
    text_file.write(data)
    text_file.close()
  
def readfile(fpath):
    data = ''
    text_file = open(fpath, "r")
    data = text_file.read()
    text_file.close()
    return data

import sys, os
#data = readfile("fconfig.txt")
#names = re.findall('stream (.*?) {', data, re.DOTALL)
#urls =  re.findall('http://localhost/JD/(.*?)/playlist.m3u8', data, re.DOTALL)
#i=0
#for name in names:
#    dpath = name.replace("_","").replace("-","").replace(" ","").replace("/original/","") + urls[i]
#    print dpath
#    if True:#make_p(dpath):
        #makefileW(dpath+'/strStatus', "OFF")
        #makefileW(dpath+'/strData', dpath+"^#^geturlhttp://165.227.125.118/getJD/getjd.php?chtag="+urls[i]+"^#^Samsung GT-I9505 stagefright/Beyonce/1.1.9 (Linux;Android 5.0.1)^#^1^#^04^#^^#^^#^00:05")
        #makefileW(dpath+'/strAuth', "")
        #makefileW(dpath+'/strType', "False")
        #makefileW(dpath+'/strMsg', "Stream OFFLINE")
        #makefileW(dpath+'/toggle', "ON")
#        makefileW(dpath+'.py', readfile("hlsproxy.py"))
        #make_p(dpath+'/sessions')
#    else:
#        print "Already exist" 
#    i=i+1
    #break


##data = readfile("fconfig.txt")
##names = re.findall('stream (.*?) {', data, re.DOTALL)
##urls =  re.findall('url http://199.66.90.21:8888/jd(.*?).m3u8', data, re.DOTALL)
##i=0
##for name in names:
##    dpath = name.replace("_","").replace("-","").replace(" ","") + urls[i]
##    print 'stream '+name+' {'+'\n'+'  url http://127.0.0.1:8888/hls/'+dpath+'/output.m3u8 user_agent="Samsung GT-I9505 stagefright/Beyonce/1.1.9 (Linux;Android 5.0.1)";'+'\n'+'}'
##    i=i+1
    

datas = readfile("jio").splitlines()

i=0
for url in datas:
    dpath = url
    if make_p(url):
        makefileW(dpath+'/strStatus', "OFF")
        makefileW(dpath+'/strData', url+"^#^"+url+"_1200^#^Samsung GT-I9505 stagefright/Beyonce/1.1.9 (Linux/index.m3u8;Android 5.0.1)^#^2^#^None^#^^#^^#^00:05")
        makefileW(dpath+'/strAuth', "")
        makefileW(dpath+'/strType', "False")
        makefileW(dpath+'/strMsg', "Stream OFFLINE")
        makefileW(dpath+'/toggle', "ON")
        makefileW(dpath+'.py', readfile("hlsproxy.py"))
        make_p(dpath+'/sessions')
        print url
    else:
        makefileW(dpath+'.py', readfile("hlsproxy.py"))
        print "Already exist" 
    i=i+1
    






##data = readfile("fconfig.txt")
##names = re.findall('stream (.*?) {', data, re.DOTALL)
##urls =  re.findall('url (.*?).m3u8', data, re.DOTALL)
##i=0
##for url in urls:
##    if "hls://198.144.158.11/jd" in url:
##        dpath = names[i].replace("_","").replace("-","").replace(" ","") + url.replace("hls://198.144.158.11/jd","")
##        print 'stream '+names[i]+' {'+'\n'+'  url http://127.0.0.1/hls/'+dpath+'/output.m3u8 user_agent="Samsung GT-I9505 stagefright/Beyonce/1.1.9 (Linux;Android 5.0.1)";'+'\n'+'}'
##    else:
##        print 'stream '+names[i]+' {'+'\n'+'  url '+url+'.m3u8 user_agent="Samsung GT-I9505 stagefright/Beyonce/1.1.9 (Linux;Android 5.0.1)";'+'\n'+'}'    
##    i=i+1

#for sdir in os.listdir('/var/www/html/hls/'):
#    try:
#        makefileW(sdir+'/strData', readfile(sdir+'/strData').replace('185.246.209.104','127.0.0.1'))
#        print sdir
#    except:
#        print "error" + sdir
    