|
| 1 | +import random |
| 2 | +import tweepy |
| 3 | + |
| 4 | +consumer_key="type_your_key_here" |
| 5 | +consumer_secret="type_your_key_secret_here" |
| 6 | +access_token="type_your_token_here" |
| 7 | +access_token_secret="type_your_token_secret_here" |
| 8 | + |
| 9 | +filename="quote.txt" # a text file which contains quotes (one quote in each line) |
| 10 | + |
| 11 | +def file_read(filename): # this function takes file name as input and returns list of string |
| 12 | + data_link=open(filename,"r") |
| 13 | + dt = data_link.readlines() |
| 14 | + for idx in range(len(dt)): |
| 15 | + dt[idx] = dt[idx][:-1] # this is used to remove \n |
| 16 | + return dt |
| 17 | + |
| 18 | +def select_tweet(list_tweet): # this function takes list of strings as input and then return a randomly selected element of input list |
| 19 | + sz = len(list_tweet) |
| 20 | + idx = random.randint(0,sz-1) # sz-1 is used because index of list starts from 0 and goes to N-1 for N elements |
| 21 | + msg = list_tweet[idx] |
| 22 | + return msg |
| 23 | + |
| 24 | +def start(consumer_key,consumer_secret,access_token,access_token_secret,filename="quote.txt"): |
| 25 | + auth = tweepy.OAuthHandler(consumer_key,consumer_secret) |
| 26 | + auth.set_access_token(access_token,access_token_secret) |
| 27 | + api = tweepy.API(auth,wait_on_rate_limit=True,wait_on_rate_limit_notify=True,compression=True) |
| 28 | + file_data = file_read(filename) |
| 29 | + msg=select_tweet(file_data) |
| 30 | + api.update_status(msg) |
| 31 | + |
| 32 | +start(consumer_key,consumer_secret,access_token,access_token_secret,filename) |
0 commit comments