import graphics
import driver
import game
import twython
import time

TWITTER_APP_KEY = 'ntLFB6p6IApzcEECKF4NdHsdJ'
TWITTER_APP_KEY_SECRET = '9AkI8vTZwxPXFrBMzcENZUscp9TEAJyFdXwCVrN3fVFZLifalO' 
TWITTER_ACCESS_TOKEN = '808021956-h3pMy2nfM2afQE6aC8hDTl69bb0JDu2hutYUqdoY'
TWITTER_ACCESS_TOKEN_SECRET = 'RLozX4oMPu7rUpfehHGEv6WZXS8xgbV4RdjjfmgbIgboV'

t = twython.Twython(app_key=TWITTER_APP_KEY, 
            app_secret=TWITTER_APP_KEY_SECRET, 
            oauth_token=TWITTER_ACCESS_TOKEN, 
            oauth_token_secret=TWITTER_ACCESS_TOKEN_SECRET)

class Twitter(game.Game):
  def __init__(self, *args, **kwargs):
    super().__init__(*args, **kwargs)
    self.nl = iter([])
    
  def scroll(self, title, text):
    titlesprite = graphics.TextSprite(title, x=0, y=0, width=5, height=7)
    self.sprites.add(titlesprite)
    sprite = graphics.TextSprite(text, x=112, y=8, width=5, height=7)
    self.sprites.add(sprite)
    while (-1)*((sprite.width+1)*len(text)) < sprite.x:
      self.graphics.clear()
      for x in self.sprites:
        x.draw(self.graphics)
      time.sleep(0.02)
      sprite.x -= 1
      self.graphics.draw(self.serial)
      yield
    self.graphics.clear()
    self.sprites.remove(sprite)
    self.sprites.remove(titlesprite)

  def tweetloop(self, tweets):
    twats = {x['id']:(x['user']['screen_name'], ' '.join(x['text'].split("\n"))) for x in tweets}
    for i in twats.keys():
      yield from self.scroll("@"+twats[i][0], twats[i][1])
    yield from self.scroll("#Hackafe", "Tweet us to get your text here!")
    

  def loop(self):
    try:
        next(self.nl)
    except StopIteration:
        try:
            search = t.search(q='#hackafe OR #bitcamp', count=10)
        except twython.exceptions.TwythonError:
            pass
        tweets = search['statuses']
        self.nl = self.tweetloop(tweets)
        time.sleep(2)


GAME = Twitter

import graphics
import driver
import game
import twython
import time

TWITTER_APP_KEY = 'ntLFB6p6IApzcEECKF4NdHsdJ'
TWITTER_APP_KEY_SECRET = '9AkI8vTZwxPXFrBMzcENZUscp9TEAJyFdXwCVrN3fVFZLifalO' 
TWITTER_ACCESS_TOKEN = '808021956-h3pMy2nfM2afQE6aC8hDTl69bb0JDu2hutYUqdoY'
TWITTER_ACCESS_TOKEN_SECRET = 'RLozX4oMPu7rUpfehHGEv6WZXS8xgbV4RdjjfmgbIgboV'

t = twython.Twython(app_key=TWITTER_APP_KEY, 
            app_secret=TWITTER_APP_KEY_SECRET, 
            oauth_token=TWITTER_ACCESS_TOKEN, 
            oauth_token_secret=TWITTER_ACCESS_TOKEN_SECRET)

class Twitter(game.Game):
  def __init__(self, *args, **kwargs):
    kwargs['framerate'] = 120
    super().__init__(*args, **kwargs)
    self.nl = iter([])
    
  def scroll(self, title, text):
    titlesprite = graphics.TextSprite(title, x=0, y=0, width=5, height=7)
    self.sprites.add(titlesprite)
    sprite = graphics.TextSprite(text, x=112, y=8, width=5, height=7)
    self.sprites.add(sprite)
    while (-1)*((sprite.width+1)*len(text)) < sprite.x:
      self.graphics.clear()
      for x in self.sprites:
        x.draw(self.graphics)
      time.sleep(0.02)
      sprite.x -= 1
      self.graphics.draw(self.serial)
      yield
    self.graphics.clear()
    self.sprites.remove(sprite)
    self.sprites.remove(titlesprite)

  def tweetloop(self, tweets):
    twats = {x['id']:(x['user']['screen_name'], ' '.join(x['text'].split("\n"))) for x in tweets}
    for i in twats.keys():
      yield from self.scroll("@"+twats[i][0], twats[i][1])
    yield from self.scroll("#Hackafe", "Tweet us to get your text here!")
    

  def loop(self):
    try:
        next(self.nl)
    except StopIteration:
        try:
            search = t.search(q='#hackafe OR #bitcamp', count=10)
        except twython.exceptions.TwythonError:
            pass
        tweets = search['statuses']
        self.nl = self.tweetloop(tweets)
        time.sleep(2)


GAME = Twitter

Speed up twitter