def update_boards(boards): #update list of boardnames, and tweet about new ones try: r = s.get('http://a.4cdn.org/boards.json') #don't bother with If-Modified-Since r.raise_for_status() jbrds = json.loads(r.content) new_listed_boards = [] for json_board in r.json()['boards']: new_listed_boards.append(json_board) except (RequestException, ConnectionError, HTTPError, Timeout) as e: print "Error getting boards from 4chan. Using old boards instead." return boards new_boards = [new_listed_board['board'] for new_listed_board in new_listed_boards] if boards != new_boards: #If there is a new board... try: with open('.boards_cache.txt', 'r') as f: last_update_timestamp = f.readline() #Get last time boards were updated except IOError: open('.boards_cache.txt', 'w').close() #Create file if it doesn't exist last_update_timestamp = 0 if (time.time() - float(last_update_timestamp)) / 86400 < DAYS_NEW_BOARD_IS_FRESH: #If the boards were updated recently enough... for new_board in new_boards: if not new_board in boards: print "New board: "+new_board #print NEW_BOARD_TEXT.format(board=new_board) twitter.update_status(status=NEW_BOARD_TEXT.format(board=new_board)) boards = new_boards #Update boards with open('.boards_cache.txt', 'w') as f: f.write(str(time.time())+'\n') for listed_board in boards: f.write(listed_board+'\n') return boards
def send_tweet(tweet, graph): logger.info('Uploading graph') response = twitter.upload_media(media=graph) media_ids = [response['media_id']] logger.info('Tweeting: {}'.format(tweet)) try: twitter.update_status(status=tweet, media_ids=media_ids) except Exception as e: logger.error('{}: {}'.format(e.__class__.__name__, e))
def twitter(usermask,messagetype,channel,chatline,args): allowed_usermasks = ['*****@*****.**','*****@*****.**'] if args == None: ircmessage = "refusing to make an empty tweet" ttymessage = "did not tweet anything" else: allowed = False for mask in allowed_usermasks: if mask in usermask: allowed = True if not allowed == True: ircmessage = _usermask_to_username(usermask) + ": i can not do this, dave." ttymessage = "twitter: users mask was not in the list of allowed usermasks." else: from twitter import twitter try: twitter.update_status(str(args)) ircmessage = _usermask_to_username(usermask) + ": tweet sent: " + str(args) ttymessage = "tweet should have been sent." except: ircmessage = _usermask_to_username(usermask) + ": something went wrong when trying to tweet." ttymessage = "tweet not sent. something went wrong." return ircmessage, ttymessage
def on_success(self, data): if 'text' in data: username = data['user']['screen_name'] print('@{} tweeted the keyword.'.format(username)) from twitter import ( message, twitter ) finalPrint = '@' finalPrint += username finalPrint += '\n' finalPrint += message try: a = twitter.update_status(status=finalPrint, in_reply_to_status_id=data['id']) print('Tweet sent!') except TwythonError as e: print('ERROR: The tweet could not be sent.')
stdout.write('done!\n') stdout.flush() ### MAIN LOOP while True: print 'Starting loop!' print 'Updating boardnames...' boardnames = update_boards(boardnames) for board in boards: board.update() next_GET = repdigits.nextget(board.new_post['no'], 6) #if (board.next_GET != next_GET): #check if the GET is over #board.next_GET = next_GET #why did i put this in? =[ posts_to_go = board.next_GET - board.new_post['no'] posts_to_go = round(posts_to_go, 1-len(str(posts_to_go))) #round to the first digit min_until = (board.time_GET_occurs - datetime.utcnow()).total_seconds()/60.0 print "Time until /"+board.board+'/ '+GET_name(board.next_GET)+": ", str(timedelta(minutes=round(min_until))) if (min_until < MINUTES_GET_IS_SOON and min_until > MINUTES_GET_IS_UPON_US): if board.tweeted == False: print 'Tweeting!' msg = GET_TEXT.format(board=board.board, time_until=int(min_until), get_name=GET_name(board.next_GET), posts_to_go=int(posts_to_go)).capitalize() twitter.update_status(status=msg) #could add another tweepy error handler print msg else: print 'Already tweeted.' sleep(2) #to meet 4chan API timing standards print 'Sleeping 10 minutes...' sleep(10*60)
def update_years_saved(time_saved): with open('/home/piwheels/time_saved.txt', 'w') as f: f.write('{}'.format(time_saved)) def roundup(n): years = 25 return ((n // years) + 1) * years years_saved_prev = get_years_saved() logger.debug('years saved prev: {}'.format(years_saved_prev)) next_milestone = roundup(years_saved_prev) logger.debug('next milestone: {}'.format(next_milestone)) time_saved_now = db.get_total_time_saved() logger.debug('time saved now: {}'.format(time_saved_now)) years_saved = int(time_saved_now.split()[0]) if years_saved >= next_milestone: tweet = 'piwheels.org has now saved over {} years of build time'.format( next_milestone) logger.info('Tweeting: {}'.format(tweet)) twitter.update_status(status=tweet) else: logger.info('Not tweeting: {:,} < {:,}'.format(years_saved, next_milestone)) update_years_saved(years_saved)