Skip to content

Instantly share code, notes, and snippets.

@davlgd
Created Dec 31, 2018
Embed
What would you like to do?
Vérificateur d'actus Python + Vocal
*/5 * * * * export DISPLAY=:0 && ~/ih_news_checker/ih_news_checker.py 2>&1 | /usr/bin/logger -t ih_news_checker
#!/usr/bin/env python
# -*- coding: utf-8 -*
import xml.etree.ElementTree as tree
import urllib2
import json
import os
FILE_GUID = os.path.expanduser("~") + "/.ih_news_guid_list.txt"
URL_FEED = "https://api-v1.inpact-hardware.com/rss/news.xml"
def _check_last_items():
_items = tree.parse(urllib2.urlopen(URL_FEED)).getroot().iter("item")
_news = list()
_guids = list()
for i in _items:
_news.append({ "title":i.findtext("title"),
"link":i.findtext("link"),
"link_comments":i.findtext("comments"),
"guid":i.findtext("guid"),
"description":i.findtext("description"),
"creator":i.findtext("{http://purl.org/dc/elements/1.1/}creator"),
"date":i.findtext("pubDate"),
"image":i.find("enclosure").attrib["url"]})
_guids.append(i.findtext("guid"))
return {"news":_news,"guids":_guids}
def _read_already_seen_guids():
result = list()
if os.path.isfile(FILE_GUID):
with open(FILE_GUID,"r") as f: result = f.readlines()
return result
def _extract_news(_feed_content, _guids_list):
_feed_guids_with_newline = ["%s\n" % guid for guid in _feed_content["guids"]]
_new_guids = list(set(_feed_guids_with_newline) - set(_guids_list))
_result = list()
for n in _feed_content["news"]:
if n["guid"] + "\n" in _new_guids: _result.append(n)
return _result
def check_news():
_feed_items = _check_last_items()
_already_seen_guids = _read_already_seen_guids()
_new_news = list()
if len(_already_seen_guids) == 0:
_already_seen_guids = ["%s\n" % guid for guid in _feed_items["guids"]]
with open(FILE_GUID,"w") as f: f.writelines(_already_seen_guids)
else:
_new_news = _extract_news(_feed_items, _already_seen_guids)
for n in _new_news:
with open(FILE_GUID,"a") as f: f.writelines(n["guid"] + "\n")
if len(_already_seen_guids) > 500:
with open(FILE_GUID,"w") as f: f.writelines(["%s\n" % guid for guid in _feed_items["guids"]])
return {"feed":_feed_items["news"],"new":_new_news}
if __name__ == "__main__": print json.dumps(check_news())
#!/usr/bin/env python
# -*- coding: utf-8 -*
import tts
import ih
import os
news = ih.check_news()["new"]
text_to_read = None
if len(news) == 1:
text_to_read = "INpact Hardware a publié une nouvelle actualité"
elif len(news) > 1 :
text_to_read = "INpact Hardware a publié {} nouvelles actualités".format(len(news))
if text_to_read:
tts.speak(text_to_read.decode("utf-8"))
print text_to_read + " :"
for n in news:
tts.speak(n["title"])
print n["title"].encode("utf-8")
print n["link"].encode("utf-8")
os.system("xdg-open https://www.inpact-hardware.com")
#!/usr/bin/env python
# -*- coding: utf-8 -*
import os
MODULE_TO_USE = "gtts"
TMP_FILE = "/tmp/gTTS_temp_sample"
def _gtts(phrase_to_read):
from gtts import gTTS
tts = gTTS(text=phrase_to_read, lang="fr")
tts.save(TMP_FILE + ".mp3")
os.system("mpg321 -q {}{}".format(TMP_FILE, ".mp3"))
def _pico2wave(phrase_to_read):
os.system("pico2wave -l fr-FR -w {}{} \"{}\"".format(TMP_FILE, ".wav", phrase_to_read.encode("utf-8")))
os.system("aplay -q {}{}".format(TMP_FILE, ".wav"))
def _pyttsx(phrase_to_read):
import pyttsx3
engine = pyttsx3.init()
engine.setProperty("voice", "french")
engine.setProperty("rate", 100)
engine.say(phrase_to_read)
engine.runAndWait()
def speak(phrase_to_pass):
if MODULE_TO_USE == "gtts": _gtts(phrase_to_pass)
elif MODULE_TO_USE == "pico": _pico2wave(phrase_to_pass)
else: _pyttsx(phrase_to_pass)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment