Advertisement
Guest User

Untitled

a guest
Jan 16th, 2019
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.11 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # encoding: utf-8
  3. #https://github.com/tweepy/tweepy
  4. import tweepy
  5. import csv
  6. import homoglyphs as hg
  7.  
  8. #Twitter API credentials
  9. consumer_key = "QJoiPPoTBjZ1nCz0EaUKCJlvI"
  10. consumer_secret = "pGBFjyQDywvaNg1eVVno41A6fsBkLYGa9X6vuCaYxtqthOFq22"
  11. access_key = "1054176018733371393-bkOeAbtz5hsGRqSAE1hhyDujusp1Li"
  12. access_secret = "RzkXI7sIu0OGmaCjLYs3ahDS8Aw70UD5PzMxjHrAepojB"
  13.  
  14.  
  15. def get_all_tweets(screen_name):
  16. #Twitter only allows access to a users most recent 3240 tweets with this method
  17.  
  18. #authorize twitter, initialize tweepy
  19. auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
  20. auth.set_access_token(access_key, access_secret)
  21. api = tweepy.API(auth)
  22.  
  23. #initialize a list to hold all the tweepy Tweets
  24. alltweets = []
  25.  
  26. #make initial request for most recent tweets (200 is the maximum allowed count)
  27. new_tweets = api.user_timeline(screen_name = screen_name,count=200)
  28.  
  29. #save most recent tweets
  30. alltweets.extend(new_tweets)
  31.  
  32. #save the id of the oldest tweet less one
  33. oldest = alltweets[-1].id - 1
  34.  
  35. #keep grabbing tweets until there are no tweets left to grab
  36. while len(new_tweets) > 0:
  37. print("getting tweets before %s" % (oldest))
  38.  
  39. #all subsiquent requests use the max_id param to prevent duplicates
  40. new_tweets = api.user_timeline(screen_name = screen_name,count=200,max_id=oldest)
  41.  
  42. #save most recent tweets
  43. alltweets.extend(new_tweets)
  44.  
  45. #update the id of the oldest tweet less one
  46. oldest = alltweets[-1].id - 1
  47.  
  48. print("...%s tweets downloaded so far" % (len(alltweets)))
  49.  
  50.  
  51. #transform the tweepy tweets into a 2D array that will populate the csv
  52. #outtweets = [[tweet.id_str, tweet.created_at, tweet.text.encode("utf-8")] for tweet in alltweets]
  53. outtweets = [[tweet.coordinates] for tweet in alltweets]
  54. with open('tweets.txt', 'w') as f:
  55. for item in outtweets:
  56. f.write("%s\n" % item)
  57.  
  58.  
  59.  
  60. f.close()
  61.  
  62.  
  63.  
  64.  
  65. if __name__ == '__main__':
  66. #pass in the username of the account you want to download
  67. get_all_tweets("miounster")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement