class TetrisBot: def __init__(self): self.fb_client = PyFb(json.load(open('../tokens.json', 'r'))['tetris']) self.db_client = sqlite3.connect('tetris.db') # returns id, tetris state def read_latest_state(self): cursor = self.db_client.cursor() cursor.execute('SELECT id, state FROM CreatedPosts ORDER BY ts DESC LIMIT 1') row = [_ for _ in cursor.fetchall()][0] return row[0], pickle.loads(row[1]) def write_state(self, tetris_state, msg_prefix=''): ts = int(datetime.now().timestamp()) ## TODO: hardcoded! very naughty... #bg = 'angus.jpeg bg = 'angus2.jpg' tetris_state.to_image('out.png', bg=bg) msg = msg_prefix + 'React to vote on a move!\n❤️ Left\t😂 Right\t😮 Rotate\t👍 Down\t😡 Hard drop\t😢 Swap' post_id = self.fb_client.create_post(msg, 'out.png')['post_id'] self.db_client.cursor().execute('INSERT INTO CreatedPosts(id, state, ts) VALUES(?, ?, ?)', (post_id, pickle.dumps(tetris_state), ts)) self.db_client.commit() return post_id def new_state(self): print('Starting over and writing it!') g = tetris.TetrisState() g.drop() g.drop() g.drop() self.write_state(g, msg_prefix='NEW GAME!\n')
import random, sys, json sys.path.insert(0, '..') from pyfb.pyfb import PyFb import model, videocardgen fb_client = PyFb(json.load(open('../tokens.json', 'r'))['scotty']) def post(thumbnail_url, title): videocardgen.draw_card(thumbnail_url, title) fb_client.create_post(message=title, image_path='tmp-card.png') if __name__ == '__main__': post(model.random_video().thumbnail, model.do_chain())
import sys, json sys.path.insert(0, '..') from pyfb.pyfb import PyFb data = json.load(open(sys.argv[1], 'r')) image_path = sys.argv[2] fb_client = PyFb(json.load(open('../tokens.json', 'r'))['electoralcollege']) red_votes = sum(data['red'].values()) blue_votes = sum(data['blue'].values()) rc = data['candidates']['red'] rc = rc[:rc.index('.')] bc = data['candidates']['blue'] bc = bc[:bc.index('.')] winning_candidate = rc if red_votes > blue_votes else bc losing_candidate = rc if red_votes < blue_votes else bc winner_votes = red_votes if red_votes > blue_votes else blue_votes pid = fb_client.create_post(message='{} has beaten {} with {} votes'.format(winning_candidate, losing_candidate, winner_votes), image_path=image_path)['post_id'] fb_client.comment_on_post(message='Nothing says good logging like a Facebook comment: ' + json.dumps(data), post_id=pid)
import random, sys, json sys.path.insert(0, '..') from pyfb.pyfb import PyFb import imagedrawer, model fb_client = PyFb(json.load(open('../tokens.json', 'r'))['exodia']) deck = model.Deck(id=0) hand = deck.draw_five() image_path = 'out.png' imagedrawer.draw_hand('bg.jpg', hand, out_path=image_path) message = '' if sorted(hand) == model.EXODIA_HAND: message = "THE UNSTOPPABLE EXODIA! I'VE ASSEMBLED ALL FIVE SPECIAL CARDS! ALL FIVE PIECES OF THE PUZZLE! EXODIA! OBLITERATE!!!\n" message = message + '\n' + '\n'.join(hand) print(message) pid = fb_client.create_post(message=message, image_path=image_path)['post_id'] session = model.Session() session.add(model.Post.from_fb_post_id_and_hand(pid, hand)) session.commit()
def __init__(self): self.fb_client = PyFb(json.load(open('../tokens.json', 'r'))['tetris']) self.db_client = sqlite3.connect('tetris.db')
import random, sys, json sys.path.insert(0, '..') from pyfb.pyfb import PyFb import drawer, model from angusphotos.ap import random_angus_photo_file_path fb_client = PyFb(json.load(open('../tokens.json', 'r'))['pcbuilder']) fb_client.create_post(message=model.pstr_pc(model.random_pc()), image_path=drawer.draw_pc_mosaic())
def get_fb_client(): return PyFb(json.load(open('../tokens.json', 'r'))['wheresangus'])
def iteration(db_client, fb_client): # get unused question count unused_qs = unused_questions(db_client) unused_question_count = 0 if unused_qs is not None: unused_question_count = len(unused_qs) print('Unused question count:', unused_question_count) if unused_question_count < 400: print('Deciding to pull more questions') add_questions_to_db(db_client, 100) unused_qs = unused_questions(db_client) lp = last_post(db_client) la = last_answer(db_client) if lp is not None: print('Found last post id:', lp[0], 'the answer was:', la) fb_client.comment_on_post(lp[0], 'The answer was: ' + la) else: print('No latest post found!') print('Making new post!') chosen_question = random.choice(unused_qs) while chosen_question['question'].strip() == '': chosen_question = random.choice(unused_qs) print('Chose the question:', str(chosen_question)) make_post(db_client, fb_client, chosen_question) if __name__ == '__main__': db_client = sqlite3.connect('jeopardy.db') fb_client = PyFb(json.load(open('../tokens.json', 'r'))['jeopardy']) iteration(db_client, fb_client)
import random, sys, json sys.path.insert(0, '..') from pyfb.pyfb import PyFb import metallizer fb_client = PyFb(json.load(open('../tokens.json', 'r'))['metal']) if __name__ == '__main__': text, photo_path = metallizer.make_post() post_id = fb_client.create_post(message=text, image_path=photo_path)['post_id']
import random, sys, json, xkcd sys.path.insert(0, '..') from pyfb.pyfb import PyFb CACHE = xkcd.get_cache() def get_post_content(): latest_comic_num = int(xkcd.get_latest_from_cache(CACHE)['num']) chosen_num = random.randint(1, latest_comic_num) print('Picking comic, 1 <= {} <= {}'.format(chosen_num, latest_comic_num)) comic = xkcd.get_comic(chosen_num, CACHE) content = 'https://xkcd.com/{}/\n#{} Title: {}\nAlt:{}'.format( comic['num'], comic['num'], comic['safe_title'], comic['alt']) if 'transcript' in comic.keys() and len(comic['transcript']) > 0: content += '\nTranscript:{}'.format(comic['transcript']) return content, xkcd.ensure_comic_image_path(comic) if __name__ == '__main__': fb_client = PyFb(json.load(open('../tokens.json', 'r'))['xkcd']) text, image_path = get_post_content() fb_client.create_post(message=text, image_path=image_path)