# HG changeset patch # User Walter Cruz # Date 1231171995 7200 # Node ID e8660f0d3b97b62341b3e518fff545365426426a # Parent 56e57a84cb4ae93c61b9f393f7fa1bf745f29e43 Adding proper support for python-twitter 0.5 and 0.6 diff -r 56e57a84cb4a -r e8660f0d3b97 feed2twitter/README --- a/feed2twitter/README Tue Sep 23 13:55:21 2008 -0300 +++ b/feed2twitter/README Mon Jan 05 14:13:15 2009 -0200 @@ -17,8 +17,7 @@ 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 create a configuration file. You can see a sample with the command *feed2twitter -p*. It will looks like: :: @@ -28,11 +27,12 @@ password = twitter_password mode = text interval = 01:00 + items = 5 -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. +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. In the sample, feed2twitter will check the provided feed each hour. -Run it with the command feed2twitter. It will update your twitter with your latest 5 posts, in the interval that you have set. +Run it with the command feed2twitter. It will update your twitter with your latest 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. -You can contact me at walter@waltercruz.com. Have fun! \ No newline at end of file +You can contact me at walter@waltercruz.com. Have fun! diff -r 56e57a84cb4a -r e8660f0d3b97 feed2twitter/README.pt --- a/feed2twitter/README.pt Tue Sep 23 13:55:21 2008 -0300 +++ b/feed2twitter/README.pt Mon Jan 05 14:13:15 2009 -0200 @@ -1,25 +1,38 @@ +============ +Feed2Twitter +============ + O feed2twitter atualiza uma conta qualquer no Twitter a partir de um feed xml. É uma alternativa livre à serviços como o Twitterfeed. -Atualmente, está em versão beta. Para instalar o feed2twitter: -instale o python-twitter: - http://code.google.com/p/python-twitter/ -instale o feedparser: - http://www.feedparser.org/ + +1. instale o python-twitter: http://code.google.com/p/python-twitter/ + +1. instale o feedparser: http://www.feedparser.org/ Instale o feed2twitter como comando: python setup.py install -Executando: - você precisará configurar o seu feed e sua conta do twitter. - Crie uma pasta chamada .feed2twitter no $HOME do usuário que irá executar o feed2twitter - Adicione um arquivo chamado default.cfg, como o modelo: +Executando +---------- + +* Você precisará configurar o seu feed e sua conta do twitter. +* Você precisa criar um arquivo de configuração. Você pode ver um exemplo com o comando *feed2twitter -p*. Será parecido com isso: + + :: -[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 + items = 5 -Execute o comando feed2twitter. Isso irá atualizar o seu twitter com as últimas 5 entradas no feed, hora a hora. +A url é a url do feed que você quer publicar, o modo pode ser 'text' (140 caracteres do seu texto) ou 'title' (O título do seu post, com um link). O interval é o intervalo no qual o feed2twitter irá publicar seus últimos posts. No exemplo, feed2twitter irá checar o seu feed a cada hora. + +Execute o comando feed2twitter. Isso irá atualizar o seu twitter com as últimas entradas no feed, no intervalo que você configurou. Obrigado ao Gleicon da Silveira (http://zenmachine.wordpress.com/about/), de quem tomei emprestado um trecho de código. + +Você pode me contatar em walter@waltercruz.com. Divirta-se! diff -r 56e57a84cb4a -r e8660f0d3b97 feed2twitter/VERSION --- a/feed2twitter/VERSION Tue Sep 23 13:55:21 2008 -0300 +++ b/feed2twitter/VERSION Mon Jan 05 14:13:15 2009 -0200 @@ -1,1 +1,1 @@ -0.4.1.2 \ No newline at end of file +0.6 \ No newline at end of file diff -r 56e57a84cb4a -r e8660f0d3b97 feed2twitter/docs/default.cfg.sample --- a/feed2twitter/docs/default.cfg.sample Tue Sep 23 13:55:21 2008 -0300 +++ b/feed2twitter/docs/default.cfg.sample Mon Jan 05 14:13:15 2009 -0200 @@ -3,4 +3,5 @@ username = twitter_user password = twitter_password mode = text -interval = 01:00 \ No newline at end of file +interval = 01:00 +items = 1 \ No newline at end of file diff -r 56e57a84cb4a -r e8660f0d3b97 feed2twitter/feed2twitter.egg-info/PKG-INFO --- a/feed2twitter/feed2twitter.egg-info/PKG-INFO Tue Sep 23 13:55:21 2008 -0300 +++ b/feed2twitter/feed2twitter.egg-info/PKG-INFO Mon Jan 05 14:13:15 2009 -0200 @@ -1,6 +1,6 @@ Metadata-Version: 1.0 Name: feed2twitter -Version: 0.4.1.2 +Version: 0.5dev Summary: Publish your feed items to twitter Home-page: http://www.assembla.com/spaces/feed2twitter Author: Walter Cruz @@ -25,8 +25,7 @@ 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 create a configuration file. You can see a sample with the command *feed2twitter -p*. It will looks like: :: @@ -36,14 +35,16 @@ password = twitter_password mode = text interval = 01:00 + items = 5 - 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. + 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. In the sample, feed2twitter will check the provided feed each hour. - Run it with the command feed2twitter. It will update your twitter with your latest 5 posts, in the interval that you have set. + Run it with the command feed2twitter. It will update your twitter with your latest 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. You can contact me at walter@waltercruz.com. Have fun! + Keywords: twitter feed Platform: UNKNOWN Classifier: Intended Audience :: Developers diff -r 56e57a84cb4a -r e8660f0d3b97 feed2twitter/feed2twitter/__init__.py --- a/feed2twitter/feed2twitter/__init__.py Tue Sep 23 13:55:21 2008 -0300 +++ b/feed2twitter/feed2twitter/__init__.py Mon Jan 05 14:13:15 2009 -0200 @@ -27,13 +27,18 @@ from urllib2 import HTTPError class Feed2Twitter(object): - def __init__(self, url, username, passwd, mode='title'): + def __init__(self, url, username, passwd, mode='title', items = 5): self.mode = mode - self.url=url - self.username=username - self.passwd=passwd - self.twApi=twitter.Api(username=self.username, password=self.passwd) - self.twApi.SetSource('feed2twitter') + self.url = url + self.items = items + self.username = username + self.passwd = passwd + from pkg_resources import resource_string + self.version = resource_string(__name__, '../VERSION') + self.twApi = twitter.Api(username=self.username, password=self.passwd) + self.twApi.SetXTwitterHeaders('feed2twitter', 'http://www. assembla.com/spaces/feed2twitter', self.version) +# self.twApi.parameters['source'] = 'feed2twitter' +# self.twApi._request_headers['source'] = 'feed2twitter' self.set_attr_headers() self.rss = readrss.parse(url) @@ -72,13 +77,39 @@ def update(self): lastread = self.rss.getlastRead() if not lastread: - self.twitIt(reversed(self.rss.feed['items'][:5])) + self.twitIt(reversed(self.rss.feed['items'][:self.items])) else: try: lista = [item for item in self.rss.feed['items'] if item['published_parsed'] > lastread] except: lista = [item for item in self.rss.feed['items'] if item['updated_parsed'] > lastread] - self.twitIt(reversed(lista[:5])) + self.twitIt(reversed(lista[:self.items])) + +def PostUpdate(self, text): + from twitter import Status + from twitter import TwitterError + import simplejson + '''Post a twitter status message from the authenticated user. + + The twitter.Api instance must be authenticated. + + Args: + text: The message text to be posted. Must be less than 140 characters. + + Returns: + A twitter.Status instance representing the message posted + ''' + if not self._username: + raise TwitterError("The twitter.Api instance must be authenticated.") + if len(text) > 140: + raise TwitterError("Text must be less than or equal to 140 characters.") + url = 'http://twitter.com/statuses/update.json' + data = {'status': text,'source':'feed2twitter'} + json = self._FetchUrl(url, post_data=data) + data = simplejson.loads(json) + return Status.NewFromJsonDict(data) + +#setattr(twitter.Api, PostUpdate.__name__, PostUpdate) def update(): opt = Options() @@ -89,7 +120,6 @@ foo_config = resource_string(__name__, '../docs/default.cfg.sample') print(foo_config) sys.exit(1) - configfile =os.path.expanduser('~/.feed2twitter/default.cfg') try: fd = open(options.config_filename, 'r') except IOError: @@ -103,12 +133,21 @@ 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) print(interval) while True: - feed2tw = Feed2Twitter(url, username, password, mode) + feed2tw = Feed2Twitter(url, username, password, mode, items) + from distutils import version + if version.LooseVersion(twitter.__version__) > version.LooseVersion('0.5'): + feed2tw.twApi.SetSource('feed2twitter') + else: + setattr(twitter.Api, PostUpdate.__name__, PostUpdate) feed2tw.update() time.sleep(interval) diff -r 56e57a84cb4a -r e8660f0d3b97 feed2twitter/feed2twitter/readrss.py --- a/feed2twitter/feed2twitter/readrss.py Tue Sep 23 13:55:21 2008 -0300 +++ b/feed2twitter/feed2twitter/readrss.py Mon Jan 05 14:13:15 2009 -0200 @@ -42,7 +42,10 @@ pickle.dump(self.feed['items'][0][key], output) else: if 'published_parsed' in item: - pickle.dump(item[key], output) + key = 'published_parsed' + else: + key = 'updated_parsed' + pickle.dump(item[key], output) output.close() def getlastRead(self):