def repost_replies(account_name): bf = open('.blacklist_%s'%account_name,'a+') blacklist = bf.read().splitlines() bf.close() rp = open('.reposted_%s'%account_name,'a+') reposted = rp.read().splitlines() account = settings.ACCOUNTS.get(account_name) try: logging.info('[%s] Getting last mentions offset'%account_name) bot = TwitterBot(settings.CONSUMER_KEY,settings.CONSUMER_SECRET, account['key'],account['secret']) mentions = [] try: mentions = bot.api.mentions() logging.info('[%s] Got %d mentions'%(account_name,len(mentions))) except Exception,e: logging.error('[%s] Failed to get mentions. %s'%(account_name,e)) for mess in reversed(mentions): try: author = mess.author.screen_name if str(author) in blacklist: logging.debug('[%s] Author %s blacklisted. Skipping.'%(account_name,str(author))) continue if str(mess.id) in reposted: logging.debug('[%s] Message #%s already reposted. Skipping.'%(account_name,str(mess.id))) continue message = mess.text.split(' ') if message[0] != '@%s'%account_name: continue #not a "@reply" trigger = message[1] triggers = dict(account['triggers']) if trigger not in triggers: logging.warning('[%s] Bad message format, sending DM to author'%account_name) bot.dm(author,account['not_triggered']) else: len_params = {'message':'','user':author} mess_len = len(triggers[trigger]%len_params) params = {'message':bot.trim_message(' '.join(message[2:]),mess_len),'user':author} message = triggers[trigger]%params logging.info('[%s] Tweeting message %s'%(account_name,message)) bot.tweet(message) rp.write('%s\n'%mess.id) except Exception,e: logging.error('%s'%e) continue
def main(): feed = feedparser.parse( 'https://fedoramagazine.org/feed/') # read the feed try: links = pickle.load(open('tweeted_links', 'rb')) # load posted links except FileNotFoundError: links = set() # no link is posted yet try: bot = TwitterBot(USERNAME, PASSWORD) for entry in feed.entries[::-1]: link = entry.link title = entry.title print(title, link) if link not in links: bot.tweet(title + '\n' + link) links.add(link) bot.quit() # quits and clears the chromedriver from memory finally: pickle.dump(links, open('tweeted_links', 'wb')) # add the posted links to the file
def main(): try: now = time() # Construct auth and twitterbot object auth = OAuth(config['access_token'], config['access_secret'], config['consumer_key'], config['consumer_secret']) twitterBot = TwitterBot(auth) # create brainyquote object and load quotes quote = BrainyQuote() quote.load() iter = 0 while True: # Safeguard, just stop running after 1000 iterations if iter >= 1000: break random_quote = quote.randomize() if not quote.exists(random_quote): text, auth = random_quote auth = twitterify(auth) if len(auth) + len(text) > 140: text = text[0:140 - len(auth)] tweet = '%s %s' % (text, auth) if twitterBot.tweet(tweet): quote.save(random_quote) break iter = iter + 1 except Exception as err: Logger("Unhandled exception!: %s" % err) except QuoteException as qerr: Logger("Quote exception in program: %s" % qerr) finally: message = 'Finished running TwitterBot, date: {0}, running time: {1} secs'.format(str(datetime.now()), time() - now) Logger(message)
if args.verbose > 0: print('Saving to db: {}/{}'.format( entry.get_author(), entry.get_title())) db_saver.save(entry) # Twitter bot # TODO: async if args.twitter: info_str = '{0}/{1}[{2}]'.format(entry.get_author(), entry.get_title(), entry.hashcode()) if entry.hashcode() not in tweeted: if args.verbose > 1: # TODO: log print('{0} is not tweeted yet'.format(info_str)) twitter_bot.tweet(entry) tweeted.add(entry.hashcode()) tweeted_file.write('{}\n'.format(entry.hashcode())) print('tweeted: {0}'.format(info_str)) else: if args.verbose > 1: print('{0} is already tweeted'.format(info_str)) except Exception as e: print(str(e)) time.sleep(args.interval) except KeyboardInterrupt: if args.verbose > 0: print('Keyboard Interrupt: clean up')
# Import the TwitterBot class from twitterbot import TwitterBot # create a new bot bot = TwitterBot("my_username", "my_secret_password") # tweet bot.tweet("Hello world from python and selenium") # quit the bot and close the chrome window bot.quit()
print('Saving to db: {}/{}'.format( entry.get_author(), entry.get_title())) db_saver.save(entry) # Twitter bot # TODO: async if args.twitter: info_str = '{0}/{1}[{2}]'.format( entry.get_author(), entry.get_title(), entry.hashcode()) if entry.hashcode() not in tweeted: if args.verbose > 1: # TODO: log print('{0} is not tweeted yet'.format(info_str)) twitter_bot.tweet(entry) tweeted.add(entry.hashcode()) tweeted_file.write('{}\n'.format(entry.hashcode())) print('tweeted: {0}'.format(info_str)) else: if args.verbose > 1: print('{0} is already tweeted'.format(info_str)) except Exception as e: print(str(e)) time.sleep(args.interval) except KeyboardInterrupt: if args.verbose > 0: print('Keyboard Interrupt: clean up')