[kupfer] plugin.shorten_links: add support for Shorl.com and Ur1.ca



commit 47d9ccc447b168c32d20dde6b5e9eb4f7a7bf028
Author: Karol BÄ?dkowski <karol bedkowsk+gh gmail com>
Date:   Tue Dec 22 10:34:13 2009 +0100

    plugin.shorten_links: add support for Shorl.com and Ur1.ca

 kupfer/plugin/shorten_links.py |   75 ++++++++++++++++++++++++++++++++++++++++
 1 files changed, 75 insertions(+), 0 deletions(-)
---
diff --git a/kupfer/plugin/shorten_links.py b/kupfer/plugin/shorten_links.py
index 03b4721..b452848 100644
--- a/kupfer/plugin/shorten_links.py
+++ b/kupfer/plugin/shorten_links.py
@@ -1,5 +1,6 @@
 # -*- coding: UTF-8 -*-
 
+import re
 import httplib
 import urllib
 
@@ -13,6 +14,13 @@ __version__ = "2009-12-21"
 __author__ = "Karol BÄ?dkowski <karol bedkowski gmail com>"
 
 
+_HEADER = {
+		'Content-type':'application/x-www-form-urlencoded',
+		'Accept': 'text/xml,application/xml,application/xhtml+xml,text/html',
+		'Accept-charset': 'utf-8;q=0.7'
+}
+
+
 class _ShortLinksService(Leaf):
 	def get_icon_name(self):
 		return "text-html"
@@ -45,6 +53,71 @@ class TinyUrl(_ShortLinksService):
 		return _('Error')
 
 
+SHORL_HOST='shorl.com'
+SHORL_PATH='/create.php?'
+SHORL_RESULT_RE = re.compile(r'Shorl: \<a href=".+?" rel="nofollow">(.+?)</a>')
+
+class Shorl(_ShortLinksService):
+	""" Shorten urls with shorl.com """
+	def __init__(self):
+		_ShortLinksService.__init__(self, 'Shorl.com', 'Shorl.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')
+
+		except (httplib.HTTPException, ValueError), err:
+			pretty.print_error(__name__, 'TinyUrl.process error', type(err), err)
+		return _('Error')
+
+
+UR1CA_HOST='ur1.ca'
+UR1CA_PATH=''
+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')
+
+	def process(self, url):
+		if not (url.startswith('http://') or url.startswith('https://') or 
+				url.startswith('mailto:')):
+			url = 'http://' + url
+		query_param = urllib.urlencode(dict(longurl=url, submit='Make it an ur1!'))
+		try:
+			conn = httplib.HTTPConnection(UR1CA_HOST)
+			#conn.debuglevel=255
+			conn.request("POST", UR1CA_PATH, query_param, _HEADER)
+			resp = conn.getresponse()
+			if resp.status != 200:
+				raise ValueError('invalid response %d, %s' % (resp.status,
+					resp.reason))
+			
+			result = resp.read()
+			resurl = UR1CA_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 '''
 
@@ -77,6 +150,8 @@ class ServicesSource(Source):
 
 	def get_items(self):
 		yield TinyUrl()
+		yield Shorl()
+		yield Ur1Ca()
 
 	def get_icon_name(self):
 		return "applications-internet"



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]