[kupfer] plugin.shorten_links: Refactor, merge duplicated code
- From: Ulrik Sverdrup <usverdrup src gnome org>
- To: svn-commits-list gnome org
- Cc:
- Subject: [kupfer] plugin.shorten_links: Refactor, merge duplicated code
- Date: Thu, 24 Dec 2009 01:40:46 +0000 (UTC)
commit c62290392e3efd71caee989f0386d027ba502a85
Author: Ulrik Sverdrup <ulrik sverdrup gmail com>
Date: Tue Dec 22 12:51:18 2009 +0100
plugin.shorten_links: Refactor, merge duplicated code
Refactor mercilessly: we don't like duplicated code. Kupfer loves
patches with more removed lines than added lines! :-)
We refactor to share common code in the 3 of the services that use
GET requests.
kupfer/plugin/shorten_links.py | 107 +++++++++++++---------------------------
1 files changed, 35 insertions(+), 72 deletions(-)
---
diff --git a/kupfer/plugin/shorten_links.py b/kupfer/plugin/shorten_links.py
index 9222e9f..8ca8e5e 100644
--- a/kupfer/plugin/shorten_links.py
+++ b/kupfer/plugin/shorten_links.py
@@ -22,67 +22,62 @@ _HEADER = {
class _ShortLinksService(Leaf):
+ def __init__(self, name):
+ Leaf.__init__(self, name, name)
def get_icon_name(self):
return "text-html"
-
-TINYURL_PATH="/api-create.php?"
-TINYURL_HOST="tinyurl.com"
-
-class TinyUrl(_ShortLinksService):
- """ Shorten urls with tinyurl.com """
- def __init__(self):
- _ShortLinksService.__init__(self, 'TinyUrl.com', 'TinyUrl.com')
+class _GETService(_ShortLinksService, pretty.OutputMixin):
+ """ A unified shortener service working with GET requests """
+ host = None
+ path = None
+ result_regex = None
def process(self, url):
- query_param = urllib.urlencode(dict(url=url))
+ query_string = urllib.urlencode({"url": url})
try:
- conn = httplib.HTTPConnection(TINYURL_HOST)
- #conn.debuglevel=255
- conn.request("GET", TINYURL_PATH+query_param)
+ conn = httplib.HTTPConnection(self.host)
+ conn.request("GET", self.path+query_string)
resp = conn.getresponse()
if resp.status != 200:
raise ValueError('invalid response %d, %s' % (resp.status,
resp.reason))
result = resp.read()
- return result
+ if self.result_regex is not None:
+ resurl = re.findall(self.result_regex, result)
+ if resurl:
+ return resurl[0]
+ else:
+ return result
except (httplib.HTTPException, ValueError), err:
- pretty.print_error(__name__, 'TinyUrl.process error', type(err), err)
+ self.output_error(type(err), err)
return _('Error')
-SHORL_HOST='shorl.com'
-SHORL_PATH='/create.php?'
-SHORL_RESULT_RE = re.compile(r'Shorl: \<a href=".+?" rel="nofollow">(.+?)</a>')
+class TinyUrl(_GETService):
+ host = "tinyurl.com"
+ path = "/api-create.php?"
-class Shorl(_ShortLinksService):
- """ Shorten urls with shorl.com """
def __init__(self):
- _ShortLinksService.__init__(self, 'Shorl.com', 'Shorl.com')
+ _ShortLinksService.__init__(self, u'TinyUrl.com')
- def process(self, url):
- query_param = urllib.urlencode(dict(url=url))
- try:
- conn = httplib.HTTPConnection(SHORL_HOST)
- #conn.debuglevel=255
- conn.request("GET", SHORL_PATH+query_param)
- resp = conn.getresponse()
- if resp.status != 200:
- raise ValueError('invalid response %d, %s' % (resp.status,
- resp.reason))
-
- result = resp.read()
- resurl = SHORL_RESULT_RE.findall(result)
- if resurl:
- return resurl[0]
- return _('Error')
+class Shorl(_GETService):
+ host = 'shorl.com'
+ path = '/create.php?'
+ result_regex = r'Shorl: \<a href=".+?" rel="nofollow">(.+?)</a>'
- except (httplib.HTTPException, ValueError), err:
- pretty.print_error(__name__, 'TinyUrl.process error', type(err), err)
- return _('Error')
+ def __init__(self):
+ _ShortLinksService.__init__(self, u'Shorl.com')
+class BitLy(_GETService):
+ host = 'bit.ly'
+ path = '/?'
+ result_regex = r'\<input id="shortened-url" value="(.+?)" \/\>'
+
+ def __init__(self):
+ _ShortLinksService.__init__(self, u'Bit.ly')
UR1CA_HOST='ur1.ca'
UR1CA_PATH=''
@@ -91,7 +86,7 @@ UR1CA_RESULT_RE = re.compile(r'\<p class="success">.+?<a href=".+?">(.+?)</a></p
class Ur1Ca(_ShortLinksService):
""" Shorten urls with Ur1.ca """
def __init__(self):
- _ShortLinksService.__init__(self, 'Ur1.ca', 'Ur1.ca')
+ _ShortLinksService.__init__(self, u'Ur1.ca')
def process(self, url):
if not (url.startswith('http://') or url.startswith('https://') or
@@ -118,38 +113,6 @@ class Ur1Ca(_ShortLinksService):
return _('Error')
-
-BITLY_HOST='bit.ly'
-BITLY_PATH='/?'
-BITLY_RESULT_RE = re.compile(r'\<input id="shortened-url" value="(.+?)" \/\>')
-
-class BitLy(_ShortLinksService):
- """ Shorten urls with bit.ly """
- def __init__(self):
- _ShortLinksService.__init__(self, 'Bit.ly', 'Bit.ly')
-
- def process(self, url):
- query_param = urllib.urlencode(dict(url=url,s='',keyword=''))
- try:
- conn = httplib.HTTPConnection(BITLY_HOST)
- #conn.debuglevel=255
- conn.request("GET", BITLY_PATH+query_param)
- resp = conn.getresponse()
- if resp.status != 200:
- raise ValueError('invalid response %d, %s' % (resp.status,
- resp.reason))
-
- result = resp.read()
- resurl = BITLY_RESULT_RE.findall(result)
- if resurl:
- return resurl[0]
- return _('Error')
-
- except (httplib.HTTPException, ValueError), err:
- pretty.print_error(__name__, 'TinyUrl.process error', type(err), err)
- return _('Error')
-
-
class ShortenLinks(Action):
''' Shorten links with selected engine '''
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]