Scrollphat Python twitter code help needed

HI folks, Programmed the scrollphat to stream my twitter timeline. Totally new to python and have very limited coding experience, mostly from 20 years ago. The code below works, but the tweets constantly repeat until a new one is posted. So I’m looking for some help to -

  • stop tweets after they scroll once.
  • strip out any http links embedded in the tweet - I just want the text content.
  • ideally change the tweets to all upper case letters to make it easier to read, but that’s not a priority.

Feel free to copy the code for your own projects, you’ll have to install twython though.

Any ideas?

===
#!/usr/bin/env python

import scrollphat
import sys
import time
import thread
from twython import TwythonStreamer

Twitter application authentication

APP_KEY = ''
APP_SECRET = ''
OAUTH_TOKEN = ''
OAUTH_TOKEN_SECRET = ‘’

class MyStreamer(TwythonStreamer):
def on_success(self, data):
if ‘text’ in data:
tweet = ‘@{}: {}’.format(data[‘user’][‘screen_name’].encode(‘utf-8’), data[‘text’].encode(‘utf-8’))
scrollphat.write_string(tweet)
#print tweet

def on_error(self, status_code, data):
    print status_code, data

def scroll_display(sleeptime):
# turn everything off
scrollphat.clear()
scrollphat.rotate = True

while True:
    try:
        scrollphat.scroll()
        #print 'thread awake'
        time.sleep(sleeptime)
    except KeyboardInterrupt:
        scrollphat.clear()
        sys.exit(-1)

def main():
# kick off the thread to manage the display
thread.start_new_thread(scroll_display, (0.1,))

# Requires Authentication as of Twitter API v1.1
stream = MyStreamer(APP_KEY, APP_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET)
stream.user()

if name == “main”:
main()

Hi, a quick google found this post for stripping html strings:


which seems like it might help with one bit of your question?

Another quick google for “python strings to upper case” pulled this back


so you’d want to change:
scrollphat.write_string(tweet)
to
scrollphat.write_string(tweet.upper())

As for the scrolling once, it’s very hard to follow your code with the formatting above, I’d suggest pushing to somewhere like github and providing a link as that formats code quite well in a webpage and allows cutting and pasting of links to specific lines, and that might make it easier for someone to understand your code and give you some more pointers :)

1 Like

I think I may be able to be of assistance! Have a look at my tutorial here. It includes converting the tweet to uppercase to make it a bit more legible, and adding padding to the beginning and end, and some chevrons at the start to pre-warn of incoming tweets. Hope it helps!