中文社区

http://www.52pi.net/
http://shumeipai.nxez.com/what-raspi-used-for

动态更新域名指向ip

https://www.v2ex.com/t/249694

https://vircloud.net/operations/cf-ddns.html#selection-365.82-365.97 这个比较好

  1. import requests
  2. import os.path
  3. import time
  4. import json
  5. class Ddns:
  6. def __init__(self, domain, mail, token):
  7. self.domain = domain
  8. self.mail = mail
  9. self.token = token
  10. self.headers = {
  11. 'X-Auth-Email': self.mail,
  12. 'X-Auth-Key': self.token,
  13. 'Content-Type': 'application/json'
  14. }
  15. self.ipcacheFile = './.ipcache.txt'
  16. self.recordsCacheFile = './.recordscache.txt'
  17. self.currentIp = self.getCurrentIp()
  18. def display(self):
  19. print(self.domain)
  20. def getCurrentIp(self):
  21. return requests.get('http://whatismyip.akamai.com').text
  22. def records(self):
  23. if self.getCache(self.recordsCacheFile):
  24. print('cache records find')
  25. return self.getCache(self.recordsCacheFile)
  26. res = requests.get('https://api.cloudflare.com/client/v4/zones/' + self.zoneIdentifier() +'/dns_records',
  27. params={'type':'A'},
  28. headers=self.headers)
  29. jres = res.json()
  30. if jres['success'] == True:
  31. self.setCache(self.recordsCacheFile,content=jres['result'])
  32. return jres['result']
  33. else:
  34. return []
  35. def ipHasChange(self):
  36. if not os.path.isfile(self.ipcacheFile):
  37. file = open(self.ipcacheFile, mode='w+')
  38. file.write(self.currentIp)
  39. file.close()
  40. return True
  41. file = open(self.ipcacheFile, mode='r+')
  42. originIp = file.read()
  43. ret = False
  44. if originIp != self.currentIp:
  45. file.write(self.currentIp)
  46. ret = True
  47. file.close()
  48. return ret
  49. def updateRecords(self):
  50. if not self.ipHasChange():
  51. print('ip not change')
  52. return
  53. for record in self.records():
  54. print(self.updateIpv4(record))
  55. def zoneIdentifier(self):
  56. res = requests.get('https://api.cloudflare.com/client/v4/zones', params={
  57. 'name': self.domain
  58. }, headers= self.headers)
  59. jres = res.json()
  60. return jres['result'][0]['id']
  61. def updateIpv4(self, item):
  62. res = requests.put('https://api.cloudflare.com/client/v4/zones/' + item['zone_id'] + '/dns_records/' + item['id'],
  63. headers=self.headers,
  64. json={
  65. # 'id': item['zone_id'],
  66. 'type': item['type'],
  67. 'name': item['name'],
  68. 'content': self.currentIp,
  69. 'ttl': 600,
  70. 'proxied': False
  71. })
  72. jres = res.json()
  73. if not isinstance(jres, dict):
  74. return False
  75. if not jres.has_key('success'):
  76. return False
  77. return jres['success']
  78. def setCache(self, filepath, content, ttl = 3600):
  79. file = open(filepath, mode='w')
  80. data = {
  81. 'content': content,
  82. 'timestamp': time.time()
  83. }
  84. file.write(json.dumps(data))
  85. file.close()
  86. def getCache(self, filepath, ttl = 3600):
  87. if not os.path.isfile(filepath):
  88. return False
  89. file = open(filepath, mode='r')
  90. if not file:
  91. file.close()
  92. return False
  93. cache = json.loads(file.read())
  94. if not cache.has_key('timestamp'):
  95. file.close()
  96. return False
  97. if time.time() - cache['timestamp'] >= ttl:
  98. file.close()
  99. return False
  100. file.close()
  101. return cache['content']
  102. dns = Ddns("winlans.top", mail="winlans.li@gmail.com", token="f8e757f3ad89707dd968c2c54e77efdc33ca6")
  103. 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())

自动链接wifi

http://www.52pi.net/archives/58