changeset 17:ad16b90dae70

Adding the interval setting
author Walter Cruz <walter@waltercruz.com>
date Wed, 06 Aug 2008 18:29:45 -0300
parents 45000ce6033c
children 26bb4296392a
files feed2twitter/README feed2twitter/docs/default.cfg.sample feed2twitter/feed2twitter/__init__.py feed2twitter/feed2twitter/parsetime.py feed2twitter/scripts/feed2twitter feed2twitter/setup.py feed2twitter/tests/test_parsetime.py feed2twitter/tests/test_readrss.py feed2twitter/tests/test_tinyurl.py feed2twitter/tests/teste_readrss.py feed2twitter/tests/teste_tinyur.py
diffstat 11 files changed, 157 insertions(+), 80 deletions(-) [+]
line wrap: on
line diff
--- a/feed2twitter/README	Tue Aug 05 15:18:39 2008 -0300
+++ b/feed2twitter/README	Wed Aug 06 18:29:45 2008 -0300
@@ -1,24 +1,38 @@
+============
+Feed2Twitter
+============
+
 Feed2twitter updates a twitter account from a xml feed.
 It's a free alternative to services like Twitterfeed.
 
 To install feed2twitter:
-install python-twitter:
-  http://code.google.com/p/python-twitter/
-installfeedparser:
-  http://www.feedparser.org/
+
+1. install python-twitter:  http://code.google.com/p/python-twitter/
 
-install it with: python setup.py install
+1. install feedparser:  http://www.feedparser.org/
+
+
+Install it with: python setup.py install
 
 Running:
-  you need to set your feed and your twitter account.
-  Create a folder under the $HOME of the user that will run feed2twitter called .feed2twitter
-  Add a file called default.cfg, like this:
+--------
+* You need to set your feed and your twitter account.
+* Create a folder under the $HOME of the user that will run feed2twitter called .feed2twitter
+* Add a file called default.cfg, like this:
+
+ ::
 
-[global]
-url = http://example.com/rss
-username = twitter_user
-password = twitter_password
+        [global]
+        url = http://example.com/rss
+        username = twitter_user
+        password = twitter_password
+        mode = text
+        interval = 01:00
 
-Run it with the command feed2twitter. It will update your twitter with your latest 5 posts, hour by hour
+The url is the url for the feed that you want to publish, mode can be 'text' (140 chars of your post text) or 'title' (your post title, with a link). The interval is the interval in which the feed2twitter will publish your last posts.
+
+Run it with the command feed2twitter. It will update your twitter with your latest 5 posts, in the interval that you have set.
 
