Skip to content

Commit 64c4c91

Browse files
committed
automatic_weather
1 parent 752bac6 commit 64c4c91

11 files changed

Lines changed: 272 additions & 4 deletions
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
__author__ = 'luyi'
2+
import os
3+
import urllib.request
4+
import urllib.parse
5+
import json
6+
class weather(object):
7+
# 获取城市代码的uri
8+
code_uri = "http://apistore.baidu.com/microservice/cityinfo?cityname="
9+
# 获取天气信息的uri
10+
weather_uri = "http://apistore.baidu.com/microservice/weather?cityid="
11+
# 主处理逻辑
12+
def mainHandle(self):
13+
#city_name = input("输入你要查询的天气:")
14+
fileHandle = open ( 'test.txt', 'r' )
15+
city_name = fileHandle.read()
16+
fileHandle.close()
17+
uri = self.code_uri + urllib.parse.quote(city_name)
18+
#获取该城市天气情况的网址
19+
print("查询中请等待")
20+
ret = json.loads(urllib.request.urlopen(uri).read().decode("utf8"))
21+
if ret['errNum'] != 0:
22+
print(ret['retMsg'])
23+
return False
24+
#查询失败
25+
else:
26+
#查询成功使用json解析
27+
#需要更详细信息,请看百度api的说明文档。使用方法类似。
28+
weather_uri = self.weather_uri + ret['retData']['cityCode']
29+
data = json.loads(urllib.request.urlopen(weather_uri).read().decode("utf8"))
30+
if data['errNum'] == 0:
31+
ret_data = data['retData']
32+
output = "城市名:" + city_name + "\r\n"
33+
output += "更新时间:" + ret_data["date"] + " " + ret_data["time"] + "\r\n"
34+
output += "天气:" + ret_data["weather"] + " [" + ret_data["WD"] + ret_data["WS"] + "]\r\n"
35+
output += "当前温度:" + ret_data["temp"] + " (" + ret_data["h_tmp"] + " ---> " + ret_data["l_tmp"] + ")\r\n"
36+
37+
print(output)
38+
39+
fileHandle = open ( 'weather.txt', 'w' )
40+
fileHandle.write(output)
41+
fileHandle.close()
42+
43+
return True
44+
else:
45+
print(data['errMsg'])
46+
return False
47+
if __name__ == "__main__":
48+
weather = weather()
49+
weather.mainHandle()
50+
a = input("按任意键退出")

automatic_weather/get_ip.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
# -*- coding: UTF-8 -*-
44

5-
6-
75
import urllib2, httplib
86
from BeautifulSoup import BeautifulSoup
97

@@ -45,8 +43,24 @@ def getIpAddr():
4543
addrEn[n] = addrEn[n].strip()
4644
print addrEn[n], # 逗号是为了print后不换行
4745

48-
print
49-
#print [ip, addr, addrEn]
46+
#进一步化 浙江省杭州市---》杭州
47+
name = addr
48+
if name.find(u"省") !=-1:# 包含'省'
49+
#print u'有省'
50+
name=name.split(u'省')[1]
51+
if name.find(u"市") != -1:#包含‘市’
52+
#print u'有市'
53+
me = addr
54+
if name.find(u"省") !=-1:# 包含'省'
55+
#print u'有省'
56+
name=name.split(u'省')[1]
57+
if name.find(u"市") != -1:#包含‘市’
58+
#print u'有市'
59+
name=name.split(u'市')[0]
60+
print (name)
61+
fileHandle = open ( 'test.txt', 'w' )
62+
fileHandle.write ( name )
63+
fileHandle.close()
5064

5165

5266
if __name__ == '__main__':

