#/usr/bin/python
import os
import json
import time
import sys
from requests import session
import re
"""
POST /cgi-bin/login.cgi HTTP/1.1
Host: 192.168.178.145:8000
Connection: keep-alive
Content-Length: 39
Cache-Control: max-age=0
Upgrade-Insecure-Requests: 1
Origin: http://192.168.178.145:8000
Content-Type: application/x-www-form-urlencoded
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.122 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9
Referer: http://192.168.178.145:8000/cgi-bin/index.cgi?cmd=logout&v=4.0.12
Accept-Encoding: gzip, deflate
Accept-Language: de-DE,de;q=0.9,en-US;q=0.8,en;q=0.7
"""

with session() as c:
    payload = {
    'cmd''login',
    'username'"admin",
    'password'"admin"
    }
    c.post('http://192.168.178.145:8000/cgi-bin/login.cgi'data=payload)
    response c.get('http://192.168.178.145:8000/cgi-bin/index.cgi?cmd=static_configuration_page&v=4.0.12')

    """
    cmd=create_static_conf&fe_num=1&rec_std=dvbs&diceqc=1&freq=11836&pol=h&sym_rate=27500&fec=34&mod_type=qpsk&pilot_tones=none&roll_off=none&pids=0%2C100%2C101%2C102%2C104&service_name=Das+Erste&lcn=
    
    view with:
     vlc rtp://239.1.0.0:1500

    """
    print(response.headers)
    print(response.text)
    # cmd=create_static_conf&fe_num=1&rec_std=dvbs&diceqc=1&freq=12187&pol=h&sym_rate=27500&fec=34&mod_type=qpsk&pilot_tones=none&roll_off=none&pids=0%2C47%2C71%2C136%2C167&service_name=VOX&lcn=
    if sys.argv[1] == "config":
        fo=open("config","w" )
        for in range (208):
            payload= { "cmd""service_properties",
                "sat""sat 0",
                "ser""service "str(a) }
            response c.post('http://192.168.178.145:8000/cgi-bin/index.cgi'data=payload)

        #response = c.get('http://192.168.178.145:8000/cgi-bin/index.cgi?cmd=create_static_conf&fe_num=1&rec_std=dvbs&diceqc=1&freq=11836&pol=h&sym_rate=27500&fec=34&mod_type=qpsk&pilot_tones=none&roll_off=none&pids=0%2C100%2C101%2C102%2C104&service_name=Das+Erste&lcn=')
            fo.write(response.text "\n")
            print(response.headers)
            print(response.text)
    if sys.argv[1] == "set":
        import re
        with open("config","r"as origin_file:
            for line in origin_file:
                linep re.findall("\"" sys.argv[2] + "\"" line)
                if linep:
                #     line = line[0].split('"')[1]
                    printline )
                    infojson.loads(line)
                    # print(info["freq"])
                    # {"channel_name":"ZDF","msys":"dvbs","freq":"11954","pol":"h","sr":"27500","fec":"34","mtype":"qpsk","pilot":"none","ro":"none","pids":"0,100,110,120,130"}
                    c.get('http://192.168.178.145:8000/cgi-bin/index.cgi?cmd=terminate_static_conf&fe_num=1')
                    time.sleep(1)
                    rest'&fe_num=1&rec_std=' info["msys"] +  '&diceqc=1&freq=' info["freq"] + '&pol=' info["pol"] + '&sym_rate=' info["sr"] + '&fec=' info["fec"] + '&mod_type=' info["mtype"] + '&pilot_tones=none&roll_off=none&pids=' info["pids"].replace(" ","%2C") + '&service_name=' info["channel_name"].replace(" ","+") +'&lcn='
                    # print(rest)
                    response c.get('http://192.168.178.145:8000/cgi-bin/index.cgi?cmd=create_static_conf' rest)
                    os.system("vlc rtp://239.1.0.0:1500&")
                   
    if sys.argv[1] == "end":
        c.get('http://192.168.178.145:8000/cgi-bin/index.cgi?cmd=terminate_static_conf&fe_num=1')
# print("vlc rtp://239.1.0.0:1500")