Advertisement
Guest User

Untitled

a guest
Sep 13th, 2017
764
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.23 KB | None | 0 0
  1. #BOT BY @TYLERGLAIEL
  2. #@WIKIHOWDREAMS
  3. #PICKS A RANDOM WIKIHOW IMAGE AND CAPTIONS IT WITH A RANDOM TWEET
  4.  
  5. import requests
  6. from bs4 import BeautifulSoup
  7. import random
  8. import shutil
  9. from PIL import Image
  10. import tweepy
  11. import time
  12.  
  13. last_chose_tweet_index = "0"
  14. time_between_tweets = 60*60
  15. time_between_failed_tweets = 60 #if the tweet fails (exception) how long to wait before trying again
  16.  
  17. banned_users = ["WikihowDreams"]
  18. banned_words = ["http", "@", "&"]
  19. punctuation = [":", ".", "?", "!"]
  20.  
  21. def get_twitter():
  22.     CONSUMER_KEY = '123456789...'
  23.     CONSUMER_SECRET = '123456789...'
  24.     ACCESS_KEY = '123456789...'
  25.     ACCESS_SECRET = '123456789...'
  26.     auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
  27.     auth.set_access_token(ACCESS_KEY, ACCESS_SECRET)
  28.     return tweepy.API(auth)
  29.  
  30. def grabimage():
  31.     url = "http://www.wikihow.com/Special:Randomizer"
  32.     result = requests.get(url)
  33.     html = result.content
  34.     soup = BeautifulSoup(html, "html.parser")
  35.  
  36.     header = soup.find_all("img", {"class": "whcdn content-fill"}, src=True)
  37.  
  38.  
  39.     url = random.choice(header)['src']
  40.  
  41.     response = requests.get(url, stream=True)
  42.     with open('temp.jpg', 'wb') as out_file:
  43.         shutil.copyfileobj(response.raw, out_file)
  44.  
  45.        
  46.     img = Image.open("temp.jpg");
  47.     img2 = img.crop((0,0,img.size[0], img.size[1]-22))
  48.     img2.save("img.jpg")
  49.     return "img.jpg"
  50.  
  51. def grabtweet():
  52.     global last_chose_tweet_index
  53.     twitter = get_twitter()
  54.     results = twitter.search(q="\"How To\" -RT -http -https -t.co", lang="en", since_id=last_chose_tweet_index)
  55.    
  56.     #res = random.choice(results)
  57.     #print(res.text)
  58.     #print(res.id)
  59.     for i in range(0,100):
  60.         res = random.choice(results)
  61.         valid = False
  62.         text = res.text;
  63.        
  64.         #trim tweet down
  65.         croptext = text[text.lower().find("how to"):]
  66.         for p in punctuation:
  67.             index = croptext.find(p)
  68.             if(index != -1):
  69.                 croptext = croptext[:croptext.find(p)]
  70.            
  71.         #validate tweet
  72.         #tweet must contain "how to"
  73.         valid = True
  74.        
  75.         if(croptext.lower().find("how to") != 0):
  76.             valid = False
  77.            
  78.         #tweet must not contain banned words
  79.         for banned in banned_words:
  80.             if(croptext.lower().find(banned) != -1):
  81.                 valid = False
  82.                 break
  83.        
  84.         #tweet must not be from banned user
  85.         for banned in banned_users:
  86.             if(res.user.screen_name.lower() == banned.lower()):
  87.                 valid = False
  88.                
  89.         if(valid):
  90.             print(croptext)
  91.             last_chose_tweet_index = res.id
  92.             return croptext
  93.    
  94.     raise Exception("no valid tweet")
  95.            
  96. def make_and_send_tweet():
  97.     twitter = get_twitter()
  98.     image = grabimage()
  99.     tweet = grabtweet()
  100.     twitter.update_with_media(image, status=tweet)
  101.    
  102. def run_bot():
  103.     while True:
  104.         try:
  105.             make_and_send_tweet()
  106.             #sleep 1 hour
  107.             time.sleep(time_between_tweets)
  108.         except:
  109.             #sleep 1 minute
  110.             time.sleep(time_between_failed_tweets)
  111.        
  112. #make_and_send_tweet()
  113. run_bot()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement