 |
Linux Format forums Help, discussion, magazine feedback and more
|
| View previous topic :: View next topic |
| Author |
Message |
TwIsT
Joined: Tue May 17, 2005 9:05 pm Posts: 38
|
Posted: Sat Nov 26, 2011 4:44 pm Post subject: Python Help |
|
|
Hi Guys,
I'm trying to add my twitter feed to my conky config. To do this I need to use a python script that calls the python twitter API however when I try to run the script from the command line I get a strange Error:
| Code: | File "timeline.py", line 10
api = twitter.Api(consumer_key = CusKey, consumer_secret = CusSec, access_token.key = AccTokKey, access_token.secret = AccTokKeySec)
^
IndentationError: expected an indented block |
The Script is:
| Code: |
from django.utils.encoding import smart_str, smart_unicode
import twitter
CusKey = "XX"
CusSec = "XXX"
AccTokKey = "XXXX"
AccTokKeySec = "XXXXX"
def main():
api = twitter.Api(consumer_key = CusKey, consumer_secret = CusSec, access_token.key = AccTokKey, access_token.secret = AccTokKeySec)
timeline = api.GetFriendsTimeline()
for status in timeline:
print status.user.name.encode(‘ascii’, ‘ignore’), ‘————’, status.text.encode(‘ascii’, ‘ignore’)
if __name__ == ‘__main__’:
main()
|
anyone got any ideas?
Thanks
TwIsT |
|
| Back to top |
|
 |
thameslink
Joined: Fri Dec 11, 2009 11:17 pm Posts: 18
|
Posted: Sat Nov 26, 2011 5:08 pm Post subject: code indentation |
|
|
You have to use indentation in python to delineate blocks of code
so everything under the def main() needs to be indented one level
then everything under the for statement another level and under the if condition another level.
| Code: | from django.utils.encoding import smart_str, smart_unicode
import twitter
CusKey = "XX"
CusSec = "XXX"
AccTokKey = "XXXX"
AccTokKeySec = "XXXXX"
def main():
api = twitter.Api(consumer_key = CusKey, consumer_secret = CusSec, access_token.key = AccTokKey, access_token.secret = AccTokKeySec)
timeline = api.GetFriendsTimeline()
for status in timeline:
print status.user.name.encode(‘ascii’, ‘ignore’), ‘————’, status.text.encode(‘ascii’, ‘ignore’)
if __name__ == ‘__main__’:
main() |
I don't quite understand the last if statement but that should solve the indention problem.
If you get more errors post them up and I'll try and help.
It's good for me, I'm studying python at the moment |
|
| Back to top |
|
 |
TwIsT
Joined: Tue May 17, 2005 9:05 pm Posts: 38
|
Posted: Sat Nov 26, 2011 5:20 pm Post subject: |
|
|
Thanks for the response... I don't know python myself I got this example from a blog post it looks like the authors blog has also stripped the white space.
I have fixed the indents. However I now get a new error:
| Code: | File "timeline.py", line 13
SyntaxError: Non-ASCII character '\xe2' in file timeline.py on line 13, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details
|
The File now Looks like:
| Code: |
from django.utils.encoding import smart_str, smart_unicode
import twitter
CusKey = "XX"
CusSec = "XXX"
AccTokKey = "XXXX"
AccTokKeySec = "XXXXX"
def main():
api = twitter.Api(consumer_key = CusKey, consumer_secret = CusSec, access_token.key = AccTokKey, access_token.secret = AccTokKeySec)
timeline = api.GetFriendsTimeline()
for status in timeline:
print status.user.name.encode(‘ascii’, ‘ignore’), ‘————’, status.text.encode(‘ascii’, ‘ignore’)
if __name__ == ‘__main__’:
main()
|
I keep stripping out my values for the twitter API because with them you could read my twitter stream!! |
|
| Back to top |
|
 |
