StormFalcon32

PremiumAPI

Feb 11th, 2020
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.33 KB | None | 0 0
  1. from searchtweets import gen_rule_payload, load_credentials, collect_results
  2. import InputOutput as io
  3. import time
  4. import math
  5. from datetime import datetime, timedelta
  6. from email.utils import parsedate_tz
  7.  
  8.  
  9. def to_datetime(datestring):
  10.     time_tuple = parsedate_tz(datestring.strip())
  11.     dt = datetime(*time_tuple[:6])
  12.     return dt - timedelta(seconds=time_tuple[-1])
  13.  
  14.  
  15. premium_search_args = load_credentials(r'D:\Python\Twitter\twitter_keys.yaml',
  16.                                        yaml_key="search_tweets_premium",
  17.                                        env_overwrite=False)
  18.  
  19.  
  20. def scrape_tweets(keyword, low, high):
  21.     while low < high:
  22.         num = 0
  23.         results_per_call = 500
  24.         max_results = 500
  25.         pt_rule = keyword + ' lang:en'
  26.         low_string = low.strftime('%Y%m%d%H%M')
  27.         high_string = high.strftime('%Y%m%d%H%M')
  28.         rule = gen_rule_payload(pt_rule=pt_rule, from_date=low_string,
  29.                                 to_date=high_string, results_per_call=results_per_call)
  30.         tweets = collect_results(rule, max_results=max_results,
  31.                                  result_stream_args=premium_search_args)
  32.         end = tweets[len(tweets) - 1].created_at_datetime
  33.         for tweet in tweets:
  34.             if tweet.lang != 'en':
  35.                 continue
  36.             is_retweet = False
  37.             orig_length = 0
  38.             has_comment = False
  39.             if 'retweeted_status' in tweet:
  40.                 is_retweet = True
  41.                 if 'extended_tweet' in tweet['retweeted_status']:
  42.                     orig_length = len(
  43.                         tweet['retweeted_status']['extended_tweet']['full_text'])
  44.                 else:
  45.                     orig_length = len(tweet['retweeted_status']['text'])
  46.             if orig_length > 0 and orig_length < len(tweet.all_text):
  47.                 has_comment = True
  48.             lat = ''
  49.             long = ''
  50.             if tweet.geo_coordinates is not None:
  51.                 lat = tweet.geo_coordinates['latitude']
  52.                 long = tweet.geo_coordinates['longitude']
  53.             io.output_file(['id', 'date', 'text', 'lat', 'long', 'likes', 'replies', 'retweets', 'is_retweet',
  54.                             'has_comment', 'user_id', 'user_name', 'user_date', 'bio', 'location'],
  55.                            [tweet.id, str(tweet.created_at_datetime), tweet.all_text, lat, long,
  56.                                tweet.favorite_count, tweet['reply_count'], tweet.retweet_count, is_retweet, has_comment,
  57.                                tweet.user_id, tweet.screen_name, str(
  58.                                to_datetime(tweet['user']['created_at'])),
  59.                                tweet.bio, tweet['user']['location']],
  60.                            print_columns=False
  61.                            )
  62.             num += 1
  63.         high = end - timedelta(seconds=1)
  64.         print(num)
  65.         print(high)
  66.         delay = max_results / results_per_call
  67.         time.sleep(delay)
  68.  
  69. # update key directory in code - good
  70. # update endpoint URL - good
  71. # update start and end date - good
  72. # update results per call and max results - good
  73. # update keyword - good
  74.  
  75.  
  76. if __name__ == "__main__":
  77.     keyword = 'fat acceptance'
  78.     scrape_tweets(
  79.         keyword,
  80.         datetime(2010, 1, 1),
  81.         datetime(2010, 11, 4, hour=1, minute=43, second=0)
  82.     )
Add Comment
Please, Sign In to add comment