20
20
from qreator_lib.i18n import _
23
from urllib import urlencode
26
25
class IsdgShortener(object):
26
# TRANSLATORS: this refers to the is.dg URL shortening service
28
29
def __init__(self, url=None):
29
self.api_url= "http://is.gd/create.php"
30
self.api_url = "http://is.gd/create.php"
32
33
def short_url(self):
33
self.r = requests.get("{0}?format=simple&url={1}".format(self.api_url, self.url))
34
self.r = requests.get("{0}?format=simple&url={1}".format(self.api_url,
34
36
if self.r.content.startswith("Error"):
35
37
raise Exception(self.r.content)
36
38
return self.r.content
39
41
class TinyUrlShortener(object):
42
# TRANSLATORS: this refers to the tinyurl.com URL shortening service
40
43
name = _("TinyUrl")
41
45
def __init__(self, url=None):
42
46
self.api_url = "http://tinyurl.com/api-create.php"
50
54
class BitlyShortener(object):
55
# TRANSLATORS: this refers to the bit.ly URL shortening service
52
58
def __init__(self, url=None):
53
59
self.api_url = 'http://api.bit.ly/v3/shorten'
55
self.data = {"login": "", # TODO
61
self.data = {"login": "", # TODO
57
63
"longUrl": self.url}
59
65
def short_url(self):
65
71
class GoogleShortener(object):
72
# TRANSLATORS: this refers to the goo.gl URL shortening service
67
75
def __init__(self, url=None):
68
76
self.api_key = "AIzaSyCU_pFNpACW4luWEZRgNnfWMEUdlnZyYQI"
69
77
self.api_url = 'https://www.googleapis.com/urlshortener/v1/url'
71
79
self.params = {"key": self.api_key,
72
'Content-Type' : 'application/json'}
80
'Content-Type': 'application/json'}
73
81
self.data = {"longUrl": self.url}
75
83
def short_url(self):
76
self.r = requests.post(self.api_url, headers=self.params, data=json.dumps(self.data))
84
self.r = requests.post(self.api_url, headers=self.params,
85
data=json.dumps(self.data))
77
86
self.results = json.loads(self.r.content)
78
87
return self.results["id"]
81
SHORTENER_TYPES = [IsdgShortener, TinyUrlShortener, BitlyShortener, GoogleShortener,]
84
98
class QRCodeURLGtk(object):
101
115
self.entry.set_placeholder_text(_('[URL]'))
102
116
self.combobox.set_active(0)
104
# Currently four shortener options are available: isdg google tinyurl bitly
105
# We are choosing isdg at the moment. Later we will allow to define this
118
# Currently four shortener options are available: isdg, google,
120
# We are choosing isdg at the moment. Later we will allow to define
121
# this in preferences.
107
122
self.Shortener = SHORTENER_TYPES[0]
124
# TRANSLATORS: leave '{0}' as it is, it will be replaced by the
125
# chosen URL shortening service
109
126
self.builder.get_object("togglebuttonShorten").set_tooltip_text(
110
"Use the {0} online service to generate a short URL.".format(self.Shortener.name))
127
"Use the {0} online service to generate a short URL.".format(
128
self.Shortener.name))
111
129
self.builder.get_object("togglebuttonShorten").set_has_tooltip(True)
113
self.entryhandler = self.entry.connect("changed", self.on_entryURL_changed)
114
# moved this signal from the glade file to here, to be able to block it later
115
self.iconhandler = self.entry.connect("icon-press", self.on_entryURL_icon_press)
131
self.entryhandler = self.entry.connect("changed",
132
self.on_entryURL_changed)
133
# Moved this signal from the glade file to here, to be able to block
135
self.iconhandler = self.entry.connect("icon-press",
136
self.on_entryURL_icon_press)
116
137
self.combobox.connect("changed", self.on_comboboxtextProtocol_changed)
118
139
def on_togglebuttonShorten_toggled(self, widget):
124
145
self.entry.handler_block(self.iconhandler)
125
146
self.combobox.set_sensitive(False)
126
147
self.long_address = self.address
127
self.entry.set_icon_activatable(Gtk.EntryIconPosition.SECONDARY, False)
128
# if the request takes longer, the interface should in the meantime change to
129
# unsensitive button and entry, to show at least some behaviour
148
self.entry.set_icon_activatable(Gtk.EntryIconPosition.SECONDARY,
150
# If the request takes longer, the interface should in the
151
# meantime change to unsensitive button and entry, to show at
152
# least some behaviour
130
153
GObject.idle_add(self._shorten_and_display)
132
155
self.entry.set_has_tooltip(False)
135
158
self.entry.handler_unblock(self.iconhandler)
136
159
self.combobox.set_sensitive(True)
137
160
self.qr_code_update_func(self.address)
138
self.entry.set_icon_activatable(Gtk.EntryIconPosition.SECONDARY, True)
161
self.entry.set_icon_activatable(Gtk.EntryIconPosition.SECONDARY,
140
164
def on_messagedialog1_response(self, widget, data=None):
150
174
self.short_address = s.short_url()
151
175
except requests.exceptions.ConnectionError as e:
152
self.messagedialog.set_markup(_("A network connection error occured: {0}".format(e)))
176
# TRANSLATORS: leave '{0}' as it is, it will be replaced by the
177
# exception's error message
178
self.messagedialog.set_markup(
179
_("A network connection error occured: {0}".format(e)))
153
180
self.messagedialog.show()
154
181
GObject.idle_add(self._reset_shortener)
156
183
except Exception as e:
157
self.messagedialog.set_markup(_("An error occured while trying to shorten the url: {0}".format(e)))
184
# TRANSLATORS: leave '{0}' as it is, it will be replaced by the
185
# exception's error message
186
self.messagedialog.set_markup(
187
_("An error occured while trying to shorten the URL: {0}".format(e))) # pylint: disable=E0501
158
188
self.messagedialog.show()
159
189
GObject.idle_add(self._reset_shortener)
190
220
def check_and_remove_protocol(self, widget):
192
222
Creates a list of protocols (as given by the combobox).
193
Checks if beginning of the text entry is in that list. If so, remove the protocol
194
and select the appropriate protocol in the combobox.
223
Checks if the beginning of the text entry is in that list. If so,
224
remove the protocol and select the appropriate protocol in the
196
for n, protocol in enumerate([mr[0] for mr in self.combobox.get_model()]):
227
for n, protocol in enumerate(
228
[mr[0] for mr in self.combobox.get_model()]):
197
229
if widget.get_text().strip().startswith(protocol):
198
230
widget.set_text(widget.get_text().strip().strip(protocol))
199
231
self.combobox.set_active(n)