File size: 5,287 Bytes
942dcac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63364aa
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import argparse
from mainLogic.error import errorList
from mainLogic.utils.glv import Global
import sys
from mainLogic.utils.os2 import SysFunc
import os
from mainLogic.main import Main
from beta.shellLogic import shell
from mainLogic.startup.checkup import CheckState

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

# hardcoding the list of executables required for the script to run
# should be available in the PATH or the user should provide the path to the executables
EXECUTABLES = glv.EXECUTABLES




def main():

    # parsing the arguments
    parser = argparse.ArgumentParser(description='PhysicsWallah M3u8 parser.')

    parser.add_argument('--csv-file', type=str, help='Input csv file. Legacy Support too.')
    parser.add_argument('--id', type=str,
                        help='PhysicsWallh Video Id for single usage. Incompatible with --csv-file.   Must be used '
                             'with --name')
    parser.add_argument('--name', type=str,
                        help='Name for the output file. Incompatible with --csv-file.   Must be used with --id')
    parser.add_argument('--dir', type=str, help='Output Directory')
    parser.add_argument('--verbose', action='store_true', help='Verbose Output')
    parser.add_argument('--shell',action='store_true',help='Start the shell')
    parser.add_argument('--version', action='version', version='%(prog)s 1.0')
    parser.add_argument('--simulate', action='store_true',
                        help='Simulate the download process. No files will be downloaded.)')

    args = parser.parse_args()

    if args.shell:
        shell.main()


    # user_input is given preference i.e if --verbose is true it will override
    # however if --verbose is false but prefs['verbose'] is true
    glv.vout = args.verbose

    global prefs

    # check if all dependencies are installed
    state = CheckState().checkup(EXECUTABLES, directory=args.dir,verbose=glv.vout)
    prefs = state['prefs']


    # --------------------------------------------------------------------------------------------------------------------------------------
    # setting verbose output
    # gives preference to user input
    if not glv.vout and prefs['verbose']: glv.vout = prefs['verbose']

    verbose = glv.vout

    OUT_DIRECTORY = prefs['dir']
    if verbose: Global.hr(); glv.dprint(f"Tmp Dir is: {SysFunc.modify_path(prefs['tmpDir'])}")
    if verbose: Global.hr(); glv.dprint(f'Output Directory: {OUT_DIRECTORY}')
    if verbose: Global.hr(); glv.dprint(f"Horizontal Rule: {not Global.disable_hr}")

    # --------------------------------------------------------------------------------------------------------------------------------------
    # end of loading user preferences


    # starting the main process

    #if both csv file and (id or name) is provided then -> exit with error code 3
    if args.csv_file and (args.id or args.name):
        print("Both csv file and id (or name) is provided. Unable to decide. Aborting! ...")
        sys.exit(3)

    # handle in case --csv-file is provided
    if args.csv_file:

        # simulation mode
        if args.simulate:
            print("Simulating the download csv process. No files will be downloaded.")
            print("File to be processed: ", args.csv_file)
            exit(0)

        # exiting in case the CSV File is not found
        if not os.path.exists(args.csv_file):
            errorList['csvFileNotFound']['func'](args.csv_file)
            sys.exit(errorList['csvFileNotFound']['code'])

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

                # adding support for csv file with partial errors
                try:
                    Main(id=id,
                         name=name,
                         directory=OUT_DIRECTORY,
                         ffmpeg=state['ffmpeg'],
                         nm3Path=state['nm3'],
                         mp4d=state['mp4decrypt'],
                         tmpDir=prefs['tmpDir'],
                         verbose=verbose,
                         suppress_exit=True  # suppress exit in case of error (as multiple files are being processed)
                         ).process()

                except Exception as e:
                    errorList['downloadFailed']['func'](name, id)



    # handle in case key and name is given
    elif args.id and args.name:

        # simulation mode
        if args.simulate:
            print("Simulating the download process. No files will be downloaded.")
            print("Id to be processed: ", args.id)
            print("Name to be processed: ", args.name)
            exit(0)

        try:

            Main(id=args.id,
                 name=args.name,
                 directory=OUT_DIRECTORY,
                 ffmpeg=state['ffmpeg'],
                 nm3Path=state['nm3'],
                 mp4d=state['mp4decrypt'],
                 tmpDir=prefs['tmpDir'],
                 verbose=verbose).process()

        except Exception as e:
            errorList['downloadFailed']['func'](args.name, args.id)
            sys.exit(errorList['downloadFailed']['code'])

    # in case neither is used
    else:
        exit(1)


if __name__ == "__main__":
    main()