This repository has been archived by the owner on Oct 30, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathWolframAlpha.py
87 lines (71 loc) · 2.77 KB
/
WolframAlpha.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
''' This script will send a mail to the client when Google reaches their revenue to $300b. '''
import urllib2
import urllib
import httplib
from xml.etree import ElementTree as etree
import smtplib
class wolfram(object):
def __init__(self, appid):
self.appid = appid
self.base_url = 'http://api.wolframalpha.com/v2/query?'
self.headers = {'User-Agent': None}
def _get_xml(self, ip):
url_params = {'input': ip, 'appid': self.appid}
data = urllib.urlencode(url_params)
req = urllib2.Request(self.base_url, data, self.headers)
xml = urllib2.urlopen(req).read()
return xml
def _xmlparser(self, xml):
data_dics = {}
tree = etree.fromstring(xml)
# retrieving every tag with label 'plaintext'
for e in tree.findall('pod'):
for item in [ef for ef in list(e) if ef.tag == 'subpod']:
for it in [i for i in list(item) if i.tag == 'plaintext']:
if it.tag == 'plaintext':
data_dics[e.get('title')] = it.text
return data_dics
def search(self, ip):
# This function will query about Google's revenue using WolframAlpha API
xml = self._get_xml(ip)
result_dics = self._xmlparser(xml)
titles = result_dics.keys()
Res = result_dics['Result']
Res_Split = Res.split('.')
arg_1 = Res_Split[0]
revenue = arg_1[1:]
try:
if int(revenue) >= 300:
self.send_mails()
else:
print "Google has not reached it's revenue to $300b yet."
except:
print "Erorr in output."
def send_mails(self):
gmail_user = "[email protected]"
gmail_pwd = "test@123"
FROM = '[email protected]'
TO = ['[email protected]'] #must be a list
SUBJECT = "Google has reached it's revenue to $300b"
TEXT = "Hi,\n\nThis is to inform you that Google has reached it's revenue to $300b.\n \nRegards,\nAutomatic Mail Sender."
# Prepare actual message
message = """\From: %s\nTo: %s\nSubject: %s\n\n%s
""" % (FROM, ", ".join(TO), SUBJECT, TEXT)
try:
#server = smtplib.SMTP(SERVER)
server = smtplib.SMTP("smtp.test.com", 25)
server.ehlo()
server.starttls()
server.login(user_name, password)
server.sendmail(FROM, TO, message)
#server.quit()
server.close()
print 'Google has reached their revenue to $300b.\nSuccessfully sent the mail'
except:
print "Failed to send mail"
if __name__ == "__main__":
schedule = "0 0 * * *"
appid = 'T8WRWQ-7X2GVQ6Y3V'
query = 'Google Revenue'
w = wolfram(appid)
w.search(query)