File size: 3,640 Bytes
41ca5f7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import sys
import os
from mainLogic.error import errorList
from mainLogic.utils.glv import Global
from mainLogic.utils.os2 import SysFunc
from mainLogic.main import Main
from beta.shellLogic import shell
from mainLogic.startup.checkup import CheckState
from mainLogic.utils.gen_utils import generate_safe_folder_name
from mainLogic.utils import glv_var

# global variables
prefs = {}
glv = Global()

# hardcoding the list of executables required for the script to run
EXECUTABLES = glv_var.EXECUTABLES

def check_dependencies(directory, verbose):
    """Check if all dependencies are installed."""
    global prefs
    state = CheckState().checkup(EXECUTABLES, directory=directory, verbose=verbose)
    prefs = state['prefs']
    return state

def start_shell():
    """Start the shell if requested."""
    shell.main()

def start_webui(port, verbose):
    """Start the WebUI if requested."""
    from run import app
    if not prefs['webui']:
        Global.errprint("WebUI is not enabled in the preferences. Exiting ...")
        sys.exit(1)

    if 'webui-port' in prefs:
        port = prefs['webui-port']

    if verbose:
        Global.hr()
        Global.dprint(f"Starting WebUI on port {port}")

    app.run(host="0.0.0.0", debug=True, port=port)

def download_process(id, name, state, verbose, simulate=False):
    """Process a single download or simulate the download."""
    if simulate:
        print("Simulating the download process. No files will be downloaded.")
        print(f"Id to be processed: {id}")
        print(f"Name to be processed: {name}")
        return

    try:
        Main(
            id=id,
            name=generate_safe_folder_name(name),
            directory=prefs['dir'],
            ffmpeg=state['ffmpeg'],
            vsdPath=state['vsd'],
            token=prefs['token'],
            mp4d=state['mp4decrypt'],
            tmpDir=prefs['tmpDir'],
            verbose=verbose
        ).process()
    except Exception as e:
        if verbose:
            Global.hr()
            glv.errprint(f"Error: {e}")
        errorList['downloadFailed']['func'](name, id)
        sys.exit(errorList['downloadFailed']['code'])

def handle_csv_file(csv_file, state, verbose, simulate=False):
    """Handle processing of CSV file."""
    if not os.path.exists(csv_file):
        errorList['csvFileNotFound']['func'](csv_file)
        sys.exit(errorList['csvFileNotFound']['code'])

    if simulate:
        print("Simulating the download csv process. No files will be downloaded.")
        print(f"File to be processed: {csv_file}")
        return

    with open(csv_file, 'r') as f:
        for line in f:
            name, id = line.strip().split(',')
            name = generate_safe_folder_name(name)
            download_process(id, name, state, verbose)

def main(csv_file=None, id=None, name=None, directory=None, verbose=False, shell=False, webui_port=None, simulate=False):
    if shell:
        start_shell()

    glv.vout = verbose

    state = check_dependencies(directory, glv.vout)

    if webui_port is not None:
        start_webui(webui_port, glv.vout)

    if simulate:
        if csv_file:
            handle_csv_file(csv_file, state, glv.vout, simulate=True)
        elif id and name:
            download_process(id, name, state, glv.vout, simulate=True)
        return

    if csv_file and (id or name):
        print("Both csv file and id (or name) is provided. Unable to decide. Aborting! ...")
        sys.exit(3)

    if csv_file:
        handle_csv_file(csv_file, state, glv.vout)
    elif id and name:
        download_process(id, name, state, glv.vout)
    else:
        sys.exit(1)