automatic_weather/get_yuyin_hecheng.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# -*- coding: utf-8 -*-
2+
3+
import os
4+
import urllib.request
5+
import sys
6+
import json
7+
8+
'''
9+
def get_token():
10+
apiKey = "Ll0c53MSac6GBOtpg22ZSGAU"
11+
secretKey = "44c8af396038a24e34936227d4a19dc2"
12+
13+
auth_url = "https://openapi.baidu.com/oauth/2.0/token?grant_type=client_credentials&client_id=" + apiKey + "&client_secret=" + secretKey;
14+
15+
res = urllib.request.urlopen(auth_url)
16+
print(res.read())
17+
json_data = res.read()
18+
19+
return json.loads(json_data)['access_token']
20+
'''
21+
if __name__ == "__main__":
22+
#token = get_token()
23+
24+
fileHandle = open ( 'weather.txt', 'r' )
25+
weather = fileHandle.read()
26+
fileHandle.close()
27+
28+
29+
#url = "http://tsn.baidu.com/text2audio?tex="+sys.argv[1]+"&lan=zh&per=1&pit=1&spd=7&cuid=7519663&ctp=1&tok="+token
30+
url = "http://tsn.baidu.com/text2audio?tex="+weather+"&lan=zh&per=1&pit=1&spd=7&cuid=7519663&ctp=1&tok=24.91b892cbba2c73d07f9fba69182b7960.2592000.1456136364.282335-7519663"
31+
32+
33+
os.system('mpg123 "%s"'%(url))
34+
#这个在linux可以运行的。也可以自行从我git下载mpg123.然后在window命令行运行。
35+
a = input()
36+
#print(token)
37+
38+
#测试数据
39+
#24.91b892cbba2c73d07f9fba69182b7960.2592000.1456136364.282335-7519663
40+
'''
41+
http://tsn.baidu.com/text2audio?tex=城市名:杭州
42+
更新时间:16-01-23 18:00
43+
天气:多云 [无持续风向6-7级(34~43km/h)]
44+
当前温度:-8 (0 ---> -8)&lan=zh&per=0&pit=5&spd=7&cuid=7519663&ctp=1&tok=24.91b892cbba2c73d07f9fba69182b7960.2592000.1456136364.282335-7519663&qq-pf-to=pcqq.c2c
45+
'''
2.82 KB
Loading
65.1 KB
Loading
7.91 KB
Loading
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
urllibPython 2 namePython 3 name
2+
urllib.urlopen() Deprecated. See urllib.request.urlopen() which mirrorsurllib2.urlopen()
3+
urllib.urlretrieve() urllib.request.urlretrieve()
4+
urllib.urlcleanup() urllib.request.urlcleanup()
5+
urllib.quote() urllib.parse.quote()
6+
urllib.quote_plus() urllib.parse.quote_plus()
7+
urllib.unquote() urllib.parse.unquote()
8+
urllib.unquote_plus() urllib.parse.unquote_plus()
9+
urllib.urlencode() urllib.parse.urlencode()
10+
urllib.pathname2url() urllib.request.pathname2url()
11+
urllib.url2pathname() urllib.request.url2pathname()
12+
urllib.getproxies() urllib.request.getproxies()
13+
urllib.URLopener urllib.request.URLopener
14+
urllib.FancyURLopener urllib.request.FancyURLopener
15+
urllib.ContentTooShortError urllib.error.ContentTooShortError
16+
urllib2.urlopen() urllib.request.urlopen()
17+
urllib2.install_opener() urllib.request.install_opener()
18+
urllib2.build_opener() urllib.request.build_opener()
19+
urllib2.URLError urllib.error.URLError
20+
urllib2.HTTPError urllib.error.HTTPError
21+
urllib2.Request urllib.request.Request
22+
urllib2.OpenerDirector urllib.request.OpenerDirector
23+
urllib2.BaseHandler urllib.request.BaseHandler
24+
urllib2.HTTPDefaultErrorHandler urllib.request.HTTPDefaultErrorHandler
25+
urllib2.HTTPRedirectHandler urllib.request.HTTPRedirectHandler
26+
urllib2.HTTPCookieProcessor urllib.request.HTTPCookieProcessor
27+
urllib2.ProxyHandler urllib.request.ProxyHandler
28+
urllib2.HTTPPasswordMgr urllib.request.HTTPPasswordMgr
29+
urllib2.HTTPPasswordMgrWithDefaultRealm urllib.request.HTTPPasswordMgrWithDefaultRealm
30+
urllib2.AbstractBasicAuthHandler urllib.request.AbstractBasicAuthHandler
31+
urllib2.HTTPBasicAuthHandler urllib.request.HTTPBasicAuthHandler
32+
urllib2.ProxyBasicAuthHandler urllib.request.ProxyBasicAuthHandler
33+
urllib2.AbstractDigestAuthHandler urllib.request.AbstractDigestAuthHandler
34+
urllib2.HTTPDigestAuthHandler urllib.request.HTTPDigestAuthHandler
35+
urllib2.ProxyDigestAuthHandler urllib.request.ProxyDigestAuthHandler
36+
urllib2.HTTPHandler urllib.request.HTTPHandler
37+
urllib2.HTTPSHandler urllib.request.HTTPSHandler
38+
urllib2.FileHandler urllib.request.FileHandler
39+
urllib2.FTPHandler urllib.request.FTPHandler
40+
urllib2.CacheFTPHandler urllib.request.CacheFTPHandler
41+
urllib2.UnknownHandler urllib.request.UnknownHandler
42+
token
43+
44+
24.91b892cbba2c73d07f9fba69182b7960.2592000.1456136364.282335-7519663

