-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetTrends.py
68 lines (57 loc) · 1.94 KB
/
getTrends.py
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# import necessary libraries
import tweepy
import time
import random
import datetime
from datetime import date
# Access the credentials file and store all keys and tokens in different variables
# Assign the values correctly
all_keys = open('credentials', 'r').read().splitlines()
api_key = all_keys[0]
api_key_secret = all_keys[1]
access_token = all_keys[2]
access_token_secret = all_keys[3]
# Authenticate to Twitter
auth = tweepy.OAuthHandler(api_key, api_key_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth, wait_on_rate_limit=True)
# Testing credentials authentication
try:
api.verify_credentials()
print("Authentication Successful")
except:
print("Authentication Error")
# WOEID of Ghaziabad
# woeid = 44418
woeid = 23424848
# Worldwide trends
# woeid = 1
# fetching the trends
trends = api.get_place_trends(id=woeid)
# printing the information
print("The top trends for India are :")
tweetTrend = "The top trends for India today are : \n "
for value in trends:
for trend in value['trends']:
print(trend['name'])
# api.update_status(trend['name'])
tweetTrend += "\n"
tweetTrend += str(trend['name'])
print("\n")
print(tweetTrend)
# Finding total length of string of trends created
tweetLength = len(tweetTrend)
print("Length is : " + str(tweetLength))
# Reducing tweet length to 280 characters because Twitter allows to post only 280 characters at once
# Limit tweet length by removing excess trailing characters
final = tweetTrend[:280]
print("Final length of the tweet is " + str(len(final)))
# Removing 280 leading characters
final2 = tweetTrend[280:]
# print(len(final2))
final3 = final2[:280]
# print(len(final3))
today = date.today()
api.update_status("Today's date is : " + str(today))
tweet1 = api.update_status(status=final)
# tweet2 = api.update_status(status=final3, in_reply_to_status_id=tweet1.id)