-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtweetvoice.py
More file actions
56 lines (44 loc) · 1.78 KB
/
tweetvoice.py
File metadata and controls
56 lines (44 loc) · 1.78 KB
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
import speech_recognition as sr
import tweepy
import webbrowser
import subprocess
# Authentification
auth = tweepy.OAuth1UserHandler(consumer_key, consumer_secret,access_token,access_token_secret)
api = tweepy.API(auth)
# Initialize recognizer class (for recognizing the speech)
r = sr.Recognizer()
def search_tweets(api, keyword):
tweets = tweepy.Cursor(api.search_tweets, q=keyword).items(10)
for tweet in tweets:
print(tweet.text)
while True:
# Reading Microphone as source
# listening the speech and store in audio_text variable
with sr.Microphone() as source:
print("Talk")
audio_text = r.listen(source)
print("Time over, thanks")
# recoginize_() method will throw a request error if the API is unreachable, hence using exception handling
# using google speech recognition
text = r.recognize_google(audio_text, language = 'en-EN')
print("Text: "+ text)
if text == "tweet":
print("What do you want to tweet?")
audio_text = r.listen(source)
tweet_text = r.reconigze_google(audio_text)
auth = tweepy.OAuth1UserHandler() # Replace this with API and Access Token keys
api = tweepy.API(auth)
api.update_status(tweet_text)
print("Successfully tweeted: " + tweet_text)
elif text == "open browser":
webbrowser.open("https://www.google.com")
print("Browser opened")
elif text == "BBC News":
subprocess.run(["python", "bbcbot.py"])
print("Script ran")
elif text == "search":
keyword = input("Enter keyword: ")
search_tweets(api, keyword)
elif text == "exit":
break;
print("Exiting..")