def summarize_direct_messages(api, direct_messages, last_timestamp, timezone): sum_dms = list() if (len(direct_messages) != 0) and ('events' in direct_messages): # Summaraize DMs from the latest timestamp to the last timestamp for direct_message in direct_messages['events']: if direct_message['created_timestamp'] <= last_timestamp: break timestamp = (str)(datetime.datetime.fromtimestamp( (int)(direct_message['created_timestamp'][0:10]), datetime.timezone(datetime.timedelta(hours=timezone)))) user_info = tbc.get_userinfo_from_user_id( api, direct_message['message_create']['sender_id']) sum_dm = { 'dm_id': direct_message['id'], 'timestamp': timestamp, 'sender_info': user_info['name'] + '(@' + user_info['screen_name'] + ')', 'text': direct_message['message_create']['message_data']['text'] } sum_dms.append(sum_dm) # Wait a few minutes to keep not to ban my account tbc.log('i', mes='Waiting: time.sleep({it}), users/show'.format( it=CONFIG['interval_time'])) time.sleep(CONFIG['interval_time']) return sum_dms
def write_updated_friend_list(unfollow, filename): # Load friend list friends = list() try: with open(filename, mode='r', encoding='utf-8_sig') as f: reader = csv.reader(f, delimiter=',', quoting=csv.QUOTE_ALL) for row in reader: friend = dict() friend['user_id'] = int(row[0]) friend['screen_name'] = row[1] friend['name'] = row[2] friends.append(friend) except Exception as e: tbc.log('e', mes=e, func=str( sys._getframe().f_code.co_name), hdlg=tbc.ABORT) # Remove destroy friends from friend list updated_friendlist = list() for friend in friends: if friend['user_id'] != unfollow['user_id']: updated_friendlist.append(friend) # Update friend list # user_id, screen_name, name with open(filename, mode='w', encoding="utf-8_sig") as f: writer = csv.writer(f, delimiter=',', quotechar='"', lineterminator='\n', quoting=csv.QUOTE_ALL) for friend in updated_friendlist: writer.writerow( [friend['user_id'], friend['screen_name'], friend['name']]) return
def print_progress(index, result): mess = list() mess += ['---------- [{m1}] ----------------------------------------'.format( m1=(index + 1))] userstr = tbc.get_userstr(user_id=result['unfollow']['user_id'], screen_name=result['unfollow']['screen_name'], name=result['unfollow']['name']) mess += ['unfollow: {m1} -> {m2}'.format( m1=userstr, m2=result['unfollow']['comment'])] for mes in mess: tbc.log('i', mes=mes) return
def print_progress(index, result): mess = list() mess += ['---------- [{m1}] ----------------------------------------'.format( m1=(index + 1))] mess += ['tweet_url: {m1}'.format(m1=result['retweet']['tweet_url'])] mess += ['tweet_text: {m1}'.format(m1=result['retweet']['tweet_text'])] mess += ['retweet: {m1}'.format(m1=result['retweet']['comment'])] for follow in result['follows']: userstr = tbc.get_userstr( user_id=follow['user_id'], screen_name=follow['screen_name'], name=follow['name']) mess += ['follow: {m1} -> {m2}'.format(m1=userstr, m2=follow['comment'])] for mes in mess: tbc.log('i', mes=mes) return
#!/usr/bin/env python3 # -*- coding: utf-8 -*- # This bot is for automatically to destroy your all friendships. import os import time import tweepy import tb_common as tbc import tb_unfollow if __name__ == '__main__': tbc.log_init() tbc.log('i', mes='START SCRIPT') # Load configurations from the JSON file. CONFIG = tbc.load_config() # Set result file name result_filename = '{result_dir}/destroy_all_friendship_{datetime}.csv'.format( result_dir=CONFIG['result_dir'], datetime=tbc.get_timenow()) # Initialize the API api = tbc.twp_get_api(CONFIG['consumer_key'], CONFIG['consumer_secret'], CONFIG['access_token'], CONFIG['access_token_secret']) # Count of my friends friends_before = len(api.friends_ids(api.me().screen_name))
for keywords in keywords2d) return result_is_contest def extract_screen_names(tweet): result_screen_names = list() at_screen_names = re.findall(r'@[a-z|A-Z|0-9|_]{1,15}', tweet) for at_screen_name in at_screen_names: screen_name = at_screen_name.replace('@', '') result_screen_names.append(screen_name) return result_screen_names if __name__ == '__main__': tbc.log_init() tbc.log('i', mes='START SCRIPT') # Load configurations from the JSON file. CONFIG = tbc.load_config() # Initialize the API api = tbc.twp_get_api(CONFIG['consumer_key'], CONFIG['consumer_secret'], CONFIG['access_token'], CONFIG['access_token_secret']) # Make tweet list tweets = list() # Collect tweets from my timeline CT = CONFIG['entry_contest']['collect_tweets']['timeline'] if CT['is_enable']:
writer = csv.writer(f, delimiter=',', quotechar='"', lineterminator='\n', quoting=csv.QUOTE_ALL) for follow in result['follows']: row = list() row.extend(row_head) row += [tbc.get_userstr(follow['user_id'], follow['screen_name'], follow['name'])] row += [follow['is_followed']] row += [follow['comment']] writer.writerow(row) return if __name__ == '__main__': tbc.log_init() tbc.log('i', mes='START SCRIPT') # Load configurations from the JSON file. CONFIG = tbc.load_config() # Set result file name result_filename = '{result_dir}/entry_{datetime}.csv'.format( result_dir=CONFIG['result_dir'], datetime=tbc.get_timenow()) # Load contests contests = list() try: with open(CONFIG['entry_contest']['contest_list_filename'], mode='r', encoding='utf-8_sig') as f: reader = csv.reader(f, delimiter=',', quoting=csv.QUOTE_ALL) for row in reader: contest = dict()
body += border body += '==============================' + '\n' return subject, body def get_latest_timestamp(direct_messages): timestamp = '' if (len(direct_messages) != 0) and ('events' in direct_messages): if len(direct_messages['events']) != 0: timestamp = direct_messages['events'][0]['created_timestamp'] return timestamp if __name__ == '__main__': tbc.log_init() tbc.log('i', mes='START SCRIPT') # Load configurations CONFIG = tbc.load_config() SUM_LAST_TIMESTAMP = '' try: with open(CONFIG['sum']['last_timestamp_filename'], mode='r', encoding='utf-8_sig') as f: SUM_LAST_TIMESTAMP = f.readline().strip() except Exception as e: tbc.log('e', mes=e, func=str(sys._getframe().f_code.co_name), hdlg=tbc.CONTD)
# userstr, is_unfollowed, comment with open(filename, mode='a', encoding="utf-8_sig") as f: writer = csv.writer(f, delimiter=',', quotechar='"', lineterminator='\n', quoting=csv.QUOTE_ALL) row = list() row += [tbc.get_userstr(result['unfollow']['user_id'], result['unfollow']['screen_name'], result['unfollow']['name'])] row += [result['unfollow']['is_destroyed']] row += [result['unfollow']['comment']] writer.writerow(row) return if __name__ == '__main__': tbc.log_init() tbc.log('i', mes='START SCRIPT') # Load configurations from the JSON file. CONFIG = tbc.load_config() # Set result file name result_filename = '{result_dir}/unfollow_{datetime}.csv'.format( result_dir=CONFIG['result_dir'], datetime=tbc.get_timenow()) # Load friend list friends = list() try: with open(CONFIG['destroy_friendship']['destroy_list_filename'], mode='r', encoding='utf-8_sig') as f: reader = csv.reader(f, delimiter=',', quoting=csv.QUOTE_ALL) for row in reader: friend = dict()