changeset 29:606307f5bfa1

Adding new url shorteners
author Walter Cruz <walter@waltercruz.com>
date Wed, 19 Aug 2009 16:09:48 -0300
parents 569a8f40bb6b
children fa175368618d
files feed2twitter/VERSION feed2twitter/feed2twitter.egg-info/SOURCES.txt feed2twitter/feed2twitter/__init__.py feed2twitter/feed2twitter/shortener.py feed2twitter/feed2twitter/tinyurl.py feed2twitter/tests/test_shortener.py feed2twitter/tests/test_tinyurl.py
diffstat 7 files changed, 148 insertions(+), 85 deletions(-) [+]
line wrap: on
line diff
--- a/feed2twitter/VERSION	Wed Aug 19 15:03:07 2009 -0300
+++ b/feed2twitter/VERSION	Wed Aug 19 16:09:48 2009 -0300
@@ -1,1 +1,1 @@
-0.6.2
\ No newline at end of file
+0.6.5
\ No newline at end of file
--- a/feed2twitter/feed2twitter.egg-info/SOURCES.txt	Wed Aug 19 15:03:07 2009 -0300
+++ b/feed2twitter/feed2twitter.egg-info/SOURCES.txt	Wed Aug 19 16:09:48 2009 -0300
@@ -10,7 +10,7 @@
 feed2twitter/options.py
 feed2twitter/parsetime.py
 feed2twitter/readrss.py
-feed2twitter/tinyurl.py
+feed2twitter/shortener.py
 feed2twitter.egg-info/PKG-INFO
 feed2twitter.egg-info/SOURCES.txt
 feed2twitter.egg-info/dependency_links.txt
--- a/feed2twitter/feed2twitter/__init__.py	Wed Aug 19 15:03:07 2009 -0300
+++ b/feed2twitter/feed2twitter/__init__.py	Wed Aug 19 16:09:48 2009 -0300
@@ -21,13 +21,13 @@
 import re
 import time
 from pprint import pprint
-from tinyurl import tiny
+from shortener import services
 from parsetime import parsetime
 from httplib import BadStatusLine
 from urllib2 import HTTPError
 
 class Feed2Twitter(object):
-    def __init__(self,  url, username, passwd, mode='title', items = 5):
+    def __init__(self,  url, username, passwd, mode='title', items = 5, shortener='tinyurl'):
         self.mode = mode
         self.url = url
         self.items = items
@@ -37,9 +37,10 @@
         self.twApi = twitter.Api(username=self.username, password=self.passwd)
         self.set_attr_headers()
         self.rss = readrss.parse(url)
+        self.shortener = services[shortener]()
     
     def set_attr_headers(self):
-        self.twApi.SetXTwitterHeaders('feed2twitter', 'http://www. assembla.com/spaces/feed2twitter', self.version)    
+        self.twApi.SetXTwitterHeaders('feed2twitter', 'http://www.assembla.com/spaces/feed2twitter', self.version)    
 
     def strip_tags(self,value):
         "Return the given HTML with all tags stripped."
@@ -50,7 +51,7 @@
         oldItems=pItems=0
         for it in list(items):
             if self.mode == 'title':
-                txt=it["title"][0:114] +" "+tiny(it["link"])
+                txt=it["title"][0:114] +" "+self.shortener.short(it["link"])
             elif self.mode == 'text':
                 try:
                     txt = self.strip_tags(it.content[0].value)[0:140]
@@ -105,39 +106,53 @@
 
 #setattr(twitter.Api, PostUpdate.__name__, PostUpdate)
 
-def update():
-    opt = Options()
-    options = opt.parse()
-    conf = ConfigParser()
-    if options.sample_config:
-        from pkg_resources import resource_string
-        foo_config = resource_string(__name__, '../docs/default.cfg.sample')
-        print(foo_config)
-        sys.exit(1)
-    try:
-        fd =  open(options.config_filename, 'r')
-    except IOError:
-        print >>sys.stderr, "File %s not found" % configfile
-        sys.exit(2)
-    conf.readfp(fd)
-
+def update(**kwargs):
+    if not kwargs:
+        opt = Options()
+        options = opt.parse()
+        conf = ConfigParser()
+        if options.sample_config:
+            from pkg_resources import resource_string
+            foo_config = resource_string(__name__, '../docs/default.cfg.sample')
+            print(foo_config)
+            sys.exit(1)
+        try:
+            fd =  open(options.config_filename, 'r')
+        except IOError:
+            print >>sys.stderr, "File %s not found" % configfile
+            sys.exit(2)
+            conf.readfp(fd)
 
-    url = conf.get("global", "url").strip()
-    username = conf.get("global", "username").strip()
-    password = conf.get("global", "password").strip()
-    mode = conf.get("global","mode").strip()
-    try:
-        items = int(conf.get("global","items").strip())
-    except NoOptionError:
+            url = conf.get("global", "url").strip()
+            username = conf.get("global", "username").strip()
+            password = conf.get("global", "password").strip()
+            mode = conf.get("global","mode").strip()
+            try:
+                items = int(conf.get("global","items").strip())
+            except NoOptionError:
+                items = 5
+
+            try:
+                interval = parsetime(conf.get("global","interval").strip())
+            except:
+                print('Error in the interval setting')
+                sys.exit(1)
+
+            try:
+                shortener = conf.get('global','shortener').strip()
+            except NoOptionError:
+                shortener = 'tinyurl'
+    else:
+        url = kwargs['url']
+        username = kwargs['username']
+        password = kwargs['password']
+        mode = kwargs['mode']
         items = 5