-Kudos to Gleicon da Silveira (http://zenmachine.wordpress.com/about/), that give me a idea of the code in a post of his blog
+Kudos to Gleicon da Silveira (http://zenmachine.wordpress.com/about/), that give me a idea of the code in a post of his blog.
+
+You can contact me at walter@waltercruz.com. Have fun!
\ No newline at end of file
--- a/feed2twitter/docs/default.cfg.sample	Tue Aug 05 15:18:39 2008 -0300
+++ b/feed2twitter/docs/default.cfg.sample	Wed Aug 06 18:29:45 2008 -0300
@@ -2,3 +2,5 @@
 url = http://example.com/rss
 username = twitter_user
 password = twitter_password
+mode = text
+interval = 01:00
\ No newline at end of file
--- a/feed2twitter/feed2twitter/__init__.py	Tue Aug 05 15:18:39 2008 -0300
+++ b/feed2twitter/feed2twitter/__init__.py	Wed Aug 06 18:29:45 2008 -0300
@@ -21,6 +21,7 @@
 import time
 from pprint import pprint
 from tinyurl import tiny
+from parsetime import parsetime
 from httplib import BadStatusLine
 from urllib2 import HTTPError
 
@@ -61,8 +62,9 @@
                 self.rss.updateLastRead(it)
                 print "status: ", status.text
                 time.sleep(5)
+
             except (BadStatusLine, HTTPError):
-                #lets try in the nex ttime
+                #lets try in the next time
                 return
 
     def update(self):
@@ -74,21 +76,27 @@
             self.twitIt(reversed(lista[:5]))
 
 def update():
-    c = ConfigParser()
+    conf = ConfigParser()
     configfile =os.path.expanduser('~/.feed2twitter/default.cfg')
     try:
         fd =  open(configfile, 'r')
     except IOError:
         print >>sys.stderr, "File %s not found" % configfile
         sys.exit(2)
-    c.readfp(fd)
+    conf.readfp(fd)
 
 
-    url = c.get("global", "url").strip()
-    username = c.get("global", "username").strip()
-    password = c.get("global", "password").strip()
-    mode = c.get("global","mode").strip()
-
-
-    feed2tw = Feed2Twitter(url, username, password, mode)
-    feed2tw.update()
+    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:
+        interval = parsetime(conf.get("global","interval").strip())
+    except:
+        print('Error in the interval setting')
+        sys.exit(1)
+    print(interval)
+    while True:
+        feed2tw = Feed2Twitter(url, username, password, mode)
+        feed2tw.update()
+        time.sleep(interval)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/feed2twitter/feed2twitter/parsetime.py	Wed Aug 06 18:29:45 2008 -0300
@@ -0,0 +1,29 @@
+# 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 re
+from datetime import timedelta
+
+retime = re.compile('([0-9]{2}):([0-5][0-9])')
+
+def parsetime(stime):
+    try:
+        se= retime.match(stime)
+        matches = se.groups()
+        delta = timedelta(hours = int(matches[0]), minutes = int(matches[1]))
+        return delta.days * 24 *60 *60 + delta.seconds
+    except:
+        raise Exception, 'String format error'
--- a/feed2twitter/scripts/feed2twitter	Tue Aug 05 15:18:39 2008 -0300
+++ b/feed2twitter/scripts/feed2twitter	Wed Aug 06 18:29:45 2008 -0300
@@ -1,9 +1,6 @@
 #!/usr/bin/env python
 import feed2twitter
-import time
 
 if __name__=="__main__":
-    while 1:
       feed2twitter.update()
-      time.sleep(3600)
 
--- a/feed2twitter/setup.py	Tue Aug 05 15:18:39 2008 -0300
+++ b/feed2twitter/setup.py	Wed Aug 06 18:29:45 2008 -0300
@@ -1,10 +1,10 @@
 from setuptools import setup, find_packages
 import sys, os
-
-version = '0.2'
+v = open(path.join(path.dirname(__file__), 'VERSION'))
+VERSION = v.readline().strip()
 
 setup(name='feed2twitter',
-      version=version,
+      version=VERSION,
       description="Publish your feed items to twitter",
       long_description=open('README').read(),
       classifiers=[
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/feed2twitter/tests/test_parsetime.py	Wed Aug 06 18:29:45 2008 -0300
@@ -0,0 +1,27 @@
+# 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.parsetime import parsetime
+from nose.tools import raises
+
+def test_parsetime():
+    ti = parsetime('02:00')
+    assert(ti == 7200)
+    ti = parsetime('00:02')
+
+@raises(Exception)
+def test_wrong_format():
+    ti = parsetime('ss:ss');
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/feed2twitter/tests/test_readrss.py	Wed Aug 06 18:29:45 2008 -0300
@@ -0,0 +1,27 @@
+# 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.readrss import parse
+
+def test_feed():
+    url = 'tests/mocks/slashdot.xml'
+    t = open(url).read()
+    f = parse(t)
+    f.updateLastRead()
+    print(f.feed['items'][0]['updated_parsed'])
+    print(f.feed['items'][1]['updated_parsed'])
+    print(f.feed['items'][2]['updated_parsed'])
+    assert(f.getlastRead() == f.feed['items'][0]['updated_parsed'])
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/feed2twitter/tests/test_tinyurl.py	Wed Aug 06 18:29:45 2008 -0300
@@ -0,0 +1,22 @@
+# 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)
+
--- a/feed2twitter/tests/teste_readrss.py	Tue Aug 05 15:18:39 2008 -0300
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,27 +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.readrss import parse
-
-def test_feed():
-    url = 'tests/mocks/slashdot.xml'
-    t = open(url).read()
-    f = parse(t)
-    f.updateLastRead()
-    print(f.feed['items'][0]['updated_parsed'])
-    print(f.feed['items'][1]['updated_parsed'])
-    print(f.feed['items'][2]['updated_parsed'])
-    assert(f.getlastRead() == f.feed['items'][0]['updated_parsed'])
--- a/feed2twitter/tests/teste_tinyur.py	Tue Aug 05 15:18:39 2008 -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)
-