Sparrow1029

Twitter contest: Hashtag scrape

Feb 1st, 2018
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.21 KB | None | 0 0
  1. """This script scrapes Twitter for our #AStarWarsComicContest hashtag and saves a
  2. List of entrants."""
  3.  
  4. from twitter import Twitter, OAuth
  5.  
  6. with open('twitOAuth.txt', 'r+') as f:
  7.     keys = [line for line in f.read().splitlines()]
  8.  
  9. CONSUMER_KEY = keys[0]
  10. CONSUMER_SECRET = keys[1]
  11. ACCESS_TOKEN = keys[2]
  12. ACCESS_SECRET = keys[3]
  13.  
  14. oauth = OAuth(ACCESS_TOKEN, ACCESS_SECRET, CONSUMER_KEY, CONSUMER_SECRET)
  15. t = Twitter(auth=oauth)
  16.  
  17. # Here's the hashtag query
  18. query = t.search.tweets(q='%23AStarWarsComicContest', tweet_mode='extended')
  19.  
  20. comic_url = 'astarwarscomic.com/comics'
  21. entrants = []
  22.  
  23. for s in query['statuses']:
  24.     """Main search for tweets."""
  25.     tweet_id = s['id']
  26.     username = s['user']['screen_name']
  27.  
  28.     entities = s['entities']
  29.     urls = [url['expanded_url'] for url in entities['urls']]
  30.     mentions = [m['screen_name'] for m in entities['user_mentions']]
  31.     text = s['full_text']
  32.  
  33.     if any(comic_url in u for u in urls) and 'astarwarscomic' in mentions:
  34.         print(tweet_id)
  35.         print(username)
  36.         print(urls)
  37.         print(mentions)
  38.         print(text)
  39.         print("\n\n")
  40.  
  41.         entrants.append(username)
  42.  
  43. print("{}\n{}".format(len(entrants), entrants))
Advertisement
Add Comment
Please, Sign In to add comment