中文社区
http://www.52pi.net/
http://shumeipai.nxez.com/what-raspi-used-for
动态更新域名指向ip
https://vircloud.net/operations/cf-ddns.html#selection-365.82-365.97 这个比较好
import requestsimport os.pathimport timeimport jsonclass Ddns:def __init__(self, domain, mail, token):self.domain = domainself.mail = mailself.token = tokenself.headers = {'X-Auth-Email': self.mail,'X-Auth-Key': self.token,'Content-Type': 'application/json'}self.ipcacheFile = './.ipcache.txt'self.recordsCacheFile = './.recordscache.txt'self.currentIp = self.getCurrentIp()def display(self):print(self.domain)def getCurrentIp(self):return requests.get('http://whatismyip.akamai.com').textdef records(self):if self.getCache(self.recordsCacheFile):print('cache records find')return self.getCache(self.recordsCacheFile)res = requests.get('https://api.cloudflare.com/client/v4/zones/' + self.zoneIdentifier() +'/dns_records',params={'type':'A'},headers=self.headers)jres = res.json()if jres['success'] == True:self.setCache(self.recordsCacheFile,content=jres['result'])return jres['result']else:return []def ipHasChange(self):if not os.path.isfile(self.ipcacheFile):file = open(self.ipcacheFile, mode='w+')file.write(self.currentIp)file.close()return Truefile = open(self.ipcacheFile, mode='r+')originIp = file.read()ret = Falseif originIp != self.currentIp:file.write(self.currentIp)ret = Truefile.close()return retdef updateRecords(self):if not self.ipHasChange():print('ip not change')returnfor record in self.records():print(self.updateIpv4(record))def zoneIdentifier(self):res = requests.get('https://api.cloudflare.com/client/v4/zones', params={'name': self.domain}, headers= self.headers)jres = res.json()return jres['result'][0]['id']def updateIpv4(self, item):res = requests.put('https://api.cloudflare.com/client/v4/zones/' + item['zone_id'] + '/dns_records/' + item['id'],headers=self.headers,json={# 'id': item['zone_id'],'type': item['type'],'name': item['name'],'content': self.currentIp,'ttl': 600,'proxied': False})jres = res.json()if not isinstance(jres, dict):return Falseif not jres.has_key('success'):return Falsereturn jres['success']def setCache(self, filepath, content, ttl = 3600):file = open(filepath, mode='w')data = {'content': content,'timestamp': time.time()}file.write(json.dumps(data))file.close()def getCache(self, filepath, ttl = 3600):if not os.path.isfile(filepath):return Falsefile = open(filepath, mode='r')if not file:file.close()return Falsecache = json.loads(file.read())if not cache.has_key('timestamp'):file.close()return Falseif time.time() - cache['timestamp'] >= ttl:file.close()return Falsefile.close()return cache['content']dns = Ddns("winlans.top", mail="winlans.li@gmail.com", token="f8e757f3ad89707dd968c2c54e77efdc33ca6")dns.updateRecords()
安装samba
https://www.cnblogs.com/xiaowuyi/p/4051238.html
语音播放
pip install baidu-aip // 安装百度语音sdk
#!/usr/bin/python
# -*- coding: UTF-8 -*-
from aip import AipSpeech
APP_ID = '17904707'
API_KEY = 'PROyLy84XY9KINSKiQV4zbmC'
SECRET_KEY = 'tr9ElfNjA86GtviCtZoDv2tzjhA3luV2'
client = AipSpeech(APP_ID, API_KEY, SECRET_KEY)
result = client.synthesis('今天天气不错的', 'zh', 1, {'vol': 4})
# 识别正确返回语音二进制 错误则返回dict 参照下面错误码
if not isinstance(result, dict):
with open('auido.mp3', 'wb') as f:
f.write(result)
天气查询
使用和风天气api
api文档
示例
https://free-api.heweather.net/s6/weather/now?location=beijing&key=8228222ce0c7417fbc8ee62b2d018538
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import requests
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
class Weather(object):
def __init__(self):
self.baseUrl = 'https://free-api.heweather.net/s6/weather/'
self.token = '8228222ce0c7417fbc8ee62b2d018538'
self.location = 'auto_ip'
self.location_text = ''
def lifestyle(self):
response = requests.get(self.baseUrl + 'lifestyle', params={
'location': self.location,
'key': self.token,
})
jres = response.json()
drsg = jres['HeWeather6'][0]['lifestyle'][1] # 穿衣建议
self.location_text = jres['HeWeather6'][0]['basic']['location']
return '天气' + drsg['brf']+drsg['txt'];
def today(self):
response = requests.get(self.baseUrl + 'forecast', params={
'location': self.location,
'key': self.token,
})
jres = response.json()
today = jres['HeWeather6'][0]["daily_forecast"][0]
self.location_text = jres['HeWeather6'][0]['basic']['location']
return '最低温度' + today['tmp_min'] + '摄氏度,最高温度' \
+ today['tmp_max'] + '摄氏度,' + today['wind_dir'] + today['wind_sc'] + '级。'
def output(self):
today = self.today()
drsg = self.lifestyle()
return self.location_text + '今日天气:' + today + drsg
w = Weather();
# print(w.lifestyle())
print(w.output())
