34 try: |
34 try: |
35 import bitly_api |
35 import bitly_api |
36 except ImportError as e: |
36 except ImportError as e: |
37 print ('Unable to import bitly_api: %s' % e) |
37 print ('Unable to import bitly_api: %s' % e) |
38 |
38 |
39 try: |
39 import urllib.request |
40 import urllib.request |
|
41 except ImportError: |
|
42 import urllib2 |
|
43 |
40 |
44 def shorten_link (link): |
41 def shorten_link (link): |
45 if 'bitly_api' in sys.modules: |
42 if 'bitly_api' in sys.modules: |
46 bitly_token = Config.get_node ('bitly').get_value ('access_token', '') |
43 bitly_token = Config.get_node ('bitly').get_value ('access_token', '') |
47 |
44 |
53 Irc.broadcast ('Error while shortening link "%s": %s' % (link, e)) |
50 Irc.broadcast ('Error while shortening link "%s": %s' % (link, e)) |
54 |
51 |
55 return link |
52 return link |
56 |
53 |
57 def read_url (url): |
54 def read_url (url): |
58 if 'urllib.request' in sys.modules: |
55 data = urllib.request.urlopen (urllib.request.Request (url)).read() |
59 return urllib.request.urlopen (urllib.request.Request (url)).read() |
56 return data.decode ('utf-8') |
60 else: |
|
61 return urllib2.urlopen (url).read() |
|
62 |
57 |
63 def make_progress_bar (p, barLength, colored=True): |
58 def make_progress_bar (p, barLength, colored=True): |
64 BoldChar, ColorChar = (Irc.BoldChar, Irc.ColorChar) |
59 BoldChar, ColorChar = (Irc.BoldChar, Irc.ColorChar) |
65 return BoldChar + '[' + BoldChar \ |
60 return BoldChar + '[' + BoldChar \ |
66 + ColorChar + '2,2' + ('=' * int (round (p * barLength))) \ |
61 + ColorChar + '2,2' + ('=' * int (round (p * barLength))) \ |