thameslink
Joined: Fri Dec 11, 2009 11:17 pm Posts: 18
|
Posted: Sat Nov 26, 2011 5:38 pm Post subject: |
|
|
| '————' <--- these are the problem on line 13 I reckon just strip them out and see how it runs |
|
| Back to top |
|
 |
TwIsT
Joined: Tue May 17, 2005 9:05 pm Posts: 38
|
Posted: Sat Nov 26, 2011 6:10 pm Post subject: |
|
|
I've been googling the error and have amended the code:
| Code: |
# coding: utf-8
from django.utils.encoding import smart_str, smart_unicode
import twitter
CusKey = "QTKBmU6YTstD4j9rxWFnIw"
CusSec = "QD2nPs1khqFIuH2mCNX88cbvXiufEnai58VmrNTJU"
AccTokKey = "117377091-VczOSo6Uh3hOnCaQq3qY7r8zP9VTX7lWvCZC61nh"
AccTokKeySec = "dMeeDhk4Xfex13w0PFZMp0VlJWR4JXKuElfHSRxs"
def main():
api = twitter.Api(consumer_key = CusKey, consumer_secret = CusSec, access_token_key = AccTokKey, access_token_secret = AccTokKeySec)
timeline = api.GetFriendsTimeline()
for status in timeline:
print status.user.name.encode(‘ascii’, ‘ignore’), ‘————’, status.text.encode(‘ascii’, ‘ignore’)
if __name__ == ‘__main__’:
main()
|
This allows the code to pass the non ASCII error but I now get a new error:
| Code: |
File "timeline.py", line 14
print status.user.name.encode(‘ascii’, ‘ignore’), ‘————’, status.text.encode(‘ascii’, ‘ignore’)
^
SyntaxError: invalid syntax
|
I'm kinda getting use to python |
|
| Back to top |
|
 |
thameslink
Joined: Fri Dec 11, 2009 11:17 pm Posts: 18
|
Posted: Sat Nov 26, 2011 6:27 pm Post subject: version |
|
|
Check what version of python your running this under
might want to edit out those keys?
the syntax for print has altered between v2.7xx and 3.xx
Something like #! /usr/sbin/python2.7 in the 1st line or whatever the path to 2.7 is on your system should sort that out
Last edited by thameslink on Sat Nov 26, 2011 6:33 pm; edited 1 time in total |
|
| Back to top |
|
 |
TwIsT
Joined: Tue May 17, 2005 9:05 pm Posts: 38
|
Posted: Sat Nov 26, 2011 6:30 pm Post subject: |
|
|
Running:
Python 2.7.2+
How do you mean edit out the keys? |
|
| Back to top |
|
 |
thameslink
Joined: Fri Dec 11, 2009 11:17 pm Posts: 18
|
Posted: Sat Nov 26, 2011 6:36 pm Post subject: weird string |
|
|
| It seems to run ok with that weird '----------' string removed |
|
| Back to top |
|
 |
TwIsT
Joined: Tue May 17, 2005 9:05 pm Posts: 38
|
Posted: Sat Nov 26, 2011 6:52 pm Post subject: |
|
|
Strange It doesn't run for me with or without the ------ Things With or without the encoding. option.
Could you please post what worked for you? I can then fill in my details for the Twitter API and see if it works! |
|
| Back to top |
|
 |
thameslink
Joined: Fri Dec 11, 2009 11:17 pm Posts: 18
|
Posted: Sat Nov 26, 2011 7:00 pm Post subject: my code |
|
|
| Code: |
from django.utils.encoding import smart_str, smart_unicode
import twitter
CusKey = "XX"
CusSec = "XXX"
AccTokKey = "XXXX"
AccTokKeySec = "XXXXX"
def main():
api = twitter.Api(consumer_key = CusKey, consumer_secret = CusSec, access_token.key = AccTokKey, access_token.secret = AccTokKeySec)
timeline = api.GetFriendsTimeline()
for status in timeline:
# print status.user.name.encode('ascii', 'ignore'), status.text.encode('ascii', 'ignore')
print staus.user.name.encode('ascii', 'ignore'), status.text.encode('ascii', 'ignore')
if __name__ == '__main__':
main() |
i run this in bash with python2 ~/path/to/file
it errors with
| Code: | File "test.py", line 10
api = twitter.Api(consumer_key = CusKey, consumer_secret = CusSec, access_token.key = AccTokKey, access_token.secret = AccTokKeySec)
SyntaxError: keyword can't be an expression
|
probly because i dont have the django modules and stuff
also i had to make sure the weird backticks are replaced with proper ' style quotes |
|
| Back to top |
|
 |
TwIsT
Joined: Tue May 17, 2005 9:05 pm Posts: 38
|
Posted: Sat Nov 26, 2011 7:21 pm Post subject: |
|
|
Now we are getting somewhere Sorta....
The Error was not from DJango I was using the wrong format for the Twitter Python API
| Code: |
def main():
api = twitter.Api(consumer_key = CusKey, consumer_secret = CusSec, access_token_key = AccTokKey, access_token_secret = AccTokKeySec)
timeline = api.GetFriendsTimeline()
for status in timeline:
# print status.user.name.encode('ascii', 'ignore'), status.text.encode('ascii', 'ignore')
print staus.user.name.encode('ascii', 'ignore'), status.text.encode('ascii', 'ignore')
if __name__ == '__main__':
main()
|
However the program doesn't output anything now (It should dump my twitter stream) |
|
| Back to top |
|
 |
thameslink
Joined: Fri Dec 11, 2009 11:17 pm Posts: 18
|
Posted: Sat Nov 26, 2011 11:27 pm Post subject: ok now things start to break down |
|
|
The twitter python module used in the code must be a different module from the one I downloaded from pypi http://pypi.python.org/pypi/twitter/1.7.2
from the python interactive shell
>>>help(twitter)
gives some good examples of how to use it, it uses OAuth by the looks of it |
|
| Back to top |
|
 |
TwIsT
Joined: Tue May 17, 2005 9:05 pm Posts: 38
|
Posted: Sun Nov 27, 2011 10:52 am Post subject: |
|
|
| OK The script I was using isn't using the same twitter library as the one that you have downloaded. I imagine that they call the same stuff but the older ones are looking for python2.4. I've installed the other python libraries for twitter but when running it from the command line it complains that it cannot find cmdline which is an add-in that it creates. Back to the drawinig board I think ! |
|
| Back to top |
|
 |
thameslink
Joined: Fri Dec 11, 2009 11:17 pm Posts: 18
|
Posted: Sun Nov 27, 2011 12:40 pm Post subject: square one |
|
|
Please explain what you want to achive with this script.
why do you need all these keys and API functions.
Can the output of your twitter stream be fetched without authorisation.
please explain how the output of the python script will be directed into conky. |
|
| Back to top |
|
 |
TwIsT
Joined: Tue May 17, 2005 9:05 pm Posts: 38
|
Posted: Sat Dec 03, 2011 7:18 pm Post subject: |
|
|
OK,
Since Twitter moved from RSS feeds to OAuth you need to authorize all applications in the twitter API against your twitter account. The keys and stuff are needed to tell twitter that this application (That it does not have any prior idea of. Applications like Gwibber etc have their own sets of keys built in. This allow twitter to revoke access to the client if it does bad stuff) is allowed against your account. To get them you need to open a twitter Dev account.
The script will be put into conky by using the conky exec command every X seconds:
| Code: | | ${scroll 82 3 ${execi 300 perl /home/steve/twitter.pl -f | cat | fold -w120}} |
Like that (That being a perl twitter script.
The script I was trying to use was found on a blog. However when I tried to contact that author to get more help I heard nothing back. I've dropped that script for the perl script above that worked first time there are lots of ways of doing twitter in conky I wanted to try something different! |
|
| Back to top |
|
 |
| View previous topic :: View next topic |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|
|