| Reply | « Previous Thread | Next Thread » |
|
Join Date: Nov 2007
Posts: 319
Location: Sertaozinho/Brazil
marcelobarrosalmeida
Online
Forum Nokia Champion
|
|
During the last days I have been consuming my time trying to access Twitter using Python for S60 (with proxy support). Although there are some Python APIs for Twitter, I did not see any easy way to use them in S60. In general, they have many dependencies and need newer versions of modules for running. Maybe Python for S60 1.9.1 can be used with few changes, but the version 1.4.5 (which I use) is much more limited.
I just finished a small demo using only modules urllib and base64! With this demo is possible to get the user timeline and submit an update, both with support for proxy. It is easy to add new functions if you know Twitter API. It is also possible to use http://is.gd or http://tinyurl.com services to generate smaller URLs. My JSON decoding is "partial" (read: big and durty workaround) and needs (many) improvements. I even tried to use pys60 json.py but it not working with twitter answers and I am too tired to tweak it more. Examples, to cheer you, before showing the code. First, printing my timeline (no proxy): Code:
# Username and password - Twitter
tw = TwitterApi('marcelobarros','*senha*')
ftl = tw.get_friends_timeline()
for f in ftl:
print '-'*60
print f['user']['name']
print f['created_at']
print f['text']
Code:
> tw = TwitterApi('marcelobarros','*senha*')
> print tw.tinyfy_url('http://jedizone.wordpress.com/2009/02/01/'+
'a-nova-moda-e-virar-tijolo-agora-o-acer-aspire-one/')
http://is.gd/i2af
Need proxy? Just specify it in the known format Code:
http://user:password@proxy:port Code:
tw = TwitterApi('usr_twitter','pwd_twitter',
'http://usr_prx:pwd_prx@ip_do_prx:port_do_prx')
My main idea is to use this code to add in twitter all headlines created by my wordpress client (wordmobi). But even Rafael may use it to add Genius high scores in twitter ;-) I want to write an article ASAP as well. Thanks ! Marcelo Barros s60twitter.py Code:
# -*- coding: utf-8 -*-
# Marcelo Barros de Almeida
# marcelobarrosalmeida (at) gmail.com
#
import base64
import urllib
from urllib import unquote, splittype, splithost
class _FancyURLopener(urllib.FancyURLopener):
""" This class handles basic auth, providing user and password
when required by twitter
"""
def __init__(self, usr, pwd, prx={}):
""" Set default values for local proxy (if any)
and set user/password for twitter
"""
urllib.FancyURLopener.__init__(self,prx)
self.usr = usr
self.pwd = pwd
def prompt_user_passwd(self, host, realm):
""" Basic auth callback
"""
return (self.usr,self.pwd)
class TwitterApi(object):
""" Simple class for twitter update with proxy support
"""
def __init__(self, tw_usr, tw_pwd, proxy=""):
""" Set default values for local proxy (if any)
and set user/password for twitter
"""
self.proxyurl = proxy
self.user_agent = "urllib/1.0 (urllib)"
self.tw_usr, self.tw_pwd = tw_usr, tw_pwd
self._prepare_urlopener()
def _prepare_urlopener(self):
""" Update twitter status
# http://code.activestate.com/recipes/523016/
"""
if self.proxyurl:
XXX, r_type = splittype(self.proxyurl)
phost, XXX = splithost(r_type)
puser_pass = None
if '@' in phost:
user_pass, phost = phost.split('@', 1)
if ':' in user_pass:
user, password = user_pass.split(':', 1)
puser_pass = base64.encodestring('%s:%s' %
(unquote(user),
unquote(password))).strip()
self.urlopener_proxy = {'http':'http://%s'%phost}
if not puser_pass:
self.headers = [('User-agent', self.user_agent)]
else:
self.headers = [('User-agent', self.user_agent),
('Proxy-authorization', 'Basic ' + puser_pass) ]
else:
self.urlopener_proxy = {}
self.headers = []
def _get_urlopener(self):
""" Return an urlopener with authentication headers and proxy already set
"""
urlopener = _FancyURLopener(self.tw_usr, self.tw_pwd, self.urlopener_proxy)
urlopener.addheaders = self.headers
return urlopener
def update(self, status):
""" Update twitter with new status message
"""
status = 'status=' + status
f = self._get_urlopener().open("http://twitter.com/statuses/update.json", status)
d = f.readlines()[0]
return self.dirty_json_read(d)
def get_friends_timeline(self,page=1,count=20):
""" Return friends timeline for current user
"""
url = 'http://twitter.com/statuses/friends_timeline.json?page=%d&count=%d' % (page,count)
f = self._get_urlopener().open(url)
d = f.readlines()[0]
return self.dirty_json_read(d)
def get_user_timeline(self,page=1,count=20):
""" Return friends timeline for current user
"""
url = 'http://twitter.com/statuses/user_timeline.json?page=%d&count=%d' % (page,count)
f = self._get_urlopener().open(url)
d = f.readlines()[0]
return self.dirty_json_read(d)
def dirty_json_read(self,msg):
""" Converts a json response from twitter in a python object
"""
true = True
false = False
null = None
exec(compile('x='+msg,'<string>','exec'))
return x
def dirty_tinyfy_url(self,page):
""" Creates a tiny url using http://tinyurl.com/ service
"""
params = "url=%s" % page
url = 'http://tinyurl.com/create.php'
f = self._get_urlopener().open(url,params)
rsp = "".join(f.readlines())
b = rsp.find('" target="_blank"')
a = rsp.rfind('"',0,b) + 1
return rsp[a:b]
def tinyfy_url(self,page):
""" Creates a tiny url using http://is.gd/api_info.php service
"""
url = 'http://is.gd/api.php?longurl=%s' % page
f = self._get_urlopener().open(url)
rsp = "".join(f.readlines())
return rsp
Marcelo Barros Nokia E71, N800, N95 and XM 5800 http://www.croozeus.com http://wordmobi.wordpress.com http://jedizone.wordpress.com |
| Reply | « Previous Thread | Next Thread » |
| Thread Tools | Search this Thread |
|---|---|
| Rate This Thread | |
| Thread | Thread Starter | Forum | Replies | Last Post |
|---|---|---|---|---|
| New to Python for S60 (PyS60)? Read this first! | croozeus | Python | 2 | 2008-10-17 20:23 |
| PyS60 Talks #3: Community Edition , Appuifw2 & Featured Apps | bogdan.galiceanu | Python | 12 | 2008-09-23 07:08 |
| PyS60 Talks #2 : PyS60 Extensions | croozeus | Python | 14 | 2008-09-15 15:23 |
| [Announce] Home to Pys60 Developers - croozeus.com | bogdan.galiceanu | Python | 8 | 2008-07-12 20:31 |
| how to distribute pys60 based apps? | chall3ng3r | Python | 4 | 2007-03-27 15:13 |