automatic_weather/weather.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
������:����
2+
����ʱ��:16-01-23 18:00
3+
����:���� [�޳�������6-7��(34~43km/h)]
4+
��ǰ�¶�:-8 (0 ---> -8)
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# -*- coding: utf-8 -*-
2+
3+
import os
4+
import urllib.request
5+
import sys
6+
import json
7+
'''
8+
def get_token():
9+
apiKey = "Ll0c53MSac6GBOtpg22ZSGAU"
10+
secretKey = "44c8af396038a24e34936227d4a19dc2"
11+
12+
auth_url = "https://openapi.baidu.com/oauth/2.0/token?grant_type=client_credentials&client_id=" + apiKey + "&client_secret=" + secretKey;
13+
14+
res = urllib.request.urlopen(auth_url)
15+
print(res.read())
16+
json_data = res.read()
17+
18+
return json.loads(json_data)['access_token']
19+
'''
20+
if __name__ == "__main__":
21+
#token = get_token()
22+
#url = "http://tsn.baidu.com/text2audio?tex="+sys.argv[1]+"&lan=zh&per=1&pit=1&spd=7&cuid=7519663&ctp=1&tok="+token
23+
url = "http://tsn.baidu.com/text2audio?tex="+"陆熠十四"+"&lan=zh&per=1&pit=1&spd=7&cuid=7519663&ctp=1&tok=24.91b892cbba2c73d07f9fba69182b7960.2592000.1456136364.282335-7519663"
24+
25+
26+
os.system('mpg123 "%s"'%(url))
27+
a = input()
28+
#print(token)
29+
30+
31+
#24.91b892cbba2c73d07f9fba69182b7960.2592000.1456136364.282335-7519663
32+
33+
http://tsn.baidu.com/text2audio?tex=%E5%8F%AF%E4%BB%A5%E9%80%9A%E8%BF%87%E8%B0%83%E7%94%A8%E7%99%BE%E5%BA%A6%E8%AF%AD%E9%9F%B3%E5%90%88%E6%88%90%E6%9D%A5%E6%9C%97%E8%AF%BB%E5%A4%A9%E6%B0%94%E6%83%85%E5%86%B5%E4%BA%86&lan=zh&per=0&pit=5&spd=7&cuid=7519663&ctp=1&tok=24.91b892cbba2c73d07f9fba69182b7960.2592000.1456136364.282335-7519663&qq-pf-to=pcqq.c2c
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
urllibPython 2 namePython 3 name
2+
urllib.urlopen() Deprecated. See urllib.request.urlopen() which mirrorsurllib2.urlopen()
3+
urllib.urlretrieve() urllib.request.urlretrieve()
4+
urllib.urlcleanup() urllib.request.urlcleanup()
5+
urllib.quote() urllib.parse.quote()
6+
urllib.quote_plus() urllib.parse.quote_plus()
7+
urllib.unquote() urllib.parse.unquote()
8+
urllib.unquote_plus() urllib.parse.unquote_plus()
9+
urllib.urlencode() urllib.parse.urlencode()
10+
urllib.pathname2url() urllib.request.pathname2url()
11+
urllib.url2pathname() urllib.request.url2pathname()
12+
urllib.getproxies() urllib.request.getproxies()
13+
urllib.URLopener urllib.request.URLopener
14+
urllib.FancyURLopener urllib.request.FancyURLopener
15+
urllib.ContentTooShortError urllib.error.ContentTooShortError
16+
urllib2.urlopen() urllib.request.urlopen()
17+
urllib2.install_opener() urllib.request.install_opener()
18+
urllib2.build_opener() urllib.request.build_opener()
19+
urllib2.URLError urllib.error.URLError
20+
urllib2.HTTPError urllib.error.HTTPError
21+
urllib2.Request urllib.request.Request
22+
urllib2.OpenerDirector urllib.request.OpenerDirector
23+
urllib2.BaseHandler urllib.request.BaseHandler
24+
urllib2.HTTPDefaultErrorHandler urllib.request.HTTPDefaultErrorHandler
25+
urllib2.HTTPRedirectHandler urllib.request.HTTPRedirectHandler
26+
urllib2.HTTPCookieProcessor urllib.request.HTTPCookieProcessor
27+
urllib2.ProxyHandler urllib.request.ProxyHandler
28+
urllib2.HTTPPasswordMgr urllib.request.HTTPPasswordMgr
29+
urllib2.HTTPPasswordMgrWithDefaultRealm urllib.request.HTTPPasswordMgrWithDefaultRealm
30+
urllib2.AbstractBasicAuthHandler urllib.request.AbstractBasicAuthHandler
31+
urllib2.HTTPBasicAuthHandler urllib.request.HTTPBasicAuthHandler
32+
urllib2.ProxyBasicAuthHandler urllib.request.ProxyBasicAuthHandler
33+
urllib2.AbstractDigestAuthHandler urllib.request.AbstractDigestAuthHandler
34+
urllib2.HTTPDigestAuthHandler urllib.request.HTTPDigestAuthHandler
35+
urllib2.ProxyDigestAuthHandler urllib.request.ProxyDigestAuthHandler
36+
urllib2.HTTPHandler urllib.request.HTTPHandler
37+
urllib2.HTTPSHandler urllib.request.HTTPSHandler
38+
urllib2.FileHandler urllib.request.FileHandler
39+
urllib2.FTPHandler urllib.request.FTPHandler
40+
urllib2.CacheFTPHandler urllib.request.CacheFTPHandler
41+
urllib2.UnknownHandler urllib.request.UnknownHandler
42+
token
43+
44+
24.91b892cbba2c73d07f9fba69182b7960.2592000.1456136364.282335-7519663

0 commit comments

Comments
 (0)