def __init__(self): if not os.path.exists('TILFacts.db'): f = shelve.open('TILFacts') f.close() self.database_name = 'TILFacts' self.current_fact = None self.current_url = None root = tkinter.Tk() root.title('Today I Learned!') self.GUI = TodayILearnedWindow(root, self) with shelve.open(self.database_name) as db: self.total_facts = len(tuple(key for key in db.keys())) print("TOTAL NUM facts:", self.total_facts) self.already_seen_facts = [] root.mainloop()
class TodayILearnedApp: def __init__(self): if not os.path.exists('TILFacts.db'): f = shelve.open('TILFacts') f.close() self.database_name = 'TILFacts' self.current_fact = None self.current_url = None root = tkinter.Tk() root.title('Today I Learned!') self.GUI = TodayILearnedWindow(root, self) with shelve.open(self.database_name) as db: self.total_facts = len(tuple(key for key in db.keys())) print("TOTAL NUM facts:", self.total_facts) self.already_seen_facts = [] root.mainloop() def get_randomfact(self): #select random fact from db. if len( self.already_seen_facts ) == self.total_facts: #need to reset; all facts seen this session print("ALL FACTS SEEN! RESETTING!!!!") del self.already_seen_facts[:] with shelve.open(self.database_name) as db: random_id = random.choice( list(key for key in db.keys() if key not in self.already_seen_facts)) self.current_fact = db[random_id][0] self.current_url = db[random_id][1] self.already_seen_facts.append(random_id) self.GUI.update_display(self.current_fact, random_id) def learn_more(self): #called by clicking Learn More in GUI try: webbrowser.open(self.current_url) except Exception: print('Unable to open web browser.') def _format_fact(self, fact_string): #formats fact for entry into database fact = fact_string[4:] #remove TIL if fact.startswith('that') or fact.startswith( 'THAT') or fact.startswith('That'): fact = fact[4:] #remove redundant that following TIL, if present fact = fact.strip().capitalize() #capitalize fact sentence return fact def get_facts_from_reddit(self): try: reddit = praw.Reddit(user_agent='TILcollector') fact_subs = (s for s in ( reddit.get_subreddit('todayilearned').get_hot(limit=25)) if not s.stickied) except Exception: print( 'Problem connecting to Reddit and collecting facts. Aborting...' ) return count = 0 with shelve.open(self.database_name) as db: for sub in fact_subs: PID = sub.id if PID not in db: fact = self._format_fact(sub.title) URL = sub.url db[PID] = [fact, URL] thumbnail_url = sub.thumbnail self._downloadThumbnail(thumbnail_url, PID) count += 1 self.GUI.popup_window( 'Successfully downloaded {} new facts from Reddit!'.format(count)) def _downloadThumbnail(self, URL, ID): if not os.path.exists('TIL_thumbnails'): os.makedirs('TIL_thumbnails') try: res = requests.get(URL) with open('TIL_thumbnails/%s.png' % ID, 'wb') as f: for chunk in res.iter_content(100000): f.write(chunk) except Exception: pass def print_facts_to_file(self): with open('TILFacts.txt', 'w') as f: db = shelve.open(self.database_name) for key in db.keys(): f.write(db[key][0] + '\n') f.write('**************' * 5 + '\n\n') db.close() self.GUI.popup_window('Facts printed to file: TILfacts.txt')