dwlakes

Untitled

Mar 9th, 2022
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.68 KB | None | 0 0
  1. from logging import ERROR, error
  2.  
  3. import re
  4. import socDemBotConfig
  5. import tweepy
  6. import time
  7.  
  8. count = 0
  9. i = 0
  10.  
  11.  
  12. def searchTweets(client, query):
  13.  
  14.     response = None
  15.     try:
  16.         # web scapes and loads query in list for retweets
  17.         response = client.search_recent_tweets(query=query, max_results=10, tweet_fields=[
  18.             'created_at', 'lang'], expansions=['author_id'])
  19.     except:
  20.         print("trying to load tweets")
  21.         time.sleep(10)
  22.         searchTweets(client, query)
  23.  
  24.     print("got tweets")
  25.     time.sleep(5)
  26.     return response
  27.  
  28.  
  29. def getTweetToLike(tweets, i):
  30.     print(tweets[i])
  31.     print(tweets[i].type())
  32.     tweetToLike = tweets[i]
  33.     # print(tweetToLike.type())
  34.     return tweetToLike
  35.  
  36.  
  37. def sendTweet(count, tweet):
  38.     print(count)
  39.     print(tweet)
  40.     # print(tweetString.type())
  41.     try:
  42.         if (tweetLowerCase.find('#universalhealthcare') != -1):
  43.             client.create_tweet(in_reply_to_tweet_id=tweet.id,
  44.                                 text="Hey! Did you know that most developed nations adopted some form of universal healthcare before 1980? Read more here: https://truecostblog.com/2009/08/09/countries-with-universal-healthcare-by-date/")
  45.  
  46.         client.like(tweet.id, user_auth=True)
  47.         print(tweet.id)
  48.         client.retweet(tweet.id, user_auth=True)
  49.         # client.create_tweet(text=newList[count])
  50.         time.sleep(10)
  51.     # catches the tweet if laptop doesn't have internet connection and keeps trying
  52.     except:
  53.         print("try again soon")
  54.         time.sleep(10)
  55.         sendTweet(count, tweet)
  56.     return count
  57.  
  58.  
  59. # creates client for scraping
  60. client = tweepy.Client(
  61.     bearer_token=socDemBotConfig.BEARER_TOKEN, wait_on_rate_limit=True)
  62.  
  63. # creates new query for retweets
  64. query = '#universalhealthcare -is:retweet OR #publichousing -is:retweet OR #healthcareforall -is:retweet'
  65.  
  66. tweets = searchTweets(client, query)
  67. # print(tweets.data)
  68.  
  69.  
  70. client = tweepy.Client(consumer_key=socDemBotConfig.CONSUMER_KEY,
  71.                        consumer_secret=socDemBotConfig.CONSUMER_SECRET,
  72.                        access_token=socDemBotConfig.ACCESS_KEY,
  73.                        access_token_secret=socDemBotConfig.ACCESS_SECRET,
  74.                        wait_on_rate_limit=True)
  75.  
  76. users = {u['id']: u for u in tweets.includes['users']}
  77.  
  78. file = open('/Users/dwlakes/Desktop/bots/socdembot/testTweets.txt', 'r')
  79. f = file.readlines()
  80. newList = []
  81. for line in f:
  82.  
  83.     if not line.strip():
  84.         continue
  85.     if line[-1] == '\n':
  86.         newList.append(line[:-1])
  87.     else:
  88.         newList.append(line)
  89.     tweet = getTweetToLike(tweets, i)
  90.     sendTweet(count, tweet)
  91.     count += 1
  92.     i += 1
  93.  
Advertisement
Add Comment
Please, Sign In to add comment