Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from logging import ERROR, error
- import re
- import socDemBotConfig
- import tweepy
- import time
- count = 0
- i = 0
- def searchTweets(client, query):
- response = None
- try:
- # web scapes and loads query in list for retweets
- response = client.search_recent_tweets(query=query, max_results=10, tweet_fields=[
- 'created_at', 'lang'], expansions=['author_id'])
- except:
- print("trying to load tweets")
- time.sleep(10)
- searchTweets(client, query)
- print("got tweets")
- time.sleep(5)
- return response
- def getTweetToLike(tweets, i):
- print(tweets[i])
- print(tweets[i].type())
- tweetToLike = tweets[i]
- # print(tweetToLike.type())
- return tweetToLike
- def sendTweet(count, tweet):
- print(count)
- print(tweet)
- # print(tweetString.type())
- try:
- if (tweetLowerCase.find('#universalhealthcare') != -1):
- client.create_tweet(in_reply_to_tweet_id=tweet.id,
- 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/")
- client.like(tweet.id, user_auth=True)
- print(tweet.id)
- client.retweet(tweet.id, user_auth=True)
- # client.create_tweet(text=newList[count])
- time.sleep(10)
- # catches the tweet if laptop doesn't have internet connection and keeps trying
- except:
- print("try again soon")
- time.sleep(10)
- sendTweet(count, tweet)
- return count
- # creates client for scraping
- client = tweepy.Client(
- bearer_token=socDemBotConfig.BEARER_TOKEN, wait_on_rate_limit=True)
- # creates new query for retweets
- query = '#universalhealthcare -is:retweet OR #publichousing -is:retweet OR #healthcareforall -is:retweet'
- tweets = searchTweets(client, query)
- # print(tweets.data)
- client = tweepy.Client(consumer_key=socDemBotConfig.CONSUMER_KEY,
- consumer_secret=socDemBotConfig.CONSUMER_SECRET,
- access_token=socDemBotConfig.ACCESS_KEY,
- access_token_secret=socDemBotConfig.ACCESS_SECRET,
- wait_on_rate_limit=True)
- users = {u['id']: u for u in tweets.includes['users']}
- file = open('/Users/dwlakes/Desktop/bots/socdembot/testTweets.txt', 'r')
- f = file.readlines()
- newList = []
- for line in f:
- if not line.strip():
- continue
- if line[-1] == '\n':
- newList.append(line[:-1])
- else:
- newList.append(line)
- tweet = getTweetToLike(tweets, i)
- sendTweet(count, tweet)
- count += 1
- i += 1
Advertisement
Add Comment
Please, Sign In to add comment