Linux Format forums Forum Index Linux Format forums
Help, discussion, magazine feedback and more
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Python Help
Goto page 1, 2  Next
 
Post new topic   Reply to topic    Linux Format forums Forum Index -> Programming
View previous topic :: View next topic  
Author Message
TwIsT



Joined: Tue May 17, 2005 9:05 pm
Posts: 38

PostPosted: Sat Nov 26, 2011 4:44 pm    Post subject: Python Help Reply with quote

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
View user's profile Send private message
thameslink



Joined: Fri Dec 11, 2009 11:17 pm
Posts: 18

PostPosted: Sat Nov 26, 2011 5:08 pm    Post subject: code indentation Reply with quote

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
View user's profile Send private message
TwIsT



Joined: Tue May 17, 2005 9:05 pm
Posts: 38

PostPosted: Sat Nov 26, 2011 5:20 pm    Post subject: Reply with quote

Thanks for the response... I don't know python myself Smile 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
View user's profile Send private message
thameslink



Joined: Fri Dec 11, 2009 11:17 pm
Posts: 18

PostPosted: Sat Nov 26, 2011 5:38 pm    Post subject: Reply with quote

'————' <--- these are the problem on line 13 I reckon just strip them out and see how it runs
Back to top
View user's profile Send private message
TwIsT



Joined: Tue May 17, 2005 9:05 pm
Posts: 38

PostPosted: Sat Nov 26, 2011 6:10 pm    Post subject: Reply with quote

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
View user's profile Send private message
thameslink



Joined: Fri Dec 11, 2009 11:17 pm
Posts: 18

PostPosted: Sat Nov 26, 2011 6:27 pm    Post subject: version Reply with quote

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
View user's profile Send private message
TwIsT



Joined: Tue May 17, 2005 9:05 pm
Posts: 38

PostPosted: Sat Nov 26, 2011 6:30 pm    Post subject: Reply with quote

Running:

Python 2.7.2+

How do you mean edit out the keys?
Back to top
View user's profile Send private message
thameslink



Joined: Fri Dec 11, 2009 11:17 pm
Posts: 18

PostPosted: Sat Nov 26, 2011 6:36 pm    Post subject: weird string Reply with quote

It seems to run ok with that weird '----------' string removed
Back to top
View user's profile Send private message
TwIsT



Joined: Tue May 17, 2005 9:05 pm
Posts: 38

PostPosted: Sat Nov 26, 2011 6:52 pm    Post subject: Reply with quote

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
View user's profile Send private message
thameslink



Joined: Fri Dec 11, 2009 11:17 pm
Posts: 18

PostPosted: Sat Nov 26, 2011 7:00 pm    Post subject: my code Reply with quote

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
View user's profile Send private message
TwIsT



Joined: Tue May 17, 2005 9:05 pm
Posts: 38

PostPosted: Sat Nov 26, 2011 7:21 pm    Post subject: Reply with quote

Now we are getting somewhere Smile 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
View user's profile Send private message
thameslink



Joined: Fri Dec 11, 2009 11:17 pm
Posts: 18

PostPosted: Sat Nov 26, 2011 11:27 pm    Post subject: ok now things start to break down Reply with quote

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
View user's profile Send private message
TwIsT



Joined: Tue May 17, 2005 9:05 pm
Posts: 38

PostPosted: Sun Nov 27, 2011 10:52 am    Post subject: Reply with quote

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
View user's profile Send private message
thameslink



Joined: Fri Dec 11, 2009 11:17 pm
Posts: 18

PostPosted: Sun Nov 27, 2011 12:40 pm    Post subject: square one Reply with quote

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
View user's profile Send private message
TwIsT



Joined: Tue May 17, 2005 9:05 pm
Posts: 38

PostPosted: Sat Dec 03, 2011 7:18 pm    Post subject: Reply with quote

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 Smile there are lots of ways of doing twitter in conky I wanted to try something different!
Back to top
View user's profile Send private message
View previous topic :: View next topic  
Display posts from previous:   
Post new topic   Reply to topic    Linux Format forums Forum Index -> Programming All times are GMT
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
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
Linux Format forums topic RSS feed 


Powered by phpBB © 2001, 2005 phpBB Group


Copyright 2011 Future Publishing, all rights reserved.


Web hosting by UKFast