Advertisement
Guest User

Joeeeeeboiiiii

a guest
Dec 11th, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.17 KB | None | 0 0
  1. import json
  2. import tweepy
  3. import csv
  4. import json
  5. # create a dictionary to store your twitter credentials
  6.  
  7. twitter_cred = dict()
  8.  
  9. # load Twitter API credentials
  10.  
  11. with open('twitter_creds.json') as cred_data:
  12. info = json.load(cred_data)
  13. consumer_key = info['CONSUMER_KEY']
  14. consumer_secret = info['CONSUMER_SECRET']
  15. access_key = info['ACCESS_KEY']
  16. access_secret = info['ACCESS_SECRET']
  17.  
  18. def get_all_tweets(screen_name):
  19.  
  20. # Twitter allows access to only 3240 tweets via this method
  21.  
  22. # Authorization and initialization
  23.  
  24. auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
  25. auth.set_access_token(access_key, access_secret)
  26. api = tweepy.API(auth)
  27.  
  28. # initialization of a list to hold all Tweets
  29.  
  30. all_the_tweets = []
  31.  
  32. # We will get the tweets with multiple requests of 10 tweets each
  33.  
  34. new_tweets = api.user_timeline(screen_name=screen_name, count=10)
  35.  
  36. # saving the most recent tweets
  37.  
  38. all_the_tweets.extend(new_tweets)
  39.  
  40. # save id of 1 less than the oldest tweet
  41. oldest_tweet = all_the_tweets[-1].id - 1
  42.  
  43. # grabbing tweets till none are left
  44. for i in range(10):
  45. # The max_id param will be used subsequently to prevent duplicates
  46. new_tweets = api.user_timeline(screen_name=screen_name,
  47. count=10, max_id=1)
  48.  
  49. # save most recent tweets
  50.  
  51. all_the_tweets.extend(new_tweets)
  52.  
  53. # id is updated to oldest tweet - 1 to keep track
  54.  
  55. oldest_tweet = all_the_tweets[-1].id - 1
  56. # transforming the tweets into a 2D array that will be used to populate the csv
  57.  
  58. outtweets = [[tweet.id_str, tweet.created_at,
  59. tweet.text.encode('utf-8')] for tweet in all_the_tweets]
  60.  
  61. global id__
  62. global date__
  63. global text__
  64.  
  65. id__ = []
  66. date__ = []
  67. text__ = []
  68.  
  69. for tweet in all_the_tweets:
  70. id__.append(tweet.id_str)
  71. date__.append(tweet.created_at)
  72. text__.append(tweet.text)
  73.  
  74.  
  75. if __name__ == '__main__':
  76.  
  77. # Enter the twitter handle of the person concerned
  78.  
  79. get_all_tweets(input("Enter the twitter handle of the person whose tweets you want to download:- "))
  80. #get_all_tweets = "battlefield"
  81.  
  82. def Cleanuptweets():
  83. global get_all_tweets
  84. global id__
  85. global date__
  86. global text__
  87. global link__
  88.  
  89. link__ = []
  90.  
  91. n = 0
  92.  
  93. for things in text__:
  94. word_list = text__[n].split()
  95.  
  96. # Find the link of the post from the text.
  97. link__.append(word_list[-1])
  98.  
  99. # Getting the link out of the text.
  100. change_this = text__[n]
  101. change_this.split(word_list[-1])
  102. remove_this = word_list[-1]
  103. text__[n] = change_this.strip(remove_this)
  104.  
  105. print("ID :", id__[n], "\n")
  106. print("Date:", date__[n], "\n")
  107. print("Text:", text__[n], "\n")
  108. print("Link:", link__[n], "\n")
  109.  
  110.  
  111.  
  112. n = n + 1
  113.  
  114. i = 0
  115.  
  116. for things in text__:
  117.  
  118. date___ = str(date__[i])
  119. text___ = str(text__[i])
  120. link___ = str(link__[i])
  121.  
  122. f = open("lots_of_tweets.html", "a+")
  123. f = f.write("""
  124. <div class="tweets">
  125. <div class="tweet">
  126. <div class="creator">
  127. <h1>""" + get_all_tweets + """</h1>
  128. </div>
  129. <div class="date">
  130. <p>""" + date___ + """</p>
  131. </div>
  132. <div class="text">
  133. <p>""" + text___ + """</p>
  134. </div>
  135. <div class="link">
  136. <a href='""" + link___ + """'></a>
  137. </div>
  138. </div>
  139. </div>
  140. """)
  141.  
  142. i = i + 1
  143.  
  144.  
  145. email = """
  146. <html>
  147. <head>
  148. <meta charset="utf-8">
  149. <meta name="viewpoint" content="width-divice-width">
  150. <meta name="description" content="All the tweets today.">
  151. <title>Tweets</title>
  152. <link rel="stylesheet" href="style.css">
  153. </head>
  154. <body>
  155.  
  156. </body>
  157. </html>
  158. """
  159.  
  160. f = open("Tweets.html", "w")
  161. f = f.write(email)
  162.  
  163.  
  164. Cleanuptweets()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement