Python Help

Code junkies hangout here

Moderators: ChrisThornett, LXF moderators

Python Help

Postby TwIsT » Sat Nov 26, 2011 4:44 pm

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: Select all
  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: Select all
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
TwIsT
 
Posts: 38
Joined: Tue May 17, 2005 8:05 pm

code indentation

Postby thameslink » Sat Nov 26, 2011 5:08 pm

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: Select all
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
thameslink
 
Posts: 18
Joined: Fri Dec 11, 2009 11:17 pm

Postby TwIsT » Sat Nov 26, 2011 5:20 pm

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: Select all
 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: Select all
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!!
TwIsT
 
Posts: 38
Joined: Tue May 17, 2005 8:05 pm

Postby thameslink » Sat Nov 26, 2011 5:38 pm

'————' <--- these are the problem on line 13 I reckon just strip them out and see how it runs
thameslink
 
Posts: 18
Joined: Fri Dec 11, 2009 11:17 pm

Postby TwIsT » Sat Nov 26, 2011 6:10 pm

I've been googling the error and have amended the code:
Code: Select all
# 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: Select all
  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
TwIsT
 
Posts: 38
Joined: Tue May 17, 2005 8:05 pm

version

Postby thameslink » Sat Nov 26, 2011 6:27 pm

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.
thameslink
 
Posts: 18
Joined: Fri Dec 11, 2009 11:17 pm

Postby TwIsT » Sat Nov 26, 2011 6:30 pm

Running:

Python 2.7.2+

How do you mean edit out the keys?
TwIsT
 
Posts: 38
Joined: Tue May 17, 2005 8:05 pm

weird string

Postby thameslink » Sat Nov 26, 2011 6:36 pm

It seems to run ok with that weird '----------' string removed
thameslink
 
Posts: 18
Joined: Fri Dec 11, 2009 11:17 pm

Postby TwIsT » Sat Nov 26, 2011 6:52 pm

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!
TwIsT
 
Posts: 38
Joined: Tue May 17, 2005 8:05 pm

my code

Postby thameslink » Sat Nov 26, 2011 7:00 pm

Code: Select all
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: Select all
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
thameslink
 
Posts: 18
Joined: Fri Dec 11, 2009 11:17 pm

Postby TwIsT » Sat Nov 26, 2011 7:21 pm

Now we are getting somewhere :) Sorta....

The Error was not from DJango I was using the wrong format for the Twitter Python API
Code: Select all
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)
TwIsT
 
Posts: 38
Joined: Tue May 17, 2005 8:05 pm

ok now things start to break down

Postby thameslink » Sat Nov 26, 2011 11:27 pm

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
thameslink
 
Posts: 18
Joined: Fri Dec 11, 2009 11:17 pm

Postby TwIsT » Sun Nov 27, 2011 10:52 am

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 !
TwIsT
 
Posts: 38
Joined: Tue May 17, 2005 8:05 pm

square one

Postby thameslink » Sun Nov 27, 2011 12:40 pm

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.
thameslink
 
Posts: 18
Joined: Fri Dec 11, 2009 11:17 pm

Postby TwIsT » Sat Dec 03, 2011 7:18 pm

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: Select all
${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!
TwIsT
 
Posts: 38
Joined: Tue May 17, 2005 8:05 pm

Next

Return to Programming

Who is online

Users browsing this forum: Alexa [Bot] and 0 guests