I think the problem is that the buffer isn’t cleared after each call to scrollphat.write_string()
. I’m hoping that a scrollphat.clear()
at the end of the scroll_tweet
function will sort it out. That should clear the buffer each time.
yes good idea but that would just clear the display and display no text, i also tried clearing it at the beginning of the function… but try this… i think we cracked it mate :)
while status_length > 0:
scrollphat.scroll()
time.sleep(0.06)
status_length -= 1
else:
scrollphat.clear()
it seems to be working perfectly now!
Many thanks for your help with this project Sandy, these little zero’s are fun to work with and this has been an enjoyable first project.
thanks again
Glenn
Hi There,
I’ve been trying to adapt this code for the Scroll Phat HD but am struggling a bit. Here’s what I ended up with but can’t get anything to display. Any tips?
import sys
import time
import tweepy
import scrollphathd
from scrollphathd.fonts import font5x7
scrollphathd.clear()
while True:
try:
class MyStreamListener(tweepy.StreamListener): def on_status(self, status): if not status.text.startswith('RT'): scroll_tweet(status) def on_error(self, status_code): if status_code == 420: return False
def scroll_tweet(status): status = ' >>>>> @%s: %s ' % (status.user.screen_name.upper(), status.text.upper()) status = status.encode('ascii', 'ignore').decode('ascii') scrollphathd.write_string(status) status_length = scrollphathd.buffer_len() print status print 'status length --->',status_length print 'buffer_len --->',scrollphathd.write_string() while status_length > 0: #print 'status length',status_length #print 'buffer_len',scrollphathd.write_string(string, x=width, y=0, font=font5x7, brightness=0.5) scrollphathd.scroll() time.sleep(0.06) status_length -= 1 consumer_key ='SECRET' consumer_secret ='SECRET'
access_token = 'SECRET' access_token_secret = 'SECRET'
auth = tweepy.OAuthHandler(consumer_key, consumer_secret) auth.set_access_token(access_token, access_token_secret) api = tweepy.API(auth)
myStreamListener = MyStreamListener() myStream = tweepy.Stream(auth = api.auth, listener=myStreamListener)
myStream.filter(track=['#Raspberrypi'], async=False) except KeyboardInterrupt: scrollphathd.clear() sys.exit(-1)
granted I haven’t looked at the code very closely, a quick glance reveal that you never call scrollphathd.show()
… so would expect that indeed anything you’ve done up to the point of filling the buffer was lost on the world.
Thanks very much, making progress now.
This is what we ended up with, seems to work on the ScrollPhatHD. Happy to have any suggestions, it doesn’t seem to like multiple searches.
import time
import tweepy
import scrollphathd
scrollphathd.clear()
class MyStreamListener(tweepy.StreamListener):
def on_status(self, status):
if not status.text.startswith(‘RT’):
scroll_tweet(status)
def on_error(self, status_code):
if status_code == 420:
return False
def scroll_tweet(status):
status = ’ >>>>> @%s: %s ’ % (status.user.screen_name.upper(), status.text.upper())
status = status.encode(‘ascii’, ‘ignore’).decode(‘ascii’)
scrollphathd.write_string(status)
status_length = len(status)
while status_length > 0:
scrollphathd.scroll(x=3, y=0)
scrollphathd.flip(y=False)
scrollphathd.rotate(degrees=180)
scrollphathd.show()
scrollphathd.scroll()
time.sleep(0.1)
status_length -= 1
consumer_key =‘SECRET’
consumer_secret =‘SECRET’
access_token = ‘SECRET’
access_token_secret = ‘SECRET’
scrollphathd.write_string(“Loading Tweets”)
scrollphathd.scroll()
time.sleep(0.1)
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
myStreamListener = MyStreamListener()
myStream = tweepy.Stream(auth = api.auth, listener=myStreamListener)
myStream.filter(track=[‘raspberrypi’], async=False)
hi Keith, i have just received my scrollphat hd and will be checking this out soon!
have you made sure its formatted like this?
myStream.filter(track=[’#saintsfc’,’@BBCSport’], async=False)
glenn
Had a go with that formatting but it just seems to bring me back to a prompt. A puzzle.
i appear to be getting overlapping tweets on the scrollphatHD when using this code how is it for you ?
import time
import tweepy
import scrollphathd
from scrollphathd.fonts import font5x7smoothed
scrollphathd.clear()
while True:
try:
class MyStreamListener(tweepy.StreamListener):
def on_status(self, status):
if not status.text.startswith('RT'):
scroll_tweet(status)
def on_error(self, status_code):
if status_code == 420:
return False
def scroll_tweet(status):
status = ' >>>>> @%s: %s ' % (status.user.screen_name.upper(), status.text.upper())
status = status.encode('ascii', 'ignore').decode('ascii')
scrollphathd.write_string(status,font=font5x7smoothed, brightness=0.1)
status_length = len(status)
print status
print 'status length --->',status_length
while status_length > 0:
scrollphathd.scroll(x=2, y=0)
scrollphathd.flip(y=False)
scrollphathd.rotate(degrees=180)
scrollphathd.show()
scrollphathd.scroll()
time.sleep(0.01)
status_length -= 1
else:
scrollphathd.clear()
consumer_key ='PRIVATE'
consumer_secret ='PRIVATE'
access_token = 'PRIVATE'
access_token_secret = 'PRIVATE'
#scrollphathd.write_string("Loading Tweets")
#scrollphathd.scroll()
#time.sleep(0.1)
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
myStreamListener = MyStreamListener()
myStream = tweepy.Stream(auth = api.auth, listener=myStreamListener)
myStream.filter(track=['bbc'], async=False)
except KeyboardInterrupt:
scrollphathd.clear()
sys.exit(-1)
heres my latest code. i believe this is causing the problems:
status_length = len(status)
as we don’t seem to have an equivalent function buffer_len() for the HD version
ok heres the latest… seems theres a couple of bugs still :/
#!/usr/bin/env python
import sys
import time
import tweepy
import scrollphathd
from scrollphathd.fonts import font5x7smoothed
scrollphathd.clear()
while True:
try:
class MyStreamListener(tweepy.StreamListener):
def on_status(self, status):
if not status.text.startswith('RT'):
scroll_tweet(status)
def on_error(self, status_code):
if status_code == 420:
return False
def scroll_tweet(status):
status = ' >>>>> @%s: %s ' % (status.user.screen_name.upper(), status.text.upper())
status = status.encode('ascii', 'ignore').decode('ascii')
scrollphathd.write_string(status,font=font5x7smoothed, brightness=0.1)
status_length = scrollphathd.write_string(status, x=0, y=0,font=font5x7smoothed, brightness=0.1)
print status
print 'status length --->',status_length
while status_length > 0:
#print 'status length',status_length
#print 'buffer_len',scrollphathd.write_string(status, x=0, y=0,font=font5x7smoothed, brightness=0.1)
#scrollphathd.flip(x=True)
scrollphathd.rotate(degrees=180)
scrollphathd.show()
scrollphathd.scroll(1)
time.sleep(0.01)
status_length -= 1
else:
scrollphathd.scroll(0)
scrollphathd.clear()
consumer_key =''
consumer_secret =''
access_token = ''
access_token_secret = ''
#scrollphathd.write_string("Loading Tweets")
#scrollphathd.scroll()
#time.sleep(0.1)
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
myStreamListener = MyStreamListener()
myStream = tweepy.Stream(auth = api.auth, listener=myStreamListener)
myStream.filter(track=['hug'], async=False)
except KeyboardInterrupt:
scrollphathd.clear()
sys.exit(-1)
there’s a bug.
when the status_length var is reduced to 0 the new tweet status is set but the buffer appears to be still be scrolling and when the tweet is displayed, its often mid way through the text… i think the status_length isn’t getting the correct length.
now just this bug to resolve
Traceback (most recent call last):
File "/home/pi/Pimoroni/gScrollPhatHD.py", line 60, in <module>
myStream.filter(track=['hug'], async=False)
File "build/bdist.linux-armv6l/egg/tweepy/streaming.py", line 450, in filter
self._start(async)
File "build/bdist.linux-armv6l/egg/tweepy/streaming.py", line 364, in _start
self._run()
File "build/bdist.linux-armv6l/egg/tweepy/streaming.py", line 297, in _run
six.reraise(*exc_info)
File "build/bdist.linux-armv6l/egg/tweepy/streaming.py", line 266, in _run
self._read_loop(resp)
File "build/bdist.linux-armv6l/egg/tweepy/streaming.py", line 316, in _read_loop
line = buf.read_line().strip()
File "build/bdist.linux-armv6l/egg/tweepy/streaming.py", line 181, in read_line
self._buffer += self._stream.read(self._chunk_size)
File "/usr/lib/python2.7/dist-packages/urllib3/response.py", line 214, in read
raise ProtocolError('Connection broken: %r' % e, e)
ProtocolError: ('Connection broken: IncompleteRead(0 bytes read, 512 more expected)', IncompleteRead(0 bytes read, 512 more expected))
cool i will give it a go. :)
its working :) well done Mr-G :) hats off to you.,
its almost there ! it seems that the program cannot keep up with the twitter stream… so itll bomb out at times. im still looking into it when i get the time ! :)
what i think i need to do is buffer the tweets arriving into a file then read them from there.
Is a Tweepy Stream listener asynchronous? I guess it probably is, and would try to run multiple versions of scroll_tweet
if several tweets come in at once.
You’ll need a FIFO buffer which you dump tweets into instead of displaying them immediately, and then move your scroll_tweet
logic into a main loop which pops each Tweet off the buffer, one at a time, and scrolls it.
Edit: A FIFO buffer is just a fancy word for a list
which you append items to and them pop off the front, like so:
>>> x.append(1) # Add a tweet
>>> x
[1]
>>> x.append(2) # Add another tweet
>>> x
[1, 2]
>>> x.pop(0) # Grab the first tweet in the queue
1
Adding/removing items from a Python list is inherently thread-safe as far as I know, so you don’t have to worry about mutually exclusive locks or any complicated nonsense.
This means your program could try to add two items to the list simultaneously, and this would not result in any corruption or error.