File: cli-switch.py

package info (click to toggle)
pyatem 0.13.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,776 kB
  • sloc: python: 15,193; xml: 435; ansic: 256; sh: 26; makefile: 20
file content (46 lines) | stat: -rw-r--r-- 1,637 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#!/usr/bin/env python3
import argparse
import json
import sys

import requests
from requests.auth import HTTPBasicAuth


def switcher(proxy, index, to_source, auto=False, username=None, password=None):
    auth = None
    if username is not None and password is not None:
        auth = HTTPBasicAuth(username, password)

    if to_source.isnumeric():
        to_source = int(to_source)

    if auto:
        r = requests.get(f'{proxy}/program-bus-input', auth=auth)
        try:
            reply = r.json()
            if reply[str(index)]['source'] != to_source:
                data = {'index': index, 'source': to_source}
                requests.post(f'{proxy}/preview-input', data, auth=auth)
                data = {'index': index}
                requests.post(f'{proxy}/auto', data, auth=auth)
            else:
                sys.exit(2)
        except json.decoder.JSONDecodeError:
            sys.exit(1)
    else:
        data = {'index': index, 'source': to_source}
        requests.post(f'{proxy}/program-input', data=data, auth=auth)


if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('--proxy', '-s', required=True)
    parser.add_argument('--to-source', '-t', required=True)
    parser.add_argument('--username', '-u', required=False)
    parser.add_argument('--password', '-p', required=False)
    parser.add_argument('--auto', '-a', required=False, action='store_true')
    parser.add_argument('--index', '-i', type=int, required=False, default=0)
    args = parser.parse_args()

    switcher(args.proxy, args.index, args.to_source, args.auto, args.username, args.password)