Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

main.py 1.5 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
  1. import tweepy
  2. from tweepy import OAuthHandler
  3. import logging
  4. import csv
  5. API = {
  6. 'consumer_key': 'dYgo8B2sDLruMrVXR0Kd7Tp5N',
  7. 'consumer_secret': '5Yb6v2v4Y4zulq8fXL5iRuuTK4G0df48AoQI36B7XTm0cw1sIT',
  8. 'access_token': '1204735114581827585-OLFpb3m4IHKmRlZ0kQM1j5LMRhNCyB',
  9. 'access_token_secret': 'JXQcqSP15ihwjYDzyub9sWJFRwIjuUolhlSOlj5IlhjkP'
  10. }
  11. class TwitterClient:
  12. def __init__(self):
  13. '''API connector'''
  14. self._consumer_key = API['consumer_key']
  15. self._consumer_secret = API['consumer_secret']
  16. self._access_token = API['access_token']
  17. self._access_token_secret = API['access_token_secret']
  18. try:
  19. '''API Authentication'''
  20. self.auth = OAuthHandler(self._consumer_key, self._consumer_secret)
  21. self.auth.set_access_token(self._access_token, self._access_token_secret)
  22. self.api = tweepy.API(self.auth, wait_on_rate_limit=True)
  23. except Exception:
  24. logging.error('Authentication Failed')
  25. def get_tweets(self, hashtag):
  26. with open('./data/tweets.csv', 'a') as csv_file:
  27. csv_writer = csv.writer(csv_file)
  28. csv_writer.writerow(["Date", "Text"])
  29. for tweet in tweepy.Cursor(self.api.search, q=hashtag, count=1, lang='es', since="2021-01-01").items(1):
  30. print(tweet)
  31. csv_writer.writerow([tweet.created_at, tweet.text.encode('utf-8')])
  32. if __name__ == '__main__':
  33. twitter_client = TwitterClient()
  34. twitter_client.get_tweets("%23TurismoCity")
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...