-    try:
-        interval = parsetime(conf.get("global","interval").strip())
-    except:
-        print('Error in the interval setting')
-        sys.exit(1)
-    print(interval)
+        shortener = kwargs['shortener']
+        interval = parsetime('00:05')
+
     while True:
-        feed2tw = Feed2Twitter(url, username, password, mode, items)
+        feed2tw = Feed2Twitter(url, username, password, mode, items, shortener)
         from distutils import version
         if version.LooseVersion(twitter.__version__) > version.LooseVersion('0.5'):
             feed2tw.twApi.SetSource('feed2twitter')
@@ -146,5 +161,5 @@
         feed2tw.update()
         time.sleep(interval)
 
-__version__ = '0.6.4'
+__version__ = '0.6.5'
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/feed2twitter/feed2twitter/shortener.py	Wed Aug 19 16:09:48 2009 -0300
@@ -0,0 +1,57 @@
+# feed2twitter
+# Copyright (C) 2008 Walter Cruz <walter@waltercruz.com>
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU Affero General Public License for more details.
+
+# You should have received a copy of the GNU Affero General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+import urllib
+from xml.dom.minidom import parseString
+
+class TinyURL(object):
+    def short(self,url):
+        try:
+            params = urllib.urlencode({'url':url})
+            fp = urllib.urlopen("http://tinyurl.com/api-create.php", params)
+            return fp.read()
+
+        except IOError, e:
+            raise "urllib error."
+
+class IsGD(object):
+    def short(self,url):
+        try:
+            params = urllib.urlencode({'longurl':url})
+            url = "http://is.gd/api.php?%s" % params
+            fp = urllib.urlopen(url)
+            return fp.read()
+
+        except IOError, e:
+            raise "urllib error."
+
+class MigreME(object):
+    def short(self,url):
+        try:
+            params = urllib.urlencode({'url':url})
+            url = "http://migre.me/api.xml?%s" % params
+            fp = urllib.urlopen(url)
+            dom = parseString(fp.read())
+            return dom.getElementsByTagName('migre')[0].childNodes[0].data
+
+        except IOError, e:
+            raise "urllib error."
+
+services = {
+    'tinyurl' : TinyURL,
+    'is.gd' : IsGD,
+    'migre.me' : MigreME
+}
--- a/feed2twitter/feed2twitter/tinyurl.py	Wed Aug 19 15:03:07 2009 -0300
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,26 +0,0 @@
-# feed2twitter
-# Copyright (C) 2008 Walter Cruz <walter@waltercruz.com>
-#
-# This program is free software: you can redistribute it and/or modify
-# it under the terms of the GNU Affero General Public License as published by
-# the Free Software Foundation, either version 3 of the License, or
-# (at your option) any later version.
-#
-# This program is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-# GNU Affero General Public License for more details.
-
-# You should have received a copy of the GNU Affero General Public License
-# along with this program.  If not, see <http://www.gnu.org/licenses/>.
-
-import urllib
-
-def tiny(url):
-    try:
-        params = urllib.urlencode({'url':url})
-        fp = urllib.urlopen("http://tinyurl.com/api-create.php", params)
-        return fp.read()
-
-    except IOError, e:
-      raise "urllib error."
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/feed2twitter/tests/test_shortener.py	Wed Aug 19 16:09:48 2009 -0300
@@ -0,0 +1,39 @@
+# feed2twitter
+# Copyright (C) 2008 Walter Cruz <walter@waltercruz.com>
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU Affero General Public License for more details.
+
+# You should have received a copy of the GNU Affero General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+from feed2twitter.shortener import services
+import feed2twitter
+
+def test_tiny():
+    service = services['tinyurl']()
+    url = service.short('http://google.com')
+    assert('http://tinyurl.com/2tx' == url)
+
+def test_isgd():
+    service = services['is.gd']()
+    url = service.short('http://ebc.com.br/') # I had some trouble testing with google.. looks like it will redirect to some
+    # local version of google according to the IP, generating a different URL 
+    assert('http://is.gd/2oFmH' == url)
+
+def test_migreme():
+    service = services['migre.me']()
+    url = service.short('http://google.com')
+    assert('http://migre.me/5tR0' == url)
+
+def test_twitter():
+    feed2twitter.update(shortener='migre.me',url='',mode='title',username='',password='')
+
+
--- a/feed2twitter/tests/test_tinyurl.py	Wed Aug 19 15:03:07 2009 -0300
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,22 +0,0 @@
-# feed2twitter
-# Copyright (C) 2008 Walter Cruz <walter@waltercruz.com>
-#
-# This program is free software: you can redistribute it and/or modify
-# it under the terms of the GNU Affero General Public License as published by
-# the Free Software Foundation, either version 3 of the License, or
-# (at your option) any later version.
-#
-# This program is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-# GNU Affero General Public License for more details.
-
-# You should have received a copy of the GNU Affero General Public License
-# along with this program.  If not, see <http://www.gnu.org/licenses/>.
-
-from feed2twitter.tinyurl import tiny
-
-def test_google():
-    url = tiny('http://google.com')
-    assert('http://tinyurl.com/2tx' == url)
-