1- #!/usr/bin/env python2
1+ #!/usr/bin/env python3
22
33# uget-chrome-wrapper is a tool to integrate uGet Download manager
44# with Google Chrome in Linux systems.
1818# You should have received a copy of the GNU General Public License
1919# along with this program. If not, see <http://www.gnu.org/licenses/>.
2020
21-
22- import struct
23- import sys
24- import threading
21+ # sudo apt install python3-urllib3
22+ import struct , sys , threading , logging , json , urllib
2523from subprocess import call
26- import json
27- from urlparse import urlparse
24+ from urllib .parse import urlparse
2825from os .path import splitext , basename , join , expanduser
29- import urllib2
3026from mimetypes import guess_extension
3127
3228UGET_COMMAND = "uget-gtk"
3329VERSION = "1.3.4"
3430
31+ logger = logging .getLogger ()
32+ # log_file_path = join(expanduser('~'), 'uget-chrome-wrapper.log')
33+ # logging.basicConfig(format='%(asctime)s [%(levelname)s]: %(message)s', filename=log_file_path, filemode='a', level=logging.DEBUG)
34+ logger .propagate = False
35+
3536# Platform specific configuration
36- if sys .platform == " win32" :
37+ if sys .platform == ' win32' :
3738 # Set the default I/O mode to O_BINARY in windows
3839 import os , msvcrt
3940 msvcrt .setmode (sys .stdin .fileno (), os .O_BINARY )
4041 msvcrt .setmode (sys .stdout .fileno (), os .O_BINARY )
41- UGET_COMMAND = " uget"
42+ UGET_COMMAND = ' uget'
4243
4344def extract_file_name (url ):
4445 fileName = ''
45- if " googlevideo.com/" in url and '&title=' in url :
46+ if ' googlevideo.com/' in url and '&title=' in url :
4647 # Youtube video
47- url = urllib2 .unquote (url )
48+ url = urllib .unquote (url )
4849 url_components = url .split ('&' )
4950 for smt in url_components :
5051 if smt .startswith ('title=' ):
@@ -63,23 +64,36 @@ def extract_file_name(url):
6364
6465# Send a message to the webapp.
6566def send_message (message ):
66- # Write message size.
67- sys .stdout .write (struct .pack ('I' , len (message )))
68- # Write the message itself.
69- sys .stdout .write (message )
70- sys .stdout .flush ()
67+
68+ logger .info ('Sending message: ' + str (message ))
69+
70+ try :
71+ # Write message size.
72+ sys .stdout .buffer .write (struct .pack ('I' , len (message )))
73+ # Write the message itself.
74+ sys .stdout .write (message )
75+ sys .stdout .flush ()
76+ except Exception as e :
77+ logger .error ('Error in sending message: ' + str (e ))
7178
7279# Read messages from the webapp.
7380def read_message ():
81+
82+ logger .info ('uget-chrome-wrapper is reading the message' )
83+
7484 while 1 :
7585 # Read the message length (first 4 bytes).
76- text_length_bytes = sys .stdin .read (4 )
86+ text_length_bytes = sys .stdin .buffer . read (4 )
7787
7888 # Unpack message length as 4 byte integer.
7989 text_length = struct .unpack ('i' , text_length_bytes )[0 ]
8090
91+ logger .debug ('Message length: ' + str (text_length ))
92+
8193 # Read the text (JSON object) of the message.
82- text = sys .stdin .read (text_length ).decode ('utf-8' )
94+ text = sys .stdin .buffer .read (text_length ).decode ('utf-8' )
95+
96+ logger .debug ('Received message: ' + str (text ))
8397
8498 if text :
8599 if not 'url' in text :
0 commit comments