1919# along with this program. If not, see <http://www.gnu.org/licenses/>.
2020
2121# 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
2323from urllib .parse import urlparse
24+ from urllib .request import urlopen
2425from os .path import splitext , basename , join , expanduser
2526from mimetypes import guess_extension
2627
2728cookie_filepath = join (tempfile .gettempdir (), 'uget_cookie' )
2829UGET_COMMAND = "uget-gtk"
30+ UGET_CATEGORY_DIR = join (expanduser ('~' ), '.config/uGet/category' )
2931VERSION = "2.0.4"
3032
3133logger = logging .getLogger ()
@@ -40,6 +42,7 @@ if sys.platform == 'win32':
4042 msvcrt .setmode (sys .stdin .fileno (), os .O_BINARY )
4143 msvcrt .setmode (sys .stdout .fileno (), os .O_BINARY )
4244 UGET_COMMAND = 'uget'
45+ UGET_CATEGORY_DIR = join (expanduser ('~' ), 'AppData\\ Local\\ uGet\\ category' )
4346
4447def extract_file_name (url ):
4548 fileName = ''
@@ -152,9 +155,42 @@ def read_message():
152155 sys .exit (0 )
153156
154157
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+
155181def Main ():
156182 read_message ()
157183
158184
159185if __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