Advertisement
Guest User

Untitled

a guest
Nov 4th, 2019
345
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.25 KB | None | 0 0
  1. import argparse
  2. import uuid
  3. import requests
  4. import json
  5. import os
  6. import sys
  7. import subprocess
  8. import time
  9. import multiprocessing as mp
  10.  
  11.  
  12. def construct_outfile(source):
  13.     return "".join(source.split(".")[:-1]) + "-{side}-out.ogg"
  14.  
  15.  
  16. def make_call(in_file, out_a_file, out_b_file, key):
  17.     n = 4
  18.     m = 5
  19.     retry = True
  20.     i = 0
  21.     while retry:
  22.         call_id = uuid.uuid4()
  23.         try:
  24.             r = requests.get("https://api.contest.com/voip{auth_token}/getConnection".format(auth_token=key),
  25.                              params={"call": call_id}, timeout=1)
  26.         except:
  27.             continue
  28.         time.sleep(3)
  29.  
  30.         config = r.json()
  31.         if not config["ok"]:
  32.             i += 1
  33.             continue
  34.         conf_file = "config-{}.json".format(call_id)
  35.         with open(conf_file, "w") as f1:
  36.             f1.write(json.dumps(config["result"]["config"]))
  37.         res = {"returncode": 0}
  38.         k = 0
  39.         while retry and k < m:
  40.             print("!!!!!!!!!!!!!!!!! 1-st call !!!!!!!!!!!!!!!!!")
  41.             os.system(
  42.                 "./tgvoipcall {ip}:{port} {caller_tag} -k {key} -i {in_file} -o {out_b_file} -c {config_file} -r caller &".
  43.                 format(
  44.                     ip=config["result"]["endpoints"][0]["ip"],
  45.                     port=config["result"]["endpoints"][0]["port"],
  46.                     caller_tag=config["result"]["endpoints"][0]["peer_tags"]["caller"],
  47.                     key=config["result"]["encryption_key"],
  48.                     in_file=in_file,
  49.                     out_b_file=out_b_file,
  50.                     config_file=conf_file
  51.                 ))
  52.             time.sleep(0.5)
  53.             print("!!!!!!!!!!!!!!!!! 2-nd call !!!!!!!!!!!!!!!!!")
  54.             res = subprocess.run(
  55.                 "./tgvoipcall {ip}:{port} {callee_tag} -k {key} -i {in_file} -o {out_a_file} -c {config_file} -r callee".
  56.                 format(
  57.                     ip=config["result"]["endpoints"][0]["ip"],
  58.                     port=config["result"]["endpoints"][0]["port"],
  59.                     callee_tag=config["result"]["endpoints"][0]["peer_tags"]["callee"],
  60.                     key=config["result"]["encryption_key"],
  61.                     in_file=in_file,
  62.                     out_a_file=out_a_file,
  63.                     config_file=conf_file
  64.                 ).split())
  65.             k += 1
  66.             retry = (res.returncode != 0)
  67.         i += 1
  68.         if i > n:
  69.             retry = False
  70.             print("!!!!!!!!!!!!!!!!! Faild after {} retry !!!!!!!!!!!!!!!!!".format(n))
  71.         if retry:
  72.             print("!!!!!!!!!!!!!!!!! RETRY {} !!!!!!!!!!!!!!!!!".format(i+k))
  73.     return call_id, config
  74.  
  75.  
  76. if __name__ == "__main__":
  77.     parser = argparse.ArgumentParser(description="Run all TG Contest programs with all samples and check call results")
  78.     parser.add_argument("-k", "--api-key", help="TG contest API key", required=True)
  79.     parser.add_argument("-c", "--call-id", help="TG session call id")
  80.     parser.add_argument("-S", "--samples-dir", help="TG samples directory, defaults to .")
  81.     parser.add_argument("-O", "--output-dir", help="Directory for saving output files, directory will be created," +
  82.                                                    " defaults to SAMPLES_DIR")
  83.     parser.add_argument("-s", "--sample", help="Sample file name to play in call, must be used in conjunction with -o")
  84.     parser.add_argument("-o", "--output", help="Sample output file mask, must be used in conjunction with -s")
  85.  
  86.     args = parser.parse_args()
  87.  
  88.     source = False
  89.     source_dir = False
  90.     output_dir = ""
  91.     if args.samples_dir:
  92.         source_dir = args.samples_dir
  93.         if args.output_dir:
  94.             output_dir = args.output_dir
  95.             try:
  96.                 os.mkdir(output_dir)
  97.             except FileExistsError:
  98.                 pass
  99.         else:
  100.             output_dir = source_dir
  101.     elif args.sample:
  102.         source = args.sample
  103.         if args.output:
  104.             output = args.output
  105.         else:
  106.             output = construct_outfile(source)
  107.     if not source and not source_dir:
  108.         source_dir = "."
  109.  
  110.     key = args.api_key
  111.  
  112.     if not source_dir:
  113.         # noinspection PyUnboundLocalVariable
  114.         call_id, config = make_call(source, output.format(side="a"), output.format(side="b"), key)
  115.         sys.exit(0)
  116.  
  117.     mp.set_start_method("fork")
  118.     procs = []
  119.  
  120.     def mp_target(source, output, key, f):
  121.         call_id, config = make_call(source, output.format(side="a"), output.format(side="b"), key)
  122.         print("=!!============= RESULTS ==============")
  123.         print("=!!= Call id: {}".format(call_id))
  124.         print("=!!= Sample: {}/{}".format(source_dir, f))
  125.         with open("config-full-{}.json".format(call_id), "w") as f2:
  126.             f2.write(json.dumps(config, sort_keys=True, indent=2))
  127.         print("=!!============= END RESULTS ==============")
  128.  
  129.     for f in os.listdir(source_dir):
  130.         if not (f.find(".ogg") >= 0 and f.find("-out.ogg") == -1):
  131.             continue
  132.  
  133.         source = "{}/{}".format(source_dir, f)
  134.         output = "{}/{}".format(output_dir, construct_outfile(f))
  135.         procs.append(mp.Process(target=mp_target, args=(source, output, key, f)))
  136.         procs[-1].start()
  137.  
  138.     for p in procs:
  139.         p.join()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement