python-twitter-tools / twitter Goto Github PK
View Code? Open in Web Editor NEWPython Twitter API
Home Page: http://mike.verdone.ca/twitter/
License: MIT License
Python Twitter API
Home Page: http://mike.verdone.ca/twitter/
License: MIT License
Need a property to return rate limit remaining value.
The update.py isn't quite working anymore so I'm just adding retweet to the globals by hand until I can possibly fix that file.
This change adds retweet to the POST_ACTIONS
http://github.com/clarkbw/twitter/commit/ea6d9d1815c130a38a7f40b9d31bb17268bcbd98
Error when trying to "follow chrisokc":
Traceback (most recent call last):
File "/usr/local/lib/python2.6/dist-packages/twitter-1.3.1-py2.6.egg/twitter/ircbot.py", line 157, in handle_privmsg
self.follow(conn, evt, args[1])
File "/usr/local/lib/python2.6/dist-packages/twitter-1.3.1-py2.6.egg/twitter/ircbot.py", line 191, in follow
friends = [x['name'] for x in self.twitter.statuses.friends()]
File "/usr/local/lib/python2.6/dist-packages/twitter-1.3.1-py2.6.egg/twitter/api.py", line 114, in call
raise TwitterHTTPError(e, uri, self.format, self.encoded_args)
TwitterHTTPError: Twitter sent status 404 for URL: statuses/friends.json using parameters: ()
details: {"request":"/statuses/friends.json","error":"Not found"}
update.py doesn't uncover that /direct_messages/new.(json|xml)
is a POST action. Should probably find a better way to update functions than to just run update.py
The str method of the class TwitterHTTPError is variable because of the call self.e.fp.read(). self.e.fp is a file-like object so the second read() returns an empty string. I apologise I can't offer a solution.
If I type my code, this error seems to come up.
import twitter twitter_search = twitter.Twitter(domain="search.twitter.com") twitter_search.trends()
Error:
AttributeError: 'module' object has no attribute 'Twitter'
Yesterday it still seemed to work. I use version 1.5.2 since yesterday, because 1.6 doesn't work. "twitter_search.trends()" lead to a 404 page.
ive installed your twitter tool. when i run it :
$ twitter -h
it gives me HTML output !!
im using python 2.6.5
Really the only thing at this point that I'd like to change is the status update time from 120 seconds to 30 or 60 seconds.
It may be handy to have a command line option and config setting that would let one send all output to a pager (less by default, another if specified).
If you have CONSUMER_KEY
and CONSUMER_SECRET
set in your environment, PTT should be able to read from this and automatically use it when setting up the Twitter object.
An alternative would be having a file such as .ptt
in your home directory to read this data from.
Just like the bot can filter out @ replies, it would be great if it could filter out everything but tweets with a certain set of #hashtags. I'd like the bot to follow many people, but avoid the problem of over-loading the channel.
See https://github.com/sixohsix/twitter/blob/master/twitter/stream.py#L21
The buffer size of 1024 can easily cross tweet boundaries. This means that the tweets delivered from the python library are always one tweet behind what the streaming api has delivered to the python library.
A little bit of experimenting and character counting shows that the highest buffer size seems to be 5, as each json encoded tweet ends with a '}\r\n' and tweets are separated by '\r\n'.
When calling
t.statuses.update(status=tweet)
if the "tweet" string is Unicode, which Twitter permits, then the library dies with this exception:
Traceback (most recent call last):
File "/home/brandon/shire-tweets/tweet.py", line 61, in <module>
t.statuses.update(status=tweet)
File "/home/brandon/shire-tweets/v/lib/python2.5/site-packages/twitter-1.2.1-py2.5.egg/twitter/api.py", line 59, in __call__
File "/usr/lib/python2.5/urllib.py", line 1250, in urlencode
v = quote_plus(str(v))
UnicodeEncodeError: 'ascii' codec can't encode character u'\xfb' in position 66: ordinal not in range(128)
If you skip the oauth config or the [twitter] section, the bot complains, even though there is supposed to be a default. I can see that the defaults are being loaded, but on Mac OS X, this doesn't seem to be recognised.
Ideally, the output into IRC would look like:
@nslater I'm trying out Twitter
This mimics the format of many traditional IRC clients, as well as using the @ sign to designate that the nick is a Twitter name, and not a regular IRC nick.
The rest of the bot's output should probably loose the emoticon prefixes too.
Near line 95 of api.py
raise TwitterError(
"Twitter sent status %i for URL: %s.%s using parameters: (%s)\ndetails: %s" %(
e.code, uri, self.format, encoded_kwargs, e.fp.read()))
encoded_kwargs should be self.encoded_kwargs
I will start investigating the issue but figured I would report it. Here is my traceback.
/home/ashc/.virtualenvs/tweetbooks/lib/python2.6/site-packages/twitter/api.pyc in call(self, **kwargs)
142 headers = {}
143 if self.auth:
--> 144 headers.update(self.auth.generate_headers())
145 arg_data = self.auth.encode_params(uriBase, method, kwargs)
146 if method == 'GET':
It started happening on September 27. Not sure if there was a change to the Twitter API or anything.
Ptt is a great piece of software. I couldn't find anything better in python. So, first, thanks for the great application.
I am writing this e-mail because there is a small problem with ptt. It (I think not on purpose) uses the old version of the api. The things still work but we get some problems and some consistency issues. I mean, the developer usually expects it to behave like described in twitter api wiki, but it don't work like that, although it's a dynamic library.
A simple example is when you request a user_timeline, even if you put include_rts argument, you just can't get the retweets you did in your profile.
I was able to detect this issue because I requested count=200 tweets and it was always showing less than 200 tweets. Using twiiter api itself it was working fine and showing the retweets.
There are two problems here:
In a nut shell:
api.twitter.com + version == New api + retweets working (Everything ok)
twitter.com + without any version == Works but have the problems/bugs described above.
api.twitter.com + without any version == Works but have the problems/bugs described above.
twitter.com + version == Doesn't work
So I can fix in client side (implementation) with:
twitter = Twitter(domain="api.twitter.com", api_version='1')
But maybe that should/could be fixed in the library itself:
def init(
self, format="json",
domain="twitter.com", secure=True, auth=None,
api_version=''):
becomes
def init(
self, format="json",
domain="api.twitter.com", secure=True, auth=None,
api_version='1'):
(didn't test this aproach, but I think it will work fine)
Has anybody tried implementing twitter's oauth with this package?
It appears that methods like destroy, which take a tweet ID as the next element in the URI path are not supported, as library calls of this type can't be constructed (illegal python)
sometimes I misspell a word when I set my twitter status, and at the moment I have to open a browser, go to twitter, and delete it which is very cumbersome. Perhaps a 'destroy' action which would require an id. Which would mean that the 'set' action would need to return the id created I suppose maybe only on -f verbose?.
It would be great if we could run a bot in the CouchDB IRC channel, with the bot following the #couchdb hashtag. I should imagine there a number of other use-cases for the bot following a search, or set of searches.
if you try something like
twitter -r | grep something
you won't get output for a long time because it stays in the buffer
To get output after each new line add
sys.stdout.flush()
after printing a line (in printNicely?)
Thanks.
I am getting the following:
TwitterHTTPError: Twitter sent status 404 for URL: 1/statuses/friends_timeline.json using parameters: (oauth_consumer_key=[...]&oauth_nonce=[...]&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1275237152&oauth_token=[...]&oauth_version=1.0&oauth_signature=[...])
See https://dev.twitter.com/blog/changing-trends-api for an overview of the new API that is no longer compatible with this Python package (AFAICT.)
In short, the previous Twitter(...).trends() approach no longer works, and a call of the form http://api.twitter.com/1/trends/1.json doesn't seem to be compatible with the design philosophy that's worked so well up till this point. We've previously brainstormed some ideas in #24 that might be worth revisiting here considering that there does seem to be a gravitation towards this particular scheme of encoding parameters as part of the URL itself.
I'm happy to help work out a patch if we can come to agreement on what should be done here. As it stands, some new errata has been introduced into my book/code for Mining the Social Web, which uses this package extensively, so I'd like to help push though something quickly. Thanks.
Hi Mike,
I was using the file https://github.com/ptwobrussell/Mining-the-Social-Web/blob/master/python_code/friends_followers__get_friends.py, which uses your Twitter wrapper. When I run it, I get the error below (file paths reduced for brevity):
Traceback (most recent call last):
File ".../friends_followers__get_friends.py", line 20, in
CONSUMER_KEY, CONSUMER_SECRET)
File ".../oauth_dance.py", line 26, in oauth_dance
twitter.oauth.request_token())
File ".../api.py", line 155, in call
return wrap_response(str(handle.read()), handle.headers)
File ".../api.py", line 82, in wrap_response
return WrappedTwitterResponse(response, headers)
TypeError: str() takes at most 1 argument (2 given)
Using the debugger in Pydev, I get the following values for the relevant variables:
The value of the variable 'response'
str: oauth_token=YUq4v1d4qDTdS6wUrEV2LsrhNKJlqMCOEqTWKvciM&oauth_token_secret=k3dhogHMSK2AHpcdEL8BD6rscyifugdemugGbM3A&oauth_callback_confirmed=true
The value of the variable 'response_typ'
type: <type 'str'>
As a workaround, I replaced "return WrappedTwitterResponse(response, headers)" with "return response" in line 82 of api.py and it ran fine.
I wanted to bring this to your attention with the hopes that you can remedy the issue without us having to use a workaround.
Thanks!
When trying to get the twitterbot set up, I get various errors relating to the (apparently) path of the oauth file, and its contents.
I figured out it wont accept a path for the file, only a name, and it must be in the pwd.
Once I enter the PIN, I get:
That's it! Your authorization keys have been written to .twitterbot_oauth.
Traceback (most recent call last):
File "/usr/local/bin/twitterbot", line 9, in
load_entry_point('twitter==1.6.1', 'console_scripts', 'twitterbot')()
File "/usr/local/lib/python2.6/dist-packages/twitter-1.6.1-py2.6.egg/twitter/ircbot.py", line 352, in main
return bot.run()
File "/usr/local/lib/python2.6/dist-packages/twitter-1.6.1-py2.6.egg/twitter/ircbot.py", line 287, in run
self._irc_connect()
File "/usr/local/lib/python2.6/dist-packages/twitter-1.6.1-py2.6.egg/twitter/ircbot.py", line 281, in _irc_connect
self.config.get('irc', 'nick'))
File "/usr/local/lib/python2.6/dist-packages/python_irclib-0.4.8-py2.6.egg/irclib.py", line 439, in connect
raise ServerConnectionError, "Couldn't connect to socket: %s" % x
irclib.ServerConnectionError: Couldn't connect to socket: [Errno -5] No address associated with hostname
Further attempts to invoke "twitterbot" fail.
If i remove the oauth file, everything works again.
is it just me or are the calls to the trending API going to the old http://search.twitter.com/trends.json page. Looks like this is deprecated. I the script via easy_install and I traced the code back to this URL but the URL no longer exists. Anyone else getting this? This happens when i use this method twitter_search.trends()
I often like to view an individuals timeline, be it someone else, or even my own account. So I wonder if a 'timeline' action could be added to the command line tool?
If a tweet includes an @ for my username anywhere, I'd like to have that show up when I get my updates, and not just if it's from a person I'm already following. If this modification has already been made within the last few months, awesome. But those such updates were not appearing when I was playing around with this tool earlier this year.
To replicate, call:
twitterConnect = twitter.Twitter(auth=twitter.oauth.OAuth('','',self.CONSUMER_KEY,self.CONSUMER_SECRET),api_version=None,format='') oauth_token, oauth_token_secret = twitter.oauth_dance.parse_oauth_tokens(twitterConnect.oauth.request_token())
Result:
/usr/local/lib/python2.6/dist-packages/twitter-1.6.1-py2.6.egg/twitter/api.py:83: DeprecationWarning: object.__init__() takes no parameters
This is line 83 of api.py: response_typ.__init__(self, response)
This gives a syntax error in Python 3 and a deprecation warning in Python 2.6. I don't see any reason to pass the class to __init__()
. This fixes it:
response_typ.__init__(response)
The irc channel I'd like to use the twitter bot with is protected by a channel key, and there doesn't appear to be any way to provide a channel key in the bot configuration.
It's not completely obvious how to construct a request where a parameter is embedded into the URL. Could be good to add an example to the doc string that demonstrates.
My 'twitterbot' just quit IRC with the message (Ping timeout: 241). When I fired up SSH and looked at the console where the bot lives, I saw the following:
File "/usr/local/lib/python2.7/dist-packages/twitter-1.5.2-py2.7.egg/twitter/ircbot.py", line 127, in run_forever
self.next_task()
File "/usr/local/lib/python2.7/dist-packages/twitter-1.5.2-py2.7.egg/twitter/ircbot.py", line 122, in next_task
task()
File "/usr/local/lib/python2.7/dist-packages/twitter-1.5.2-py2.7.egg/twitter/ircbot.py", line 106, in call
return self.task()
File "/usr/local/lib/python2.7/dist-packages/twitter-1.5.2-py2.7.egg/twitter/ircbot.py", line 185, in check_statuses
IRC_BOLD, text.decode('utf-8')))
File "/usr/local/lib/python2.7/dist-packages/twitter-1.5.2-py2.7.egg/twitter/ircbot.py", line 233, in privmsg_channels
return self.ircServer.privmsg_many(channels, msg.encode('utf-8'))
File "/usr/local/lib/python2.7/dist-packages/irclib.py", line 761, in privmsg_many
self.send_raw("PRIVMSG %s :%s" % (",".join(targets), text))
File "/usr/local/lib/python2.7/dist-packages/irclib.py", line 781, in send_raw
raise ServerNotConnectedError, "Not connected."
irclib.ServerNotConnectedError: Not connected.
There has to be some way for either the bot to reconnect itself to the server after it realizes that 'hey, I can't talk to the server anymore' instead of the process just terminating.
When using the JSON data format, all response data is unavailable because by forcing the json response dictionary into a list, only the dictionary keys remain. All the data resides in the dictionary values, which are discarded.
One possible solution is to derive TwitterJsonResponse from dict instead of list.
It only happens occasionally, but sometimes tweets are repeated in the channel. Might it be possible to keep an in memory record of what has and hasn't been seen, dropping any duplicate items. You'd want to remove items after a certain time, so that memory didn't swell for long running bots.
The bot is randomly dying (irc error message is Remote host closed the connection) and when I check the console, I see the following:
Traceback (most recent call last):
File "/usr/local/bin/twitterbot", line 9, in
load_entry_point('twitter==1.6', 'console_scripts', 'twitterbot')()
File "/usr/local/lib/python2.7/dist-packages/twitter/ircbot.py", line 345, in main return bot.run()
File "/usr/local/lib/python2.7/dist-packages/twitter/ircbot.py", line 288, in run self.sched.run_forever()
File "/usr/local/lib/python2.7/dist-packages/twitter/ircbot.py", line 132, in run_forever self.next_task()
File "/usr/local/lib/python2.7/dist-packages/twitter/ircbot.py", line 127, in next_task task()
File "/usr/local/lib/python2.7/dist-packages/twitter/ircbot.py", line 111, in call return self.task()
File "/usr/local/lib/python2.7/dist-packages/twitter/ircbot.py", line 178, in check_statuses update['text'].replace('\n', ' '))
File "/usr/local/lib/python2.7/dist-packages/twitter/util.py", line 19, in htmlentitydecode lambda m: chr(name2codepoint[m.group(1)]), s)
File "/usr/lib/python2.7/re.py", line 151, in sub return _compile(pattern, flags).sub(repl, string, count)
File "/usr/local/lib/python2.7/dist-packages/twitter/util.py", line 19, in lambda m: chr(name2codepoint[m.group(1)]), s)
ValueError: chr() arg not in range(256)
When the bot receives more than one tweet in a batch, it outputs them in chronological order. In a Twitter client, this makes sense. However, on IRC, it should be reversed so that older messages are sent before newer messages.
I recently installed a new PTT and I get a notice saying,
"""
Hi there! We're gonna get you all set up to use Twitter on the
command-line.
In the web browser window that opens please choose to Allow access to
the
command-line tool. Copy the PIN number that appears on the next page
and
paste or type it here:
Please type the PIN:
"""
The thing is: no browser window opened even though I have firefox
installed. I'm running Python 2.6.5 and twitter-1.3.1 on top of
FreeBSD 8.
Any help will be highly appreciated.
Thanks!
Hi,
i found a bug when attempting to do a search using the library:
import twitter
search_twitter = twitter.Twitter(domain="search.twitter.com")
results = search_twitter.search(q="japan")
Returns the following error:
File "", line 1, in
File "/usr/local/lib/python2.6/dist-packages/twitter-1.6-py2.6.egg/twitter/api.py", line 150, in call
return self._handle_response(req, uri, arg_data)
File "/usr/local/lib/python2.6/dist-packages/twitter-1.6-py2.6.egg/twitter/api.py", line 165, in _handle_response
raise TwitterHTTPError(e, uri, self.format, arg_data)
twitter.api.TwitterHTTPError: Twitter sent status 404 for URL: 1/search.json using parameters: (q=japan)
details:
You may have mistyped the address or the page may have moved.
Notice the line : twitter.api.TwitterHTTPError: Twitter sent status 404 for URL: 1/search.json using parameters: (q=japan)
It seems that the library is appending the version number for Twitter search.
This has been changed; http://search.twitter.com/1/search.json?q=@noradio
returns a page not found error.
How do we fix this?
Hi Mike,
I just discovered what seem to be a regression in 1.3.1 and couldn't see an obvious bug-tracker for the project.
Consider:
twit = twitter.api.Twitter(username, password)
twit.statuses.friends(id='some_user')
in 1.2.1 this works, but in 1.3.1 it fails. In 1.3.1 you must pass an api_version=1 param to the constructor to make it work, causing the /1 to appear in the URL. I noticed that 1.2.1 made no attempt to include the /1 but still works - the only difference I can see if that 1.2.1 hit twitter.com directly, whereas 1.3.1 hits api.twitter.com - and the latter seems to want the '/1' for at least some API calls - but not all.
I wonder if this is by intent, and if not, then wonder if defaulting api_version to 1 might be appropriate?
Cheers,
Mark
The oauth file config should use the os.path.expanduser method to expand filenames starting with "~"
Just upgraded from 1.5.2 to 1.6 and I'm getting some errors while running the twitterbot. The bot will be running on IRC with no problems and then dies off and here is what I see in the console:
Traceback (most recent call last):
File "/usr/local/bin/twitterbot", line 9, in
load_entry_point('twitter==1.6', 'console_scripts', 'twitterbot')()
File "/usr/local/lib/python2.7/dist-packages/twitter/ircbot.py", line 349, in main
return bot.run()
File "/usr/local/lib/python2.7/dist-packages/twitter/ircbot.py", line 292, in run
self.sched.run_forever()
File "/usr/local/lib/python2.7/dist-packages/twitter/ircbot.py", line 132, in run_forever
self.next_task()
File "/usr/local/lib/python2.7/dist-packages/twitter/ircbot.py", line 127, in next_task
task()
File "/usr/local/lib/python2.7/dist-packages/twitter/ircbot.py", line 111, in call
return self.task()
File "/usr/local/lib/python2.7/dist-packages/twitter/ircbot.py", line 189, in check_statuses
IRC_BOLD, text.decode('utf-8')))
File "/usr/local/lib/python2.7/dist-packages/twitter/ircbot.py", line 236, in privmsg_channels
return self.ircServer.privmsg_many(channels, msg)
File "/usr/local/lib/python2.7/dist-packages/irclib.py", line 761, in privmsg_many
self.send_raw("PRIVMSG %s :%s" % (",".join(targets), text))
File "/usr/local/lib/python2.7/dist-packages/irclib.py", line 783, in send_raw
self.socket.send(string + "\r\n")
UnicodeEncodeError: 'ascii' codec can't encode character u'\u2019' in position 51: ordinal not in range(128)
I'm looking for a new python twitter library that supports OAuth. This library looks good, but I can't find any documentation on how to connection to twitter with oauth and post a tweet. A short and sweet example would go a long ways in helping folks use this library.
In 1.4.2 the call to twitter.friendships.exist returns a boolean which throws exception:
'bool' object is not iterable
This is the error I am having:
Python 2.6.5
weetabix@boxname:~/twitterbot$ twitterbot
Traceback (most recent call last):
File "/usr/local/bin/twitterbot", line 9, in
load_entry_point('twitter==1.6.1', 'console_scripts', 'twitterbot')()
File "/usr/lib/python2.6/dist-packages/pkg_resources.py", line 299, in load_entry_point
return get_distribution(dist).load_entry_point(group, name)
File "/usr/lib/python2.6/dist-packages/pkg_resources.py", line 2229, in load_entry_point
return ep.load()
File "/usr/lib/python2.6/dist-packages/pkg_resources.py", line 1948, in load
entry = import(self.module_name, globals(),globals(), ['name'])
ImportError: No module named ircbot
Any suggestions?
I wanted to see if you could possibly add a future feature to the
Twitterbot. I was using an older bot written in Perl, which doesn't
work any more due to some Twitter changes (OAuth) which only posted X
tweets to IRC every X minutes. I see right now I can change the time
that it checks for updates (default is 120 seconds), but the bot just
sends every tweet it finds every 120 seconds, which could lead to the
bot getting disconnected or banned from an IRC server for flooding.
Thanks again for your hard work on this bot Mike.
Regards,
Brian 'geeknik' Carpenter
http://www.geeknik.com/
If you try to do
twitter -c configfile set "Status ç á é i ô ê a"
You get:
Traceback (most recent call last):
File "/usr/local/bin/twitter", line 9, in
load_entry_point('twitter==1.6.1', 'console_scripts', 'twitter')()
File "/usr/local/lib/python2.6/dist-packages/twitter-1.6.1-py2.6.egg/twitter/cmdline.py", line 597, in main
Action()(twitter, options)
File "/usr/local/lib/python2.6/dist-packages/twitter-1.6.1-py2.6.egg/twitter/cmdline.py", line 351, in call
doAction()
File "/usr/local/lib/python2.6/dist-packages/twitter-1.6.1-py2.6.egg/twitter/cmdline.py", line 345, in
doAction = lambda : action(twitter, options)
File "/usr/local/lib/python2.6/dist-packages/twitter-1.6.1-py2.6.egg/twitter/cmdline.py", line 461, in call
twitter.statuses.update(status=statusTxt)
File "/usr/local/lib/python2.6/dist-packages/twitter-1.6.1-py2.6.egg/twitter/api.py", line 145, in call
arg_data = self.auth.encode_params(uriBase, method, kwargs)
File "/usr/local/lib/python2.6/dist-packages/twitter-1.6.1-py2.6.egg/twitter/oauth.py", line 63, in encode_params
enc_params = urlencode_noplus(sorted(params.items()))
File "/usr/local/lib/python2.6/dist-packages/twitter-1.6.1-py2.6.egg/twitter/oauth.py", line 95, in urlencode_noplus
v = v.encode('utf-8')
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 7: ordinal not in range(128)
I'm using Ubuntu 10.04. And I discovered this bug using the library itself. But just to prove it was a library related and not a bug in my program a used the official twitter in terminal that comes with the library
If I can help with anything else, just tell! :)
Thanks!
Currently using python-oauth2 to take care of my Twitter authentication, but with the addition of OAuth to this library, I figured I could consolidate and get rid of python-oauth2. However, I'm having a bit of trouble with the conversion.
Any way an example can be provided of exactly when to use the OAuth object in the authentication process?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.