├── README.md └── twitter_whitelist_bot.py /README.md: -------------------------------------------------------------------------------- 1 | # twitter_whitelist_bot 2 | 3 | 使用说明: https://mirror.xyz/zlexdl.eth/QkH4X-mj6EBfwvSsUOxluYbaX782_h4QijGdlxHnYiQ 4 | -------------------------------------------------------------------------------- /twitter_whitelist_bot.py: -------------------------------------------------------------------------------- 1 | import tweepy 2 | import time 3 | 4 | from sqlalchemy import create_engine 5 | from sqlalchemy.ext.declarative import declarative_base 6 | from sqlalchemy import Column, BigInteger, Integer, String, Text, DateTime, ForeignKey, Float 7 | from sqlalchemy.orm import sessionmaker 8 | from datetime import datetime 9 | import random 10 | 11 | engine = create_engine('mysql+pymysql://root:123456@127.0.0.1:3306/db?charset=utf8') 12 | Base = declarative_base() 13 | Session = sessionmaker(bind=engine) 14 | session = Session() 15 | 16 | consumer_key = '替换成你的consumer_key' 17 | consumer_secret = '替换成你的consumer_secret' 18 | 19 | key = '替换成你的key' 20 | secret = 'secret' 21 | 22 | auth = tweepy.OAuthHandler(consumer_key, 23 | consumer_secret) 24 | auth.set_access_token(key, 25 | secret) 26 | 27 | api = tweepy.API(auth) 28 | 29 | 30 | class TwitterBot(Base): 31 | __tablename__ = "twitter_bot" # 数据库中保存的表名字 32 | 33 | id = Column(Integer, index=True, primary_key=True) 34 | tweet_id = Column(Integer, nullable=True) 35 | screen_name = Column(String(200), nullable=True) 36 | url = Column(String(300), nullable=True) 37 | updated_at = Column(DateTime, default=datetime.now) 38 | 39 | 40 | while True: 41 | print("@@@@@@@@@@@@@@@@@@@@Start@@@@@@@@@@@@@@@@@@@@@@") 42 | print(datetime.utcnow()) 43 | 44 | public_tweets = [] 45 | 46 | try: 47 | # public_tweets = api.home_timeline(count=50, tweet_mode='extended') 48 | # public_tweets = api.user_timeline(screen_name='reinkerte31', count=3, tweet_mode='extended') 49 | public_tweets = api.list_timeline(list_id=1444116199432806401, count=100, tweet_mode='extended') 50 | 51 | except Exception as e: 52 | print(str(e)) 53 | print("sleep 60s") 54 | time.sleep(60) 55 | continue 56 | 57 | for tweet in public_tweets: 58 | 59 | if session.query(TwitterBot).filter(TwitterBot.tweet_id == tweet.id).count() > 0: 60 | print(str(tweet.id) + "已存在。") 61 | continue 62 | 63 | print("id=" + str(tweet.id)) 64 | print("created_at=" + str(tweet.created_at)) 65 | 66 | p = tweet.full_text 67 | keywords = 'Follow,Like,RT,Tag,Retweet,FOLLOW,LIKE,RETWEET,TAG,关注,转推,喜欢' 68 | 69 | count = sum([1 if w in p and w else 0 for w in keywords.split(',')]) 70 | if count > 1: 71 | print("---------------------Found 白名单推文") 72 | print(tweet.full_text) 73 | 74 | user_mentions = tweet.entities['user_mentions'] 75 | for friend in user_mentions: 76 | screen_name = friend['screen_name'] 77 | screen_names = ["zlexdl", screen_name] 78 | friendships = api.lookup_friendships(screen_name=screen_names) 79 | 80 | if len(friendships) > 1: 81 | 82 | if not friendships[1].is_following: 83 | print("Following <" + screen_name + "> ") 84 | api.create_friendship(screen_name=screen_name) 85 | 86 | print("Follow <" + screen_name + "> success!") 87 | else: 88 | print("Already following <" + screen_name + "> !") 89 | 90 | try: 91 | api.create_favorite(id=tweet.id_str) 92 | except Exception as e: 93 | print(str(e)) 94 | tweets = [ 95 | 'It would be an honor to be a part of your project! You are frontrunners in the game, you have in me a loyal supporter who always gives. Keep it up, much love! @petechang1113 @abc_noName1 @kevinLiuA1110 @tastydogclub @Adidasshow78 @mike1021031', 96 | '@Tony34108142 @waynechen2032 @Macnotmc1 @sodassdf @Ro0dZz @chou22389047 @stone20213 if my luck could ever carry me now would be the time', 97 | '@itivitimonster @Malachi007 @Ivanyichen @SawadyQ @jayfans15 @RoyLiu68727021 @havel_wu Excited to be a part of it. Hopefully your first following will be WL and OGd for appreciation 🙏'] 98 | 99 | message = random.choice(tweets) 100 | url = str("https://twitter.com/" + tweet.user.screen_name + "/status/" + tweet.id_str) 101 | print(url) 102 | re = api.update_status(message, attachment_url=url) 103 | print("转推结果:" + str(re.is_quote_status)) 104 | 105 | twitterBot = TwitterBot( 106 | screen_name=tweet.user.screen_name, 107 | url=url, 108 | tweet_id=tweet.id) 109 | print(tweet.id) 110 | session.add(twitterBot) 111 | session.commit() 112 | 113 | print("time.sleep(300) start time=" + str(datetime.utcnow())) 114 | time.sleep(300) 115 | 116 | print("time.sleep(300) start time=" + str(datetime.utcnow())) 117 | time.sleep(300) 118 | print("@@@@@@@@@@@@@@@@@@@@END@@@@@@@@@@@@@@@@@@@@@@") 119 | 120 | --------------------------------------------------------------------------------