[csw-devel] SF.net SVN: opencsw:[573] twitter/sweets/sweets.py
skayser at users.sourceforge.net
skayser at users.sourceforge.net
Tue Oct 25 10:44:42 CEST 2011
Revision: 573
http://opencsw.svn.sourceforge.net/opencsw/?rev=573&view=rev
Author: skayser
Date: 2011-10-25 08:44:42 +0000 (Tue, 25 Oct 2011)
Log Message:
-----------
twitter/sweets: add twitter search, explicitly handle encoding on stdout
Modified Paths:
--------------
twitter/sweets/sweets.py
Modified: twitter/sweets/sweets.py
===================================================================
--- twitter/sweets/sweets.py 2011-10-24 14:01:34 UTC (rev 572)
+++ twitter/sweets/sweets.py 2011-10-25 08:44:42 UTC (rev 573)
@@ -13,19 +13,32 @@
#
import json
+import pycurl
import sys
import time
import types
+import StringIO
from optparse import OptionParser,SUPPRESS_HELP
-def twitter_search_mock(query = "", since = ""):
+def twitter_search_mock(query="", since=""):
"""Search twitter (mockup), returns a single-line, JSON search result."""
f = open("sample-search-results.txt", "r")
- data = ''.join(f.readlines()) # ensure single line result
+ data = ''.join(f.readlines()) # ensure single line string
f.close()
return data
-twitter_search = twitter_search_mock
+def twitter_search_real(query, since=""):
+ """Search twitter, returns a single-line, JSON search result."""
+ output=StringIO.StringIO()
+ c = pycurl.Curl()
+ c.setopt(c.URL, 'http://search.twitter.com/search.json?q=' + query)
+ c.setopt(c.WRITEFUNCTION, output.write)
+ c.perform()
+ c.close()
+ return output.getvalue()
+
+twitter_search = twitter_search_real
+
def get_results(query):
"""Trigger twitter search and extract results from JSON response."""
json_data = twitter_search(query)
@@ -35,16 +48,22 @@
def run(interval, query):
"""Run the main loop which queries Twitter and writes the output."""
while 1:
- results = get_results(query)
- for result in results:
- print "%s - http://twitter.com/#!/%s/status/%s" % (
- result['text'],
- result['from_user'],
- result['id_str']
- )
- # Tweet URL #http://twitter.com/#!/<from_user>/status/<id_str>
- if hasattr(interval, "__call__"): interval()
- else: time.sleep(float(interval))
+ try:
+ results = get_results(query)
+ for result in results:
+ # Tweet URL #http://twitter.com/#!/<from_user>/status/<id_str>
+ msg = "%s - http://twitter.com/#!/%s/status/%s" % (
+ result['text'],
+ result['from_user'],
+ result['id_str']
+ )
+ print msg.encode(sys.stdout.encoding, 'replace')
+ if hasattr(interval, "__call__"): interval()
+ else: time.sleep(float(interval))
+ except pycurl.error:
+ print "Couldn't retrieve URL"
+ except json.ReadException:
+ print "Couldn't read JSON response"
if __name__ == '__main__':
usage = "%prog -s <searchterm> -t <secs>"
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
More information about the devel
mailing list