/ PyPortal_TwitterPlayer / twitterplayer.py
twitterplayer.py
1 # SPDX-FileCopyrightText: 2019 Limor Fried for Adafruit Industries 2 # 3 # SPDX-License-Identifier: MIT 4 5 import time 6 import os 7 import random 8 import board 9 from adafruit_pyportal import PyPortal 10 11 # Set up where we'll be fetching data from 12 TWITTER_USER = "DuneQuoteBot" 13 TWITTER_TO_XML = "https://twitrss.me/twitter_user_to_rss/?user=" 14 XML_TO_JSON = "http://www.unmung.com/mf2?url=http://www.unmung.com/feed?feed=" 15 NUM_TWEETS=20 # how many tweets we get within a query to randomize from 16 DATA_SOURCE = XML_TO_JSON+TWITTER_TO_XML+TWITTER_USER 17 TEXT_LOCATION = ["items", 0, "children", 0, "properties", "name", 0] 18 19 # the current working directory (where this file is) 20 cwd = ("/"+__file__).rsplit('/', 1)[0] 21 22 # Find all image files on the storage 23 imagefiles = [file for file in os.listdir(cwd+"/backgrounds/") 24 if (file.endswith(".bmp") and not file.startswith("._"))] 25 for i, filename in enumerate(imagefiles): 26 imagefiles[i] = cwd+"/backgrounds/"+filename 27 print("Image files found: ", imagefiles) 28 29 pyportal = PyPortal(url=DATA_SOURCE, 30 json_path=TEXT_LOCATION, 31 status_neopixel=board.NEOPIXEL, 32 default_bg=imagefiles[0], 33 text_font=cwd+"/fonts/Arial-Italic-12.bdf", 34 text_position=(25, 20), 35 text_color=0xFFFFFF, 36 text_wrap=35, # character to wrap around 37 text_maxlen=280, # cut off characters 38 ) 39 pyportal.preload_font() 40 41 while True: 42 response = None 43 try: 44 pyportal.set_background(random.choice(imagefiles)) 45 response = pyportal.fetch() 46 print("Response is", response) 47 except (IndexError, RuntimeError, ValueError) as e: 48 print("Some error occured, retrying! -", e) 49 50 # next tweet should be random! 51 tweet_idx = random.randint(0, NUM_TWEETS) 52 TEXT_LOCATION[3] = tweet_idx 53 time.sleep(60)