Completed
Push — master ( ce03d6...32fc37 )
by John
03:28
created

tclscan_main()   A

Complexity

Conditions 3

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 3
c 1
b 0
f 1
dl 0
loc 14
rs 9.4285
1
#!/usr/bin/env python3
2
"""Check latest update for TCL API devices."""
3
4
import sys  # load arguments
5
import requests  # session
6
from bbarchivist import networkutils  # lookup
7
from bbarchivist import jsonutils  # json
8
from bbarchivist import scriptutils  # default parser
9
10
__author__ = "Thurask"
11
__license__ = "WTFPL v2"
12
__copyright__ = "2017 Thurask"
13
14
15
def grab_args():
16
    """
17
    Parse arguments from argparse/questionnaire.
18
19
    Invoke :func:`droidlookup.droidlookup_main` with those arguments.
20
    """
21
    if len(sys.argv) > 1:
22
        parser = scriptutils.default_parser("bb-tclscan", "Check for updates for TCL devices")
23
        parser.add_argument(
24
            "-p",
25
            "--prd",
26
            dest="prd",
27
            help="Only scan one PRD",
28
            default=None)
29
        fgroup.add_argument(
0 ignored issues
show
Comprehensibility Best Practice introduced by
Undefined variable 'fgroup'
Loading history...
30
            "-l",
31
            "--list",
32
            dest="printlist",
33
            help="List PRDs in database",
34
            action="store_true",
35
            default=False)
36
        args = parser.parse_args(sys.argv[1:])
37
        parser.set_defaults()
38
        if args.printlist:
39
            jsonutils.list_prds()
0 ignored issues
show
Bug introduced by
It seems like a value for argument table is missing in the function call.
Loading history...
40
        elif args.prd is not None:
41
            tclscan_single(args.prd)
42
        else:
43
            tclscan_main()
44
    else:
45
        tclscan_main()
46
47
48
def tclscan_single(curef):
49
    """
50
    Scan one PRD and produce download URL and filename.
51
52
    :param curef: PRD of the phone variant to check.
53
    :type curef: str
54
    """
55
    sess = requests.Session()
56
    ctext = networkutils.tcl_check(curef, sess)
57
    if ctext is None:
58
        raise SystemExit
59
    tvver, firmwareid, filename, fsize, fhash = networkutils.parse_tcl_check(ctext)
60
    del fsize, fhash
61
    salt = networkutils.tcl_salt()
62
    vkhsh = networkutils.vkhash(curef, tvver, firmwareid, salt)
63
    updatetext = networkutils.tcl_download_request(curef, tvver, firmwareid, salt, vkhsh, sess)
64
    downloadurl = networkutils.parse_tcl_download_request(updatetext)
65
    print("{0}: HTTP {1}".format(filename, networkutils.getcode(downloadurl, sess)))
66
    print(downloadurl)
67
68
69
def tclscan_main():
70
    """
71
    Scan every PRD and produce latest versions.
72
    """
73
    scriptutils.slim_preamble("TCLSCAN")
74
    prddict = jsonutils.load_json("prds")
75
    sess = requests.Session()
76
    for device in prddict.keys():
77
        print("~{0}~".format(device))
78
        for curef in prddict[device]:
79
            checktext = networkutils.tcl_check(curef, sess)
80
            tvver, firmwareid, filename, fsize, fhash = networkutils.parse_tcl_check(checktext)
81
            del firmwareid, filename, fsize, fhash
82
            print("{0}: {1}".format(curef, tvver))
83
84
85
if __name__ == "__main__":
86
    grab_args()
87