人生苦短,我用Python~ html
一直想本身折騰些極客的東西出來,供本身使用或者是供他人使用。一則可能對本身的生活是一種幫助,二則是對本身技能的提升,三則顯得高大上,一直努力,一直提升,一直Happy!~python
直到,在京東白條了一個樹莓派3,我知道,有一段興奮的苦旅要開始了...linux
本文擬使用Python開發一個智能時鐘,而且將智能時鐘部署在 樹莓派3 微機上,7*24小時開機,以達到實時監控時鐘並在適當的時侯做出響應的目的。實際上是天天早上邪惡的提示語,加一段提示音樂。天天中午吵醒午睡的鬧鐘,房東在沒人的時候進屋能嚇他一跳...zzzhhhweb
這篇文章將要介紹的主要內容以下:正則表達式
一、早上鬧鐘,天氣預報,音樂提醒json
二、整點報時,半點報時ubuntu
一、python對天氣網站信息的web爬蟲爬取vim
二、經過分析爬取的數據,並拼接須要語音播報的文本windows
三、調用百度的語音合成api進行語音文本轉化成mp3api
四、使用python腳本將mp3下載到本地
五、設置計劃任務,每分鐘檢測一下當前時間是否知足條件
六、早上鬧鐘+天氣預報+音樂,整點報時,半點響音
開發環境:Windows 7 ×64 英文版
JetBrains PyCharm 2017.1.3 x64
部署環境:Raspberry Pi 3 Ubuntu OS
一、Python語言的熟練掌握,Python版本2.7
二、利用Python語言,實現web爬蟲,網絡爬蟲的理解
三、正則表達式的簡單使用
四、BeautifulSoup html解析庫的使用
五、linux經常使用命令的熟練掌握
首先附上程序結構:
咱們此次寫的py文件是 main_SmartClock.py ,而後還有個配置文件 qx_shs.ini、配置文件全局讀取 ConfigField.py ,GetWeatherText.py 爬取墨跡天氣的py腳本,TextToAudio.py 將文本轉化成語音的腳本。咱們可能主要用到這幾個py文件。
一、首先是配置ini 配置文件
1 [System] 2 OWNER = 東爺 ;語音播報的名字 3 4 [ClockTime] 5 CLOCK_MORNING_AWAKE = 7:16 6 CLOCK_MORNING_GO = 8:30 7 CLOCK_NOON = 13:30 8 CLOCK_AFTERNOON = 19:00 9 CLOCK_EVENING = 23:00 10 11 [CameraRecognition] 12 IMAGE_SIMILARITY = 0.85 ;視屏監控圖片匹配類似度 13 SAMPLE_IMAGES_NAME = 東爺 ;圖片樣本名稱 14 15 [Text] 16 ATTACK_ALERT_TEXT = Some People Attack 17 ALERT_TXT_MORNING = 早上好,美好的一天開始啦 18 ALERT_TXT_NOON = 中午好,小憩一下,睡個午覺,美美噠 19 ALERT_TXT_AFTERNOON = 下午好,夕陽無限好,只是近黃昏啊 20 ALERT_TXT_NIGHT = 晚上好,夜深了,早點休息吧,記得睡前喝牛奶哦 21 ALERT_TXT_OFF_WORK = 您下班啦,工做一天累了吧,休息一下吧 22 ALERT_TXT_HELLO = '您好' 23 24 25 [Path] 26 MP3_ALERT = soundFiles/alert.mp3 27 MP3_START = soundFiles/start.mp3 28 MP3_NORMAL = soundFiles/chengdu.mp3 29 MP3_WEATHER = soundFiles/weatherGreeting.mp3 30 MP3_MORNING_AWAKE = soundFiles/piaoyangguohailaikanni.mp3 31 MP3_MORNING_GO = soundFiles/chengdu.mp3 32 MP3_NOON = soundFiles/shiguangzhenghao.mp3 33 MP3_AFTERNOON = soundFiles/wenwendexinfu.mp3 34 MP3_EVENING = soundFiles/guqin.mp3 35 MP3_HALF_HOUR = soundFiles/short2.mp3 36 MP3_HOUR = soundFiles/short3.mp3 37 MP3_BACKGROUND = soundFiles/background2.mp3 38 39 CLASSIFIER_PATH = Resources/haarcascade_frontalface_default.xml 40 SAMPLE_IMAGES_SAVE_PATH = sampleImages/ 41 CAMERA_MONITORING_FILES_PATH = cameraMonitoringFiles/
二、而後是對配置文件的讀取,並設置到全局變量,由於我好幾個主要py文件都要調用這幾個全局變量,因此我就直接設置在全局了,避免重複獲取ini文件
1 # coding=utf-8 2 import ConfigParser 3 4 # 樹莓派的ubuntu系統裏面若是要使用計劃任務,則必須寫成絕對路徑,意味着這裏須要加前綴 5 # RASPBERRY_PI_PATH_ROOT = '/root/C/' 6 # RASPBERRY_PI_PATH_PROJECT = 'QX_SHS/' 7 8 # Windows調試不須要加絕對路徑 9 RASPBERRY_PI_PATH_ROOT = '' 10 RASPBERRY_PI_PATH_PROJECT = '' 11 12 RASPBERRY_PI_PATH_PLUS = RASPBERRY_PI_PATH_ROOT + RASPBERRY_PI_PATH_PROJECT 13 14 HEADERS = { 15 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit' 16 '/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safar' 17 'i/537.36', 18 } 19 20 # get configuration 21 config = ConfigParser.ConfigParser() 22 config.readfp(open(RASPBERRY_PI_PATH_PLUS + 'qx_shs.ini')) 23 24 # 獲取System 25 OWNER = config.get('System', 'OWNER') 26 27 # 獲取Text 28 ATTACK_ALERT_TEXT = config.get('Text', 'ATTACK_ALERT_TEXT') 29 ALERT_TXT_MORNING = config.get('Text', 'ALERT_TXT_MORNING') 30 ALERT_TXT_NOON = config.get('Text', 'ALERT_TXT_NOON') 31 ALERT_TXT_AFTERNOON = config.get('Text', 'ALERT_TXT_AFTERNOON') 32 ALERT_TXT_NIGHT = config.get('Text', 'ALERT_TXT_NIGHT') 33 ALERT_TXT_OFF_WORK = config.get('Text', 'ALERT_TXT_OFF_WORK') 34 ALERT_TXT_HELLO = config.get('Text', 'ALERT_TXT_HELLO') 35 36 # 獲取鬧鐘時間參數 37 CLOCK_MORNING_AWAKE = config.get('ClockTime', 'CLOCK_MORNING_AWAKE') 38 CLOCK_MORNING_GO = config.get('ClockTime', 'CLOCK_MORNING_GO') 39 CLOCK_NOON = config.get('ClockTime', 'CLOCK_NOON') 40 CLOCK_AFTERNOON = config.get('ClockTime', 'CLOCK_AFTERNOON') 41 CLOCK_EVENING = config.get('ClockTime', 'CLOCK_EVENING') 42 43 # 獲取CameraRecognition參數 44 IMAGE_SIMILARITY = config.get('CameraRecognition', 'IMAGE_SIMILARITY') 45 SAMPLE_IMAGES_NAME = config.get('CameraRecognition', 'SAMPLE_IMAGES_NAME') 46 47 # 獲取路徑 48 MP3_ALERT = RASPBERRY_PI_PATH_PLUS + config.get('Path', 'MP3_ALERT') 49 MP3_START = RASPBERRY_PI_PATH_PLUS + config.get('Path', 'MP3_START') 50 MP3_NORMAL = RASPBERRY_PI_PATH_PLUS + config.get('Path', 'MP3_NORMAL') 51 MP3_WEATHER = RASPBERRY_PI_PATH_PLUS + config.get('Path', 'MP3_WEATHER') 52 MP3_MORNING_AWAKE = RASPBERRY_PI_PATH_PLUS + config.get('Path', 'MP3_MORNING_AWAKE') 53 MP3_MORNING_GO = RASPBERRY_PI_PATH_PLUS + config.get('Path', 'MP3_MORNING_GO') 54 MP3_NOON = RASPBERRY_PI_PATH_PLUS + config.get('Path', 'MP3_NOON') 55 MP3_AFTERNOON = RASPBERRY_PI_PATH_PLUS + config.get('Path', 'MP3_AFTERNOON') 56 MP3_EVENING = RASPBERRY_PI_PATH_PLUS + config.get('Path', 'MP3_EVENING') 57 MP3_HALF_HOUR = RASPBERRY_PI_PATH_PLUS + config.get('Path', 'MP3_HALF_HOUR') 58 MP3_HOUR = RASPBERRY_PI_PATH_PLUS + config.get('Path', 'MP3_HOUR') 59 MP3_BACKGROUND = RASPBERRY_PI_PATH_PLUS + config.get('Path', 'MP3_BACKGROUND') 60 61 CLASSIFIER_PATH = RASPBERRY_PI_PATH_PLUS + config.get('Path', 'CLASSIFIER_PATH') 62 SAMPLE_IMAGES_SAVE_PATH = RASPBERRY_PI_PATH_PLUS + config.get('Path', 'SAMPLE_IMAGES_SAVE_PATH') 63 CAMERA_MONITORING_FILES_PATH = RASPBERRY_PI_PATH_PLUS + config.get('Path', 'CAMERA_MONITORING_FILES_PATH')
三、使用requests庫進行對墨跡天氣網站信息的爬取http://tianqi.moji.com/,網絡爬蟲的代碼我就不進行介紹了,代碼裏面也有詳細的註釋,要注意對其餘文件的調用,包導入。
1 # coding=utf-8 2 3 # RASPBERRY_PI_PATH_ROOT 執行Ubuntu計劃任務必須聲明絕對路徑 4 RASPBERRY_PI_PATH_ROOT = '/root/C/' 5 6 import sys 7 8 sys.path.append(RASPBERRY_PI_PATH_ROOT) 9 import re 10 import requests 11 from bs4 import BeautifulSoup 12 from datetime import datetime 13 from Helper_DG.DateTimeHelper_DG import * 14 from QX_SHS.ConfigField import * 15 16 reload(sys) 17 sys.setdefaultencoding('utf8') 18 19 20 def numtozh(num): 21 num_dict = {1: '一', 2: '二', 3: '三', 4: '四', 5: '五', 6: '六', 7: '七', 22 8: '八', 9: '九', 0: '零'} 23 num = int(num) 24 if 100 <= num < 1000: 25 b_num = num // 100 26 s_num = (num - b_num * 100) // 10 27 g_num = (num - b_num * 100) % 10 28 if g_num == 0 and s_num == 0: 29 num = '%s百' % (num_dict[b_num]) 30 elif s_num == 0: 31 num = '%s百%s%s' % (num_dict[b_num], num_dict.get(s_num, ''), num_dict.get(g_num, '')) 32 elif g_num == 0: 33 num = '%s百%s十' % (num_dict[b_num], num_dict.get(s_num, '')) 34 else: 35 num = '%s百%s十%s' % (num_dict[b_num], num_dict.get(s_num, ''), num_dict.get(g_num, '')) 36 elif 10 <= num < 100: 37 s_num = num // 10 38 g_num = (num - s_num * 10) % 10 39 if g_num == 0: 40 g_num = '' 41 num = '%s十%s' % (num_dict[s_num], num_dict.get(g_num, '')) 42 elif 0 <= num < 10: 43 g_num = num 44 num = '%s' % (num_dict[g_num]) 45 elif -10 < num < 0: 46 g_num = -num 47 num = '零下%s' % (num_dict[g_num]) 48 elif -100 < num <= -10: 49 num = -num 50 s_num = num // 10 51 g_num = (num - s_num * 10) % 10 52 if g_num == 0: 53 g_num = '' 54 num = '零下%s十%s' % (num_dict[s_num], num_dict.get(g_num, '')) 55 return num 56 57 58 # timeRegion 時間區間 1=早上,2=中午,3=傍晚,4=夜晚,其餘=如今 59 def get_weather(name, timeRegion): 60 # 下載墨跡天氣主頁源碼 61 res = requests.get('http://tianqi.moji.com/', headers=HEADERS) 62 # 用BeautifulSoup獲取所需信息 63 soup = BeautifulSoup(res.text, "html.parser") 64 address = soup.find('div', attrs={'class': 'search_default'}).em.getText() 65 temp = soup.find('div', attrs={'class': 'wea_weather clearfix'}).em.getText() 66 temp = numtozh(int(temp)) 67 weather = soup.find('div', attrs={'class': 'wea_weather clearfix'}).b.getText() 68 sd = soup.find('div', attrs={'class': 'wea_about clearfix'}).span.getText() 69 sd_num = re.search(r'\d+', sd).group() 70 sd_num_zh = numtozh(int(sd_num)) 71 sd = sd.replace(sd_num, sd_num_zh) 72 wind = soup.find('div', attrs={'class': 'wea_about clearfix'}).em.getText() 73 aqi = soup.find('div', attrs={'class': 'wea_alert clearfix'}).em.getText() 74 aqi_num = re.search(r'\d+', aqi).group() 75 aqi_num_zh = numtozh(int(aqi_num)) 76 aqi = aqi.replace(aqi_num, aqi_num_zh).replace(' ', ',空氣質量') 77 info = soup.find('div', attrs={'class': 'wea_tips clearfix'}).em.getText() 78 sd = sd.replace(' ', '百分之').replace('%', '') 79 aqi = 'aqi' + aqi 80 info = info.replace(',', ',') 81 # 獲取今天的日期 82 timeNow = datetime.datetime.now() 83 84 # 將獲取的信息拼接成一句話 85 weatherText = '' 86 if timeRegion == 1: 87 weatherText = '早上好!%s ,今天是%s年,%s月,%s日,星期%s,如今的時間是%s點,%s分。您所在的地區是%s,天氣%s,溫度%s攝氏度,%s,%s,%s,%s 開始美好的一天吧,麼麼噠。' % \ 88 ( 89 name, timeNow.year, timeNow.month, timeNow.day, get_week_day_by_date(timeNow), timeNow.hour, 90 timeNow.minute, address, 91 weather, temp, 92 sd, 93 wind, 94 aqi, info) 95 elif timeRegion == 2: 96 weatherText = '中午好!%s ,今天是%s年,%s月,%s日,星期%s,如今的時間是%s點,%s分。您所在的地區是%s,天氣%s,溫度%s攝氏度,%s,%s,%s,%s 中午必定要記得休息哦,麼麼噠。' % \ 97 ( 98 name, timeNow.year, timeNow.month, timeNow.day, get_week_day_by_date(timeNow), timeNow.hour, 99 timeNow.minute, address, 100 weather, temp, 101 sd, 102 wind, 103 aqi, info) 104 elif timeRegion == 3: 105 weatherText = '下午好!%s ,今天是%s年,%s月,%s日,星期%s,如今的時間是%s點,%s分。您所在的地區是%s,天氣%s,溫度%s攝氏度,%s,%s,%s,%s 忙了一天,累了吧,麼麼噠。' % \ 106 ( 107 name, timeNow.year, timeNow.month, timeNow.day, get_week_day_by_date(timeNow), timeNow.hour, 108 timeNow.minute, address, 109 weather, temp, 110 sd, 111 wind, 112 aqi, info) 113 elif timeRegion == 4: 114 weatherText = '晚上好!%s ,今天是%s年,%s月,%s日,星期%s,如今的時間是%s點,%s分。您所在的地區是%s,天氣%s,溫度%s攝氏度,%s,%s,%s,%s 夜已深了,早點休息吧,麼麼噠。' % \ 115 ( 116 name, timeNow.year, timeNow.month, timeNow.day, get_week_day_by_date(timeNow), timeNow.hour, 117 timeNow.minute, address, 118 weather, temp, 119 sd, 120 wind, 121 aqi, info) 122 elif timeRegion == 5: 123 weatherText = '如今是,北京時間 %s年,%s月,%s日,星期%s,%s點,%s分。' % \ 124 ( 125 timeNow.year, timeNow.month, timeNow.day, get_week_day_by_date(timeNow), timeNow.hour, 126 timeNow.minute) 127 else: 128 weatherText = '您好!%s ,今天是%s年,%s月,%s日,星期%s,如今的時間是%s點,%s分。您所在的地區是%s,天氣%s,溫度%s攝氏度,%s,%s,%s,%s 麼麼噠。' % \ 129 ( 130 name, timeNow.year, timeNow.month, timeNow.day, get_week_day_by_date(timeNow), timeNow.hour, 131 timeNow.minute, address, 132 weather, temp, 133 sd, 134 wind, 135 aqi, info) 136 137 return weatherText
四、接下來就是咱們的主程序入口了,首先上代碼,而後再進行介紹
1 # coding=utf-8 2 3 # RASPBERRY_PI_PATH_ROOT 執行Ubuntu計劃任務必須聲明絕對路徑 4 RASPBERRY_PI_PATH_ROOT = '/root/C/' 5 6 import sys 7 8 sys.path.append(RASPBERRY_PI_PATH_ROOT) 9 import os 10 from FunctionCode.GetWeatherText import * 11 from FunctionCode.TextToAudio import * 12 13 # 播放器(totem)和音樂文件(The_Snow.mp3)須要用戶指定,須要指定時間 14 def play_mp3_weather(time_index): 15 # 經過不一樣的時間,獲取不一樣時間的天氣信息 16 text = get_weather(str(OWNER), time_index) 17 # 將文字信息轉換成語音信息 18 text2voice(text, MP3_WEATHER) 19 os.system("mpg321 {0}".format(str(MP3_WEATHER))) 20 21 22 if __name__ == '__main__': 23 24 timeNow = get_current_time() 25 26 print('{0} -> welcome to use qixiao`s program . starting ...'.format(timeNow)) 27 28 # os.system("mpg321 {0}".format(str(MP3_START))) 29 # text = get_weather(str(OWNER), 0) 30 # print(text) 31 32 try: 33 # 獲取特定格式的當前時間 34 timeNowStringFormat = str('{0}:{1}'.format(timeNow.hour, timeNow.minute)) 35 # 打印當前時間 36 # print('time now -> {0}'.format(timeNow)) 37 # 進行時間判斷,並在不一樣時間段進行對應的操做 38 if timeNowStringFormat == CLOCK_MORNING_AWAKE: 39 print('{0} >>> morning ! it`s time to awake ... '.format(str(CLOCK_MORNING_AWAKE))) 40 play_mp3_weather(1) 41 os.system("mpg321 {0}".format(str(MP3_MORNING_AWAKE))) 42 if timeNowStringFormat == CLOCK_MORNING_GO: 43 print('{0} >>> morning ! it`s time to go ... '.format(str(CLOCK_MORNING_GO))) 44 play_mp3_weather(1) 45 os.system("mpg321 {0}".format(str(MP3_MORNING_GO))) 46 elif timeNowStringFormat == CLOCK_NOON: 47 print('{0} >>> good noon have a good mood ... '.format(str(CLOCK_NOON))) 48 play_mp3_weather(2) 49 os.system("mpg321 {0}".format(str(MP3_NOON))) 50 elif timeNowStringFormat == CLOCK_AFTERNOON: 51 print('{0} >>> good afternoon it`s time to happy ... '.format(str(CLOCK_AFTERNOON))) 52 play_mp3_weather(3) 53 os.system("mpg321 {0}".format(str(MP3_AFTERNOON))) 54 elif timeNowStringFormat == CLOCK_EVENING: 55 print('{0} >>> good night it`s time to sleep ... '.format(str(CLOCK_EVENING))) 56 play_mp3_weather(4) 57 os.system("mpg321 {0}".format(str(MP3_EVENING))) 58 # 整點報時功能 早7點之後,晚24點以前 除去13點午休時間 59 elif timeNow.hour > 7 and timeNow.hour < 24 and timeNow.hour != 13 and timeNow.minute == 00: 60 print('# >>> integral point. ... ') 61 os.system("mpg321 {0}".format(str(MP3_HOUR))) 62 play_mp3_weather(5) 63 # 半點報時功能 早7點之後,晚24點以前 除去13點午休時間 64 elif timeNow.hour >= 7 and timeNow.hour < 24 and timeNow.hour != 13 and timeNow.minute == 30: 65 print('# >>> half an hour ... ') 66 os.system("mpg321 {0}".format(str(MP3_HALF_HOUR))) 67 else: 68 pass 69 70 except Exception: 71 print(str(timeNow) + ' >>> ' + Exception)
該主程序經過方法獲取到了當前的時間,並進行時間的匹配,對應的時間執行對應的操做。在主程序定義的方法中,咱們進行對天氣等信息的獲取,並拼接字符串,調用百度聲音合成api進行聲音的獲取及轉化成mp3,保存到本地,最後進行聲音的播放。
五、百度接口的調用
1 # coding=utf-8 2 3 # RASPBERRY_PI_PATH_ROOT 執行Ubuntu計劃任務必須聲明絕對路徑 4 RASPBERRY_PI_PATH_ROOT = '/root/C/' 5 6 import sys 7 8 sys.path.append(RASPBERRY_PI_PATH_ROOT) 9 import requests 10 import json 11 from QX_SHS.ConfigField import * 12 13 # 將文字轉化成mp3 並保存到本地 14 def text2voice(text,mp3_path): 15 urlGetToken = 'https://openapi.baidu.com/oauth/2.0/token?grant_type=client_credentials&client_id=************&client_secret=************' 16 17 token = requests.post(urlGetToken, headers=HEADERS) 18 tokenJson = json.loads(token.content.decode(encoding="utf-8")) 19 access_token = tokenJson['access_token'] 20 21 # spd 語速 pit 音調 per 0 女 1 男 22 url = 'http://tsn.baidu.com/text2audio?tex={0}&lan=zh&cuid={1}&ctp=1&tok={2}&spd=4&pit=8&per=0'.format(text, 'qixiao_text2audio', 23 access_token) 24 # 下載轉換後的mp3格式語音 25 res = requests.get(url, headers=HEADERS) 26 # 將MP3存入本地 27 with open(mp3_path, 'wb') as f: 28 f.write(res.content)
經過調用百度的api接口,咱們就能夠將文字轉化成聲音啦,多種聲音設置能夠選擇。
進行到這一步,咱們就能夠進行部署測試了。
一、使用FTP或任何方式將代碼部署到 Raspberry Pi上
二、因爲樹莓派是ubuntu系統,須要安裝支持mp3播放的播放器,這裏我採用 mpg321
安裝mpg321,終端輸入命令:sudo apt-get install mpg123
等待片刻就能夠安裝好mpg321了,該命令能夠直接跟mp3 文件進行mp3的播放
mpg321 test.mp3
若是打開想要關閉的話,須要執行進程關閉命令:
killall mpg321 就能夠關掉啦
三、將該py腳本添加到計劃任務中去,而且使其每分鐘執行一次,來掃描當前的時間
使用crontab命令,能夠添加計劃任務,終端運行命令
crontab -e 執行編輯任務
crontab -l 查看當前用戶下的所有計劃任務
第一次執行 crontab -e 會讓你選擇默認的編輯器,最後終於發現vi竟然是最好用的。
添加計劃任務的方法也很簡單了,這裏有模板能夠參考:
每一項cron實體的內容都包含六個部分:分鐘、小時、月份中的哪一天、年份中的哪一月、星期中的哪一天,還有定時執行的命令。
# m h dom mon dow command # * * * * * command to execute # ┬ ┬ ┬ ┬ ┬ # │ │ │ │ │ # │ │ │ │ │ # │ │ │ │ └───── 星期中的哪一天(0-7)(從0到6表明星期日到星期六,也可使用名字;7是星期天,等同於0) # │ │ │ └────────── 月份 (1 - 12) # │ │ └─────────────── 月份中的日 (1 - 31) # │ └──────────────────── 小時 (0 - 23) # └───────────────────────── 分鐘 (0 - 59)
例如個人計劃任務是這麼寫的:
*/1 * * * * python2 /root/C/QX_SHS/main_SmartClock.py
*/1的意思是每分鐘執行一次。
咱們設置完後,esc->shift+;->wq 回車
這樣,計劃任務就啓動了。若是不出意外,能達到理想的效果。關於其餘計劃任務的命令,須要自行學習了。
這裏附上個人大PI的寫真~
因爲要擱到下面的桌子上安裝opencv 因此顯示器放在下面了,正常時候是在牀上 很 「美觀」的 放置着的。
攝像頭剛到,欸,繼續去研究下一個課題,人臉識別問候系統+室內監控系統,代碼已完成,正在跳部署的坑~~~(此處附上女朋友摯愛的~/攤手)
一、 部署到樹莓派時候,可能裝到計劃任務,卻沒有反應;不要緊,咱們先將代碼main方法裏的 最上面寫一段模擬的音樂播放,直接播放已經存在的路徑裏的音樂。
或者將個人註釋取消掉便可,我自己也是作過測試的。
而後直接在終端運行咱們計劃任務裏的命令,ok,若是報路徑找不到之類的問題就對了。
計劃任務裏的腳本執行命令,和直接切換到腳本所在的目錄執行的方式是不太同樣的,總之,若是在根目錄執行某一個帶目錄路徑的腳本的話,全部的腳本里的相對路徑都會找不到,那麼咱們只有拼接一個絕對路徑到項目全部的目錄前面了,這裏我在ConfigFeild.py裏面,最上面有兩行註釋進行了該操做。
再次附上ConfigFeild.py的代碼:
1 # coding=utf-8 2 import ConfigParser 3 4 # 樹莓派的ubuntu系統裏面若是要使用計劃任務,則必須寫成絕對路徑,意味着這裏須要加前綴 5 # RASPBERRY_PI_PATH_ROOT = '/root/C/' 6 # RASPBERRY_PI_PATH_PROJECT = 'QX_SHS/' 7 8 # Windows調試不須要加絕對路徑 9 RASPBERRY_PI_PATH_ROOT = '' 10 RASPBERRY_PI_PATH_PROJECT = '' 11 12 RASPBERRY_PI_PATH_PLUS = RASPBERRY_PI_PATH_ROOT + RASPBERRY_PI_PATH_PROJECT 13 14 HEADERS = { 15 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit' 16 '/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safar' 17 'i/537.36', 18 } 19 20 # get configuration 21 config = ConfigParser.ConfigParser() 22 config.readfp(open(RASPBERRY_PI_PATH_PLUS + 'qx_shs.ini')) 23 24 # 獲取System 25 OWNER = config.get('System', 'OWNER') 26 27 # 獲取Text 28 ATTACK_ALERT_TEXT = config.get('Text', 'ATTACK_ALERT_TEXT') 29 ALERT_TXT_MORNING = config.get('Text', 'ALERT_TXT_MORNING') 30 ALERT_TXT_NOON = config.get('Text', 'ALERT_TXT_NOON') 31 ALERT_TXT_AFTERNOON = config.get('Text', 'ALERT_TXT_AFTERNOON') 32 ALERT_TXT_NIGHT = config.get('Text', 'ALERT_TXT_NIGHT') 33 ALERT_TXT_OFF_WORK = config.get('Text', 'ALERT_TXT_OFF_WORK') 34 ALERT_TXT_HELLO = config.get('Text', 'ALERT_TXT_HELLO') 35 36 # 獲取鬧鐘時間參數 37 CLOCK_MORNING_AWAKE = config.get('ClockTime', 'CLOCK_MORNING_AWAKE') 38 CLOCK_MORNING_GO = config.get('ClockTime', 'CLOCK_MORNING_GO') 39 CLOCK_NOON = config.get('ClockTime', 'CLOCK_NOON') 40 CLOCK_AFTERNOON = config.get('ClockTime', 'CLOCK_AFTERNOON') 41 CLOCK_EVENING = config.get('ClockTime', 'CLOCK_EVENING') 42 43 # 獲取CameraRecognition參數 44 IMAGE_SIMILARITY = config.get('CameraRecognition', 'IMAGE_SIMILARITY') 45 SAMPLE_IMAGES_NAME = config.get('CameraRecognition', 'SAMPLE_IMAGES_NAME') 46 47 # 獲取路徑 48 MP3_ALERT = RASPBERRY_PI_PATH_PLUS + config.get('Path', 'MP3_ALERT') 49 MP3_START = RASPBERRY_PI_PATH_PLUS + config.get('Path', 'MP3_START') 50 MP3_NORMAL = RASPBERRY_PI_PATH_PLUS + config.get('Path', 'MP3_NORMAL') 51 MP3_WEATHER = RASPBERRY_PI_PATH_PLUS + config.get('Path', 'MP3_WEATHER') 52 MP3_MORNING_AWAKE = RASPBERRY_PI_PATH_PLUS + config.get('Path', 'MP3_MORNING_AWAKE') 53 MP3_MORNING_GO = RASPBERRY_PI_PATH_PLUS + config.get('Path', 'MP3_MORNING_GO') 54 MP3_NOON = RASPBERRY_PI_PATH_PLUS + config.get('Path', 'MP3_NOON') 55 MP3_AFTERNOON = RASPBERRY_PI_PATH_PLUS + config.get('Path', 'MP3_AFTERNOON') 56 MP3_EVENING = RASPBERRY_PI_PATH_PLUS + config.get('Path', 'MP3_EVENING') 57 MP3_HALF_HOUR = RASPBERRY_PI_PATH_PLUS + config.get('Path', 'MP3_HALF_HOUR') 58 MP3_HOUR = RASPBERRY_PI_PATH_PLUS + config.get('Path', 'MP3_HOUR') 59 MP3_BACKGROUND = RASPBERRY_PI_PATH_PLUS + config.get('Path', 'MP3_BACKGROUND') 60 61 CLASSIFIER_PATH = RASPBERRY_PI_PATH_PLUS + config.get('Path', 'CLASSIFIER_PATH') 62 SAMPLE_IMAGES_SAVE_PATH = RASPBERRY_PI_PATH_PLUS + config.get('Path', 'SAMPLE_IMAGES_SAVE_PATH') 63 CAMERA_MONITORING_FILES_PATH = RASPBERRY_PI_PATH_PLUS + config.get('Path', 'CAMERA_MONITORING_FILES_PATH')
這下應該能夠理解爲何須要在每一個路徑前面拼接了一段了,若是在windows下,用 當前的拼接值,若是在樹莓派,則上面註釋的兩行,寫上本身的目錄絕對路徑值。
固然了,每一個腳本最上面,爲了引用其餘文件夾裏的腳本,我也進行了相應的操做:
1 # coding=utf-8 2 3 # RASPBERRY_PI_PATH_ROOT 執行Ubuntu計劃任務必須聲明絕對路徑 4 RASPBERRY_PI_PATH_ROOT = '/root/C/'
這裏按需求來吧。
一、音樂調取網易雲的每日推薦歌單,改掉如今每日播放重複歌曲的狀況