1

I built a very simple script with PRAW that prints the top 10 link titles on reddit.com/r/worldnews. I want this to work with GeekTool, but only the following shows up:

"TOP 10 NEWS ON REDDIT

1 NEWS TITLE

2 "

I don't know why that happens since when running the script directly from the command line I have no issues whatsoever.

Here's the python script:

import praw

def main():
    subreddit = r.get_subreddit('worldnews')
    x = 1
    print "TOP 10 NEWS ON REDDIT"
    print '' 
    for submission in subreddit.get_hot(limit=10):
        print x, submission.title
        x = x+1
        print ' '

if __name__ == "__main__":
    user_agent = "Top10 0.1 by /u/alexisfg"
    r = praw.Reddit(user_agent=user_agent)
    main()
alexis_egf
  • 15
  • 5
  • works for me, how are you running it? – Padraic Cunningham Aug 12 '14 at 11:45
  • With "python /Users/alex/Desktop/top10.py" through GeekTool... – alexis_egf Aug 12 '14 at 11:51
  • Change `submission.title` to `submission.title.encode('utf-8')` – jon Aug 12 '14 at 11:51
  • @Jon That worked! Thanks! Why would it print the first line though? – alexis_egf Aug 12 '14 at 11:57
  • The first line is a red herring, it will fail on any line that has at least 1 character that can't be encoded by the ascii codec. In this case, the 2nd line is the first offender. I think GeekTool hides the error from you. see @Matt's try/except for a way to diagnose things like this. Also, checkout enumerate: https://gist.github.com/anonymous/603201ab844da47ac7b4 – jon Aug 12 '14 at 12:01

1 Answers1

0

If you put a try...except around the main function to print any exceptions, you get the following error message:

ascii codec can't encode character u'\u2019' in position 12: ordinal not in range(128)

So this is an encoding issue - some character in the second title is not in the ASCII range, which python/Geektool is using as the default encoding. You can get around this by encoding the title string explicitly with .encode('utf-8').

Matt Swain
  • 3,827
  • 4
  • 25
  • 36