19
19
# along with this program. If not, see <http://www.gnu.org/licenses/>.
20
20
21
21
# sudo apt install python3-urllib3
22
- import struct , sys , threading , logging , json , urllib , subprocess , tempfile
22
+ import argparse , struct , sys , threading , logging , json , urllib , subprocess , tempfile
23
23
from urllib .parse import urlparse
24
+ from urllib .request import urlopen
24
25
from os .path import splitext , basename , join , expanduser
25
26
from mimetypes import guess_extension
26
27
27
28
cookie_filepath = join (tempfile .gettempdir (), 'uget_cookie' )
28
29
UGET_COMMAND = "uget-gtk"
30
+ UGET_CATEGORY_DIR = join (expanduser ('~' ), '.config/uGet/category' )
29
31
VERSION = "2.0.4"
30
32
31
33
logger = logging .getLogger ()
@@ -40,6 +42,7 @@ if sys.platform == 'win32':
40
42
msvcrt .setmode (sys .stdin .fileno (), os .O_BINARY )
41
43
msvcrt .setmode (sys .stdout .fileno (), os .O_BINARY )
42
44
UGET_COMMAND = 'uget'
45
+ UGET_CATEGORY_DIR = join (expanduser ('~' ), 'AppData\\ Local\\ uGet\\ category' )
43
46
44
47
def extract_file_name (url ):
45
48
fileName = ''
@@ -152,9 +155,42 @@ def read_message():
152
155
sys .exit (0 )
153
156
154
157
158
+ def download_category (fileName ):
159
+ """
160
+ Download the category from the repository and store in the config folder.
161
+ """
162
+ downloads_directory = join (expanduser ('~' ), 'Downloads' )
163
+ url = 'https://raw.githubusercontent.com/slgobinath/uget-chrome-wrapper/master/config/category/' + fileName
164
+ with urlopen (url ) as response :
165
+ data = json .loads (response .read ().decode ('utf-8' ))
166
+ data ['info' ]['common' ]['folder' ] = join (downloads_directory , data ['info' ]['common' ]['folder' ])
167
+
168
+ category_file = join (UGET_CATEGORY_DIR , fileName )
169
+ logger .debug ('Writing to ' + str (category_file ))
170
+ with open (category_file , 'w' ) as outfile :
171
+ json .dump (data , outfile , indent = 4 )
172
+
173
+ def add_categories ():
174
+ """
175
+ Download and add the categories to the uGet config directory.
176
+ """
177
+ for index in range (1 , 7 ):
178
+ print ('000{}.json' .format (index ))
179
+ download_category ('000{}.json' .format (index ))
180
+
155
181
def Main ():
156
182
read_message ()
157
183
158
184
159
185
if __name__ == '__main__' :
160
- Main ()
186
+ parser = argparse .ArgumentParser ()
187
+ parser .add_argument ("-c" , "--categorize" , help = "create new categories based on the file types" , action = "store_true" )
188
+ parser .add_argument ("-v" , "--version" , help = "display the version of uget-chrome-wrapper and exit" , action = "store_true" )
189
+ args = parser .parse_args ()
190
+
191
+ if args .version :
192
+ print ('uget-chrome-wrapper {}' .format (VERSION ))
193
+ elif args .categorize :
194
+ add_categories ()
195
+ else :
196
+ Main ()
0 commit comments