forked from ab-anand/Bruno
-
Notifications
You must be signed in to change notification settings - Fork 0
/
daily.py
executable file
·162 lines (147 loc) · 5.8 KB
/
daily.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import wx
import os
# os.environ["HTTPS_PROXY"] = "http://user:pass@192.168.1.107:3128"
import wikipedia
import wolframalpha
import time
import webbrowser
import json
import requests
import ctypes
import random
from bs4 import BeautifulSoup
from urllib.request import urlopen
import speech_recognition as sr
import ssl
# Remove SSL error
requests.packages.urllib3.disable_warnings()
try:
_create_unverified_https_context = ssl._create_unverified_context
except AttributeError:
# Legacy Python that doesn't verify HTTPS certificates by default
pass
else:
# Handle target environment that doesn't support HTTPS verification
ssl._create_default_https_context = _create_unverified_https_context
headers = {'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6)\
AppleWebKit/537.36 (KHTML, like Gecko)\
Chrome/53.0.2785.143 Safari/537.36'}
# GUI creation
class MyFrame(wx.Frame):
def __init__(self):
print('''Welcome back Sir, Broono at your service.''')
while True:
self.OnEnter()
def OnEnter(self):
print("\nWaiting for command......")
put = input('** Enter command :') # self.txt.GetValue()
put = put.lower()
link = put.split()
if put == '':
print("Speak now...")
r = sr.Recognizer()
with sr.Microphone() as src:
audio = r.listen(src)
try:
put = r.recognize_google(audio)
put = put.lower()
link = put.split()
# self.txt.SetValue(put)
print("Command: ", put)
except sr.UnknownValueError:
print("Google Speech Recognition could not understand audio")
except sr.RequestError as e:
print("Could not request results from Google STT; {0}"
.format(e))
except Exception as e:
print(e)
print("Unknown exception occurred!")
# Open a webpage
if put.startswith('open '):
try:
print("opening "+link[1])
webbrowser.open('http://www.'+link[1]+'.com')
except Exception as e:
print(e)
print('Sorry, No Internet Connection!')
# Play Song on Youtube
elif put.startswith('play '):
try:
link = '+'.join(link[1:])
say = link.replace('+', ' ')
url = 'https://www.youtube.com/results?search_query='+link
source_code = requests.get(url, headers=headers, timeout=15)
plain_text = source_code.text
soup = BeautifulSoup(plain_text, "html.parser")
songs = soup.findAll('div', {'class': 'yt-lockup-tile'}) # yt-lockup-video
song = songs[0].contents[0].contents[0].contents[0]
hit = song['href']
print("playing "+say)
webbrowser.open('https://www.youtube.com'+hit)
except Exception as e:
print(e)
print('Sorry, No internet connection!')
# Google Search
elif put.startswith('search '):
try:
link = '+'.join(link[1:])
say = link.replace('+', ' ')
# print(link)
print("searching on google for "+say)
webbrowser.open('https://www.google.co.in/search?q='+link)
except Exception as e:
print(e)
print('Sorry, No internet connection!')
# News
elif put.startswith('science '):
try:
jsonObj = urlopen('''https://newsapi.org/v1/articles?source=new-scientist&sortBy=top&apiKey=your_API_here''')
data = json.load(jsonObj)
i = 1
print('''Here are some top science
news from new scientist''')
print(''' ================NEW SCIENTIST=============
'''+'\n')
for item in data['articles']:
print(str(i)+'. '+item['title']+'\n')
print(item['description']+'\n')
i += 1
except Exception as e:
print(e)
print('Sorry, No internet connection')
elif put.startswith('headlines '):
try:
jsonObj = urlopen('''https://newsapi.org/v1/articles?source=the-times-of-india&sortBy=top&apiKey=your_API_here''')
data = json.load(jsonObj)
i = 1
print('here are some top news from the times of india')
print(''' ===============TIMES OF INDIA============'''
+'\n')
for item in data['articles']:
print(str(i)+'. '+item['title']+'\n')
print(item['description']+'\n')
i += 1
except Exception as e:
print(e)
print(str(e))
# # Other Cases
else:
print("###### Unknown command :", put)
# try:
# # wolframalpha
# client = wolframalpha.Client(app_id)
# res = client.query(put)
# ans = next(res.results).text
# print(ans)
# print(ans)
# except Exception as e:
# print(e)
# # wikipedia
# put = put.split()
# put = ' '.join(put[2:])
# #print(put)
# print(wikipedia.summary(put))
# print('Searched wikipedia for '+put)
# Trigger GUI
if __name__ == "__main__":
frame = MyFrame()