Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import argparse
- import uuid
- import requests
- import json
- import os
- import sys
- import subprocess
- import time
- import multiprocessing as mp
- def construct_outfile(source):
- return "".join(source.split(".")[:-1]) + "-{side}-out.ogg"
- def make_call(in_file, out_a_file, out_b_file, key):
- n = 4
- m = 5
- retry = True
- i = 0
- while retry:
- call_id = uuid.uuid4()
- try:
- r = requests.get("https://api.contest.com/voip{auth_token}/getConnection".format(auth_token=key),
- params={"call": call_id}, timeout=1)
- except:
- continue
- time.sleep(3)
- config = r.json()
- if not config["ok"]:
- i += 1
- continue
- conf_file = "config-{}.json".format(call_id)
- with open(conf_file, "w") as f1:
- f1.write(json.dumps(config["result"]["config"]))
- res = {"returncode": 0}
- k = 0
- while retry and k < m:
- print("!!!!!!!!!!!!!!!!! 1-st call !!!!!!!!!!!!!!!!!")
- os.system(
- "./tgvoipcall {ip}:{port} {caller_tag} -k {key} -i {in_file} -o {out_b_file} -c {config_file} -r caller &".
- format(
- ip=config["result"]["endpoints"][0]["ip"],
- port=config["result"]["endpoints"][0]["port"],
- caller_tag=config["result"]["endpoints"][0]["peer_tags"]["caller"],
- key=config["result"]["encryption_key"],
- in_file=in_file,
- out_b_file=out_b_file,
- config_file=conf_file
- ))
- time.sleep(0.5)
- print("!!!!!!!!!!!!!!!!! 2-nd call !!!!!!!!!!!!!!!!!")
- res = subprocess.run(
- "./tgvoipcall {ip}:{port} {callee_tag} -k {key} -i {in_file} -o {out_a_file} -c {config_file} -r callee".
- format(
- ip=config["result"]["endpoints"][0]["ip"],
- port=config["result"]["endpoints"][0]["port"],
- callee_tag=config["result"]["endpoints"][0]["peer_tags"]["callee"],
- key=config["result"]["encryption_key"],
- in_file=in_file,
- out_a_file=out_a_file,
- config_file=conf_file
- ).split())
- k += 1
- retry = (res.returncode != 0)
- i += 1
- if i > n:
- retry = False
- print("!!!!!!!!!!!!!!!!! Faild after {} retry !!!!!!!!!!!!!!!!!".format(n))
- if retry:
- print("!!!!!!!!!!!!!!!!! RETRY {} !!!!!!!!!!!!!!!!!".format(i+k))
- return call_id, config
- if __name__ == "__main__":
- parser = argparse.ArgumentParser(description="Run all TG Contest programs with all samples and check call results")
- parser.add_argument("-k", "--api-key", help="TG contest API key", required=True)
- parser.add_argument("-c", "--call-id", help="TG session call id")
- parser.add_argument("-S", "--samples-dir", help="TG samples directory, defaults to .")
- parser.add_argument("-O", "--output-dir", help="Directory for saving output files, directory will be created," +
- " defaults to SAMPLES_DIR")
- parser.add_argument("-s", "--sample", help="Sample file name to play in call, must be used in conjunction with -o")
- parser.add_argument("-o", "--output", help="Sample output file mask, must be used in conjunction with -s")
- args = parser.parse_args()
- source = False
- source_dir = False
- output_dir = ""
- if args.samples_dir:
- source_dir = args.samples_dir
- if args.output_dir:
- output_dir = args.output_dir
- try:
- os.mkdir(output_dir)
- except FileExistsError:
- pass
- else:
- output_dir = source_dir
- elif args.sample:
- source = args.sample
- if args.output:
- output = args.output
- else:
- output = construct_outfile(source)
- if not source and not source_dir:
- source_dir = "."
- key = args.api_key
- if not source_dir:
- # noinspection PyUnboundLocalVariable
- call_id, config = make_call(source, output.format(side="a"), output.format(side="b"), key)
- sys.exit(0)
- mp.set_start_method("fork")
- procs = []
- def mp_target(source, output, key, f):
- call_id, config = make_call(source, output.format(side="a"), output.format(side="b"), key)
- print("=!!============= RESULTS ==============")
- print("=!!= Call id: {}".format(call_id))
- print("=!!= Sample: {}/{}".format(source_dir, f))
- with open("config-full-{}.json".format(call_id), "w") as f2:
- f2.write(json.dumps(config, sort_keys=True, indent=2))
- print("=!!============= END RESULTS ==============")
- for f in os.listdir(source_dir):
- if not (f.find(".ogg") >= 0 and f.find("-out.ogg") == -1):
- continue
- source = "{}/{}".format(source_dir, f)
- output = "{}/{}".format(output_dir, construct_outfile(f))
- procs.append(mp.Process(target=mp_target, args=(source, output, key, f)))
- procs[-1].start()
- for p in procs:
- p.join()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement