From 6984d627f3a616acefb0d4a6d42fff6bde465aba Mon Sep 17 00:00:00 2001 From: Naoto Tsukamoto Date: Mon, 9 May 2022 12:36:47 +0900 Subject: [PATCH 01/15] [jsk_unitree_startup] Send Unitree-launch-email --- .../scripts/launch-email.py | 91 +++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100755 jsk_unitree_robot/jsk_unitree_startup/scripts/launch-email.py diff --git a/jsk_unitree_robot/jsk_unitree_startup/scripts/launch-email.py b/jsk_unitree_robot/jsk_unitree_startup/scripts/launch-email.py new file mode 100755 index 0000000000..88fec3ba63 --- /dev/null +++ b/jsk_unitree_robot/jsk_unitree_startup/scripts/launch-email.py @@ -0,0 +1,91 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +from bs4 import BeautifulSoup +import datetime +import rospy +import subprocess +import sys +if sys.version_info.major == 2: + import urllib2 +else: + from urllib import request + + +class RobotLaunchEmail: + """ + Send email when robot launched with tips. + """ + def get_tips(self): + """ + Get tips from zatsuneta random article. + + Returns: + ---------- + title : str + article title + contents : str + article contents (heading) + detail_url :str + URL of the article written about the details + """ + url = 'https://zatsuneta.com/category/random.html' + if sys.version_info.major == 2: + response = urllib2.urlopen(url) + else: + response = request.urlopen(url) + soup = BeautifulSoup(response, 'html5lib') + topstories = soup.find('div', class_="article") + title = topstories.find('a')['title'] + detail_url = topstories.find('a')['href'] + contents = topstories.find('p').text + response.close() + + return title, contents, detail_url + + def send_mail(self): + """ + Send mail with mailutils + """ + sender_address = "unitreepro@jsk.imi.i.u-tokyo.ac.jp" + receiver_address = "unitree@jsk.imi.i.u-tokyo.ac.jp" + current_time = datetime.datetime.now() + mail_title = "Unitree Go1 が起動しました" + message = "" + message += "おはよう。" + message += "{}時{}分です。\\n".format( + current_time.hour, current_time.minute) + message += "\\n" + + contents_title, contents, detail_url = self.get_tips() + message += "今日の豆知識 \\n" + # Character code differences between python versions + # See https://stackoverflow.com/questions/54153986/handling-encode-when-converting-from-python2-to-python3 #NOQA + if sys.version_info.major == 2: + message += "{}: {} \\n".format( + contents_title.encode('utf-8'), contents.encode('utf-8')) + else: + message += "{}: {} \\n".format(contents_title, contents) + message += "詳細: {}".format(detail_url) + + # echo -e option is necessary in raspberry pi + cmd = "echo -e '{}'".format(message) + cmd += " | /usr/bin/mail -s '{}' -r {} {}".format( + mail_title, sender_address, receiver_address) + exit_code = subprocess.call(cmd, shell=True) + + rospy.loginfo('Title: {}'.format(mail_title)) + if exit_code > 0: + rospy.logerr( + 'Failed to send e-mail: {} -> {}'.format( + sender_address, receiver_address)) + rospy.logerr("You may need to do '$ sudo apt install mailutils'") + else: + rospy.loginfo( + 'Succeeded to send e-mail: {} -> {}'.format( + sender_address, receiver_address)) + + +if __name__ == '__main__': + RobotLaunchEmail = RobotLaunchEmail() + RobotLaunchEmail.send_mail() From e103e6d25be4445ad57f3e43eecd516afbb48b78 Mon Sep 17 00:00:00 2001 From: Naoto Tsukamoto Date: Mon, 9 May 2022 17:11:01 +0900 Subject: [PATCH 02/15] [jsk_unitree_startup] Show weather and single comment --- .../scripts/launch-email.py | 65 ++++++++++++++++--- 1 file changed, 57 insertions(+), 8 deletions(-) diff --git a/jsk_unitree_robot/jsk_unitree_startup/scripts/launch-email.py b/jsk_unitree_robot/jsk_unitree_startup/scripts/launch-email.py index 88fec3ba63..0e45f9042b 100755 --- a/jsk_unitree_robot/jsk_unitree_startup/scripts/launch-email.py +++ b/jsk_unitree_robot/jsk_unitree_startup/scripts/launch-email.py @@ -3,6 +3,8 @@ from bs4 import BeautifulSoup import datetime +import json +import random import rospy import subprocess import sys @@ -16,6 +18,16 @@ class RobotLaunchEmail: """ Send email when robot launched with tips. """ + def __init__(self): + # Character code differences between python versions + # See https://stackoverflow.com/questions/54153986/handling-encode-when-converting-from-python2-to-python3 #NOQA + if sys.version_info.major == 2: + reload(sys) + sys.setdefaultencoding('utf-8') + api_key_file = '/var/lib/robot/openweathermap_api_key.txt' + with open(api_key_file, 'r') as f: + self.appid = f.read().split('\n')[0] + def get_tips(self): """ Get tips from zatsuneta random article. @@ -43,6 +55,47 @@ def get_tips(self): return title, contents, detail_url + # Partly copied from https://github.com/knorth55/jsk_robot/blob/b2999b0cece82d7b1d46320a1e7c84e4fc078bd2/jsk_fetch_robot/jsk_fetch_startup/scripts/time_signal.py #NOQA + def get_weather_forecast(self, lang='en'): + url = 'http://api.openweathermap.org/data/2.5/weather?q=tokyo&lang={}&units=metric&appid={}'.format(lang, self.appid) # NOQA + if sys.version_info.major == 2: + resp = json.loads(urllib2.urlopen(url).read()) + else: + resp = json.loads(request.urlopen(url).read()) + weather = resp['weather'][0]['description'] + + forecast_text = "" + if lang == 'ja': + forecast_text = "今日の天気は" + weather + "です。" + else: + forecast_text = " The weather is " + weather + " now." + + # It feels like the robot is expressing its will based on the weather. + if "晴" in weather: + forecast_tuple = ( + '日差しに気をつけて。', + 'お散歩しよう!', + ) + elif "雨" in weather: + forecast_tuple = ( + '部屋の中で遊ぼう!', + '傘忘れていない?', + ) + elif "雲" or "曇" or "くもり" in weather: + forecast_tuple = ( + '晴れたらいいな', + ) + elif "雪" in weather: + forecast_tuple = ( + '雪合戦しよう!', + '寒さに気をつけて', + ) + else: + forecast_tuple = (' ') + forecast_text += random.choice(forecast_tuple) + + return forecast_text + def send_mail(self): """ Send mail with mailutils @@ -55,18 +108,14 @@ def send_mail(self): message += "おはよう。" message += "{}時{}分です。\\n".format( current_time.hour, current_time.minute) + message += "{} \\n".format(self.get_weather_forecast(lang="ja")) message += "\\n" contents_title, contents, detail_url = self.get_tips() message += "今日の豆知識 \\n" - # Character code differences between python versions - # See https://stackoverflow.com/questions/54153986/handling-encode-when-converting-from-python2-to-python3 #NOQA - if sys.version_info.major == 2: - message += "{}: {} \\n".format( - contents_title.encode('utf-8'), contents.encode('utf-8')) - else: - message += "{}: {} \\n".format(contents_title, contents) - message += "詳細: {}".format(detail_url) + message += "{}: {} \\n".format(contents_title, contents) + message += "詳細: {} \\n".format(detail_url) + message += "\\n" # echo -e option is necessary in raspberry pi cmd = "echo -e '{}'".format(message) From 6e655ebbdf1fb8f505d4fc3dcf61a9e814cd0bea Mon Sep 17 00:00:00 2001 From: Naoto Tsukamoto Date: Tue, 10 May 2022 17:10:42 +0900 Subject: [PATCH 03/15] Fix for unitree internal environment --- jsk_unitree_robot/jsk_unitree_startup/scripts/launch-email.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/jsk_unitree_robot/jsk_unitree_startup/scripts/launch-email.py b/jsk_unitree_robot/jsk_unitree_startup/scripts/launch-email.py index 0e45f9042b..d14ee2b958 100755 --- a/jsk_unitree_robot/jsk_unitree_startup/scripts/launch-email.py +++ b/jsk_unitree_robot/jsk_unitree_startup/scripts/launch-email.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 # -*- coding: utf-8 -*- from bs4 import BeautifulSoup From 05fa77de88ba0898112bf88cdd45c203eb02f1cf Mon Sep 17 00:00:00 2001 From: Naoto Tsukamoto Date: Tue, 10 May 2022 17:11:46 +0900 Subject: [PATCH 04/15] Add sevice file for launch-email --- .../scripts/launch-email.service | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 jsk_unitree_robot/jsk_unitree_startup/scripts/launch-email.service diff --git a/jsk_unitree_robot/jsk_unitree_startup/scripts/launch-email.service b/jsk_unitree_robot/jsk_unitree_startup/scripts/launch-email.service new file mode 100644 index 0000000000..404f8328b1 --- /dev/null +++ b/jsk_unitree_robot/jsk_unitree_startup/scripts/launch-email.service @@ -0,0 +1,19 @@ +[Unit] +Description=Unitree launched email Service +Requires=dhcpcd.service +Requires=networking.service +Requires=dnsmasq.service + + +[Service] +Type=simple +ExecStart=/home/pi/jsk_catkin_ws/src/jsk_robot/jsk_unitree_robot/jsk_unitree_startup/scripts/launch-email.py +RestartSec=10s +Restart=on-failure +# KillMode=control-group +# KillSignal=SIGTERM +User=pi +Group=pi + +[Install] +WantedBy=multi-user.target From bdd220d4baeee0876dd189c13cb29e8ff1aebf9f Mon Sep 17 00:00:00 2001 From: unitreepro Date: Tue, 10 May 2022 18:37:37 +0900 Subject: [PATCH 05/15] not use rospy --- .../jsk_unitree_startup/scripts/launch-email.py | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/jsk_unitree_robot/jsk_unitree_startup/scripts/launch-email.py b/jsk_unitree_robot/jsk_unitree_startup/scripts/launch-email.py index d14ee2b958..a72e0493d9 100755 --- a/jsk_unitree_robot/jsk_unitree_startup/scripts/launch-email.py +++ b/jsk_unitree_robot/jsk_unitree_startup/scripts/launch-email.py @@ -5,7 +5,6 @@ import datetime import json import random -import rospy import subprocess import sys if sys.version_info.major == 2: @@ -123,17 +122,6 @@ def send_mail(self): mail_title, sender_address, receiver_address) exit_code = subprocess.call(cmd, shell=True) - rospy.loginfo('Title: {}'.format(mail_title)) - if exit_code > 0: - rospy.logerr( - 'Failed to send e-mail: {} -> {}'.format( - sender_address, receiver_address)) - rospy.logerr("You may need to do '$ sudo apt install mailutils'") - else: - rospy.loginfo( - 'Succeeded to send e-mail: {} -> {}'.format( - sender_address, receiver_address)) - if __name__ == '__main__': RobotLaunchEmail = RobotLaunchEmail() From cd967184d1e8c59db9b4910403bf398d58173929 Mon Sep 17 00:00:00 2001 From: MiyabiTane Date: Tue, 17 May 2022 01:10:31 +0900 Subject: [PATCH 06/15] add horoscope to email --- .../scripts/launch-email.py | 86 +++++++++++++++++++ 1 file changed, 86 insertions(+) diff --git a/jsk_unitree_robot/jsk_unitree_startup/scripts/launch-email.py b/jsk_unitree_robot/jsk_unitree_startup/scripts/launch-email.py index a72e0493d9..9c0f354fab 100755 --- a/jsk_unitree_robot/jsk_unitree_startup/scripts/launch-email.py +++ b/jsk_unitree_robot/jsk_unitree_startup/scripts/launch-email.py @@ -6,6 +6,7 @@ import json import random import subprocess +import emoji import sys if sys.version_info.major == 2: import urllib2 @@ -95,6 +96,88 @@ def get_weather_forecast(self, lang='en'): return forecast_text + def add_emoji(self, mode): + """ + 0: 普通, 1: 喜び, 2: 安心, 3: 悪巧み, 4: 驚き, 5: 悲しみ, 6: 怒り, 7: 照れ, + 8: 恐怖, 9: 好き, 10: ウインク・おふざけ, 11: 退屈, 12: 混乱 + ref: https://www.webfx.com/tools/emoji-cheat-sheet/ + """ + dic = {0: ":neutral_face:", 1: ":smile:", 2: ":relieved:", 3: ":smirk:" , +       4: ":astonished:", 5: ":cry:", 6: ":angry:", 7: ":flushed:", +   8: ":scream:", 9: ":heart_eyes:", 10: ":wink:", 11: ":sleepy:", 12: ":sweat:"} + return emoji.emojize(dic[mode], language='alias') + + def get_fortune(self): + """ + Get tips from horoscope + Return: + message : str + """ + def add_comment_rank(rank): + if rank == 1: + message = "すごい、1位だ" + self.add_emoji(4) + elif rank <= 3: + message = str(rank) + "位!いい感じ" + self.add_emoji(1) + elif rank == 12: + message = "最下位..." + self.add_emoji(5) + "ラッキーアイテムをチェックしなきゃ!!" + else: + message = str(rank) + "位かぁ。そこそこかな" + self.add_emoji(0) + return " " + message + + def add_comment_love(point): + if point >= 9: + message = "出会いを求めてお散歩しちゃおっかな" + self.add_emoji(9) + elif point >= 6: + message = "気になってるあの子に会えちゃうかも" + self.add_emoji(7) + elif point <= 3: + message = "こんなの信じないぞ" + self.add_emoji(6) + else: + message = "平凡な一日になりそう..." + self.add_emoji(11) + return " " + message + + def add_comment_money(point): + if point >= 9: + message = "今日はお買い物しちゃおうかな" + self.add_emoji(10) + elif point <= 3: + message = "お金のつかい過ぎには気をつけよう..." + self.add_emoji(8) + else: + message = "今日は何事もなさそうかな" + self.add_emoji(2) + return " " + message + + def add_comment_business(point): + if point >= 7: + message = "研究頑張ったら良いことあるかも" + self.add_emoji(1) + elif point >= 4: + message = "いいのか悪いのか分からないなぁ" + self.add_emoji(12) + else: + message = "今日は研究さぼっちゃおうかな〜" + self.add_emoji(3) + return " " + message + + url = 'https://fortune.yahoo.co.jp/12astro/sagittarius' + if sys.version_info.major == 2: + response = urllib2.urlopen(url) + message = "" + else: + response = request.urlopen(url) + soup = BeautifulSoup(response, "html.parser") + fortune = soup.find('div', id="jumpdtl").find_all('td') + f_contents = soup.find('div', class_="yftn12a-md48").find_all('dd')[0].contents[0] + rank = fortune[-5].contents[0].contents[0][0:2] + point_overall = fortune[-3].contents[0].attrs['alt'] + point_love = fortune[-3].contents[0].attrs['alt'] + point_money = fortune[-2].contents[0].attrs['alt'] + point_business = fortune[-1].contents[0].attrs['alt'] + message = "今日の星座占い:いて座の運勢は【" + rank + "】" + add_comment_rank(int(rank[0])) + "\n" + message += f_contents + "\n" + message += "だって!\n" + message += "\n" + message += "総合運: " + point_overall + "\n" + message += "恋愛運: " + point_love + add_comment_love(int(point_love[-2])) + "\n" + message += "金運: " + point_money + add_comment_money(int(point_money[-2])) + "\n" + message += "仕事運: " + point_money + add_comment_business(int(point_business[-2])) + "\n" + response.close() + return message + def send_mail(self): """ Send mail with mailutils @@ -116,6 +199,9 @@ def send_mail(self): message += "詳細: {} \\n".format(detail_url) message += "\\n" + message += self.get_fortune() + message += "\\n" + # echo -e option is necessary in raspberry pi cmd = "echo -e '{}'".format(message) cmd += " | /usr/bin/mail -s '{}' -r {} {}".format( From d2c3bcf98289ed84b3f51a2f0bf780ff3e8321ff Mon Sep 17 00:00:00 2001 From: MiyabiTane Date: Tue, 17 May 2022 14:17:14 +0900 Subject: [PATCH 07/15] fix bug --- .../scripts/launch-email.py | 42 +++++++++---------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/jsk_unitree_robot/jsk_unitree_startup/scripts/launch-email.py b/jsk_unitree_robot/jsk_unitree_startup/scripts/launch-email.py index 9c0f354fab..f4e2695950 100755 --- a/jsk_unitree_robot/jsk_unitree_startup/scripts/launch-email.py +++ b/jsk_unitree_robot/jsk_unitree_startup/scripts/launch-email.py @@ -103,8 +103,9 @@ def add_emoji(self, mode): ref: https://www.webfx.com/tools/emoji-cheat-sheet/ """ dic = {0: ":neutral_face:", 1: ":smile:", 2: ":relieved:", 3: ":smirk:" , -       4: ":astonished:", 5: ":cry:", 6: ":angry:", 7: ":flushed:", -   8: ":scream:", 9: ":heart_eyes:", 10: ":wink:", 11: ":sleepy:", 12: ":sweat:"} + 4: ":astonished:", 5: ":cry:", 6: ":angry:", 7: ":flushed:", + 8: ":scream:", 9: ":heart_eyes:", 10: ":wink:", 11: ":sleepy:", 12: ":sweat:"} + return emoji.emojize(dic[mode], language='alias') def get_fortune(self): @@ -156,26 +157,26 @@ def add_comment_business(point): url = 'https://fortune.yahoo.co.jp/12astro/sagittarius' if sys.version_info.major == 2: response = urllib2.urlopen(url) - message = "" else: response = request.urlopen(url) - soup = BeautifulSoup(response, "html.parser") - fortune = soup.find('div', id="jumpdtl").find_all('td') - f_contents = soup.find('div', class_="yftn12a-md48").find_all('dd')[0].contents[0] - rank = fortune[-5].contents[0].contents[0][0:2] - point_overall = fortune[-3].contents[0].attrs['alt'] - point_love = fortune[-3].contents[0].attrs['alt'] - point_money = fortune[-2].contents[0].attrs['alt'] - point_business = fortune[-1].contents[0].attrs['alt'] - message = "今日の星座占い:いて座の運勢は【" + rank + "】" + add_comment_rank(int(rank[0])) + "\n" - message += f_contents + "\n" - message += "だって!\n" - message += "\n" - message += "総合運: " + point_overall + "\n" - message += "恋愛運: " + point_love + add_comment_love(int(point_love[-2])) + "\n" - message += "金運: " + point_money + add_comment_money(int(point_money[-2])) + "\n" - message += "仕事運: " + point_money + add_comment_business(int(point_business[-2])) + "\n" - response.close() + soup = BeautifulSoup(response, "html.parser") + fortune = soup.find('div', id="jumpdtl").find_all('td') + f_contents = soup.find('div', class_="yftn12a-md48").find_all('dd')[0].contents[0] + rank = fortune[-5].contents[0].contents[0][0:2] + point_overall = fortune[-4].contents[0].attrs['alt'] + point_love = fortune[-3].contents[0].attrs['alt'] + point_money = fortune[-2].contents[0].attrs['alt'] + point_business = fortune[-1].contents[0].attrs['alt'] + message = "今日の星座占い:いて座の運勢は【" + rank + "】" + add_comment_rank(int(rank[0])) + "\n" + message += f_contents + "\n" + message += "だって!\n" + message += "\n" + message += "総合運: " + point_overall + "\n" + message += "恋愛運: " + point_love + add_comment_love(int(point_love[-2])) + "\n" + message += "金運: " + point_money + add_comment_money(int(point_money[-2])) + "\n" + message += "仕事運: " + point_business + add_comment_business(int(point_business[-2])) + "\n" + response.close() + return message def send_mail(self): @@ -208,7 +209,6 @@ def send_mail(self): mail_title, sender_address, receiver_address) exit_code = subprocess.call(cmd, shell=True) - if __name__ == '__main__': RobotLaunchEmail = RobotLaunchEmail() RobotLaunchEmail.send_mail() From e4df359da054ccff0517dd8a4046330d2c68df95 Mon Sep 17 00:00:00 2001 From: Naoto Tsukamoto Date: Tue, 17 May 2022 22:07:35 +0900 Subject: [PATCH 08/15] [jsk_unitree_startup] Add python3-bs4 to package.xml --- jsk_unitree_robot/jsk_unitree_startup/package.xml | 1 + 1 file changed, 1 insertion(+) diff --git a/jsk_unitree_robot/jsk_unitree_startup/package.xml b/jsk_unitree_robot/jsk_unitree_startup/package.xml index 357b10d68b..e4e119e7dc 100644 --- a/jsk_unitree_robot/jsk_unitree_startup/package.xml +++ b/jsk_unitree_robot/jsk_unitree_startup/package.xml @@ -12,6 +12,7 @@ jsk_robot_startup unitree_legged_real app_manager + python3-bs4 diagnostic_aggregator From 3d707e051afa545df10fb8b253e56a69f3c9b104 Mon Sep 17 00:00:00 2001 From: Naoto Tsukamoto Date: Wed, 18 May 2022 20:18:02 +0900 Subject: [PATCH 09/15] [jsk_unitree_startup] Display robot name in mail subject --- jsk_unitree_robot/jsk_unitree_startup/scripts/launch-email.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/jsk_unitree_robot/jsk_unitree_startup/scripts/launch-email.py b/jsk_unitree_robot/jsk_unitree_startup/scripts/launch-email.py index f4e2695950..6810dc7133 100755 --- a/jsk_unitree_robot/jsk_unitree_startup/scripts/launch-email.py +++ b/jsk_unitree_robot/jsk_unitree_startup/scripts/launch-email.py @@ -5,6 +5,7 @@ import datetime import json import random +import socket import subprocess import emoji import sys @@ -186,7 +187,7 @@ def send_mail(self): sender_address = "unitreepro@jsk.imi.i.u-tokyo.ac.jp" receiver_address = "unitree@jsk.imi.i.u-tokyo.ac.jp" current_time = datetime.datetime.now() - mail_title = "Unitree Go1 が起動しました" + mail_title = "{} が起動しました".format(socket.gethostname()) message = "" message += "おはよう。" message += "{}時{}分です。\\n".format( From 11f1712989e300c0436d5ab59103aa2e90545fc5 Mon Sep 17 00:00:00 2001 From: MiyabiTane Date: Sun, 22 May 2022 16:48:13 +0900 Subject: [PATCH 10/15] fix bug --- .../jsk_unitree_startup/scripts/launch-email.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/jsk_unitree_robot/jsk_unitree_startup/scripts/launch-email.py b/jsk_unitree_robot/jsk_unitree_startup/scripts/launch-email.py index 6810dc7133..bbccc3b3b2 100755 --- a/jsk_unitree_robot/jsk_unitree_startup/scripts/launch-email.py +++ b/jsk_unitree_robot/jsk_unitree_startup/scripts/launch-email.py @@ -168,14 +168,18 @@ def add_comment_business(point): point_love = fortune[-3].contents[0].attrs['alt'] point_money = fortune[-2].contents[0].attrs['alt'] point_business = fortune[-1].contents[0].attrs['alt'] - message = "今日の星座占い:いて座の運勢は【" + rank + "】" + add_comment_rank(int(rank[0])) + "\n" + rank_int = int(re.sub(r"\D", "", rank)) + point_love_int = int(re.sub(r"\D", "", point_love)[2:]) + point_money_int = int(re.sub(r"\D", "", point_money)[2:]) + point_business_int = int(re.sub(r"\D", "", point_business)[2:]) + message = "今日の星座占い:いて座の運勢は【" + rank + "】" + add_comment_rank(rank_int) + "\n" message += f_contents + "\n" message += "だって!\n" message += "\n" message += "総合運: " + point_overall + "\n" - message += "恋愛運: " + point_love + add_comment_love(int(point_love[-2])) + "\n" - message += "金運: " + point_money + add_comment_money(int(point_money[-2])) + "\n" - message += "仕事運: " + point_business + add_comment_business(int(point_business[-2])) + "\n" + message += "恋愛運: " + point_love + add_comment_love(point_love_int) + "\n" + message += "金運: " + point_money + add_comment_money(point_money_int) + "\n" + message += "仕事運: " + point_business + add_comment_business(point_business_int) + "\n" response.close() return message From 026783690e7d8c6b8de5eb7a593bb0aa2fe88897 Mon Sep 17 00:00:00 2001 From: MiyabiTane Date: Sun, 22 May 2022 18:13:24 +0900 Subject: [PATCH 11/15] =?UTF-8?q?=E8=AA=95=E7=94=9F=E6=97=A5=E3=81=AE?= =?UTF-8?q?=E6=8C=87=E5=AE=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../scripts/launch-email.py | 61 ++++++++++++++++++- 1 file changed, 58 insertions(+), 3 deletions(-) diff --git a/jsk_unitree_robot/jsk_unitree_startup/scripts/launch-email.py b/jsk_unitree_robot/jsk_unitree_startup/scripts/launch-email.py index bbccc3b3b2..f89d14bb60 100755 --- a/jsk_unitree_robot/jsk_unitree_startup/scripts/launch-email.py +++ b/jsk_unitree_robot/jsk_unitree_startup/scripts/launch-email.py @@ -9,6 +9,8 @@ import subprocess import emoji import sys +import argparse +import re if sys.version_info.major == 2: import urllib2 else: @@ -28,6 +30,8 @@ def __init__(self): api_key_file = '/var/lib/robot/openweathermap_api_key.txt' with open(api_key_file, 'r') as f: self.appid = f.read().split('\n')[0] + self.constellation = "sagittarius" + self.constellation_jpn = "いて" def get_tips(self): """ @@ -109,6 +113,52 @@ def add_emoji(self, mode): return emoji.emojize(dic[mode], language='alias') + def set_constellation(self): + parser = argparse.ArgumentParser() + parser.add_argument("--month", default="12") + parser.add_argument("--days", default="14") + args = parser.parse_args() + + birthday_num = int(args.month) * 100 + int(args.days) + if 1221 < birthday_num <= 1231 or 101 <= birthday_num <= 119: + self.constellation = "capricorn" + self.constellation_jpn = "やぎ" + elif 119 < birthday_num <= 131 or 201 <= birthday_num <= 218: + self.constellation = "aquarius" + self.constellation_jpn = "みずがめ" + elif 218 < birthday_num <= 229 or 301 <= birthday_num <= 320: + self.constellation = "pisces" + self.constellation_jpn = "うお" + elif 320 < birthday_num <= 331 or 401 <= birthday_num <= 419: + self.constellation = "aries" + self.constellation_jpn = "おひつじ" + elif 419 < birthday_num <= 430 or 501 <= birthday_num <= 520: + self.constellation = "taurus" + self.constellation_jpn = "おうし" + elif 520 < birthday_num <= 531 or 601 <= birthday_num <= 621: + self.constellation = "gemini" + self.constellation_jpn = "ふたご" + elif 621 < birthday_num <= 630 or 701 <= birthday_num <= 722: + self.constellation = "cancer" + self.constellation_jpn = "かに" + elif 722 < birthday_num <= 731 or 801 <= birthday_num <= 822: + self.constellation = "leo" + self.constellation_jpn = "しし" + elif 822 < birthday_num <= 831 or 901 <= birthday_num <= 922: + self.constellation = "virgo" + self.constellation_jpn = "おとめ" + elif 922 < birthday_num <= 930 or 1001 <= birthday_num <= 1023: + self.constellation = "libra" + self.constellation_jpn = "てんびん" + elif 1023 < birthday_num <= 1031 or 1101 <= birthday_num <= 1121: + self.constellation = "scorpio" + self.constellation_jpn = "さそり" + elif 1121 < birthday_num <= 1130 or 1201 <= birthday_num <= 1221: + self.constellation = "sagittarius" + self.constellation_jpn = "いて" + else: + print("Inappropriate birthday. Set default constellation: sagittarius") + def get_fortune(self): """ Get tips from horoscope @@ -155,7 +205,7 @@ def add_comment_business(point): message = "今日は研究さぼっちゃおうかな〜" + self.add_emoji(3) return " " + message - url = 'https://fortune.yahoo.co.jp/12astro/sagittarius' + url = 'https://fortune.yahoo.co.jp/12astro/'+ self.constellation if sys.version_info.major == 2: response = urllib2.urlopen(url) else: @@ -163,7 +213,10 @@ def add_comment_business(point): soup = BeautifulSoup(response, "html.parser") fortune = soup.find('div', id="jumpdtl").find_all('td') f_contents = soup.find('div', class_="yftn12a-md48").find_all('dd')[0].contents[0] - rank = fortune[-5].contents[0].contents[0][0:2] + try: + rank = re.sub(r"\D", "", fortune[-5].contents[0].contents[0]) + "位" + except: + rank = re.sub(r"\D", "", fortune[-5].contents[0].attrs['alt']) + "位" point_overall = fortune[-4].contents[0].attrs['alt'] point_love = fortune[-3].contents[0].attrs['alt'] point_money = fortune[-2].contents[0].attrs['alt'] @@ -172,7 +225,8 @@ def add_comment_business(point): point_love_int = int(re.sub(r"\D", "", point_love)[2:]) point_money_int = int(re.sub(r"\D", "", point_money)[2:]) point_business_int = int(re.sub(r"\D", "", point_business)[2:]) - message = "今日の星座占い:いて座の運勢は【" + rank + "】" + add_comment_rank(rank_int) + "\n" + message = "今日の星座占い:" + message += self.constellation_jpn + "座の運勢は【" + rank + "】" + add_comment_rank(rank_int) + "\n" message += f_contents + "\n" message += "だって!\n" message += "\n" @@ -216,4 +270,5 @@ def send_mail(self): if __name__ == '__main__': RobotLaunchEmail = RobotLaunchEmail() + RobotLaunchEmail.set_constellation() RobotLaunchEmail.send_mail() From a2e1652a1360ffff32b23f0dfe1ec228717d725d Mon Sep 17 00:00:00 2001 From: Naoto Tsukamoto Date: Wed, 8 Jun 2022 22:02:49 +0900 Subject: [PATCH 12/15] [jsk_unitree_startup] Use email_topic.py to send email --- .../scripts/launch-email.py | 62 ++++++++++++------- 1 file changed, 40 insertions(+), 22 deletions(-) diff --git a/jsk_unitree_robot/jsk_unitree_startup/scripts/launch-email.py b/jsk_unitree_robot/jsk_unitree_startup/scripts/launch-email.py index f89d14bb60..b825af0124 100755 --- a/jsk_unitree_robot/jsk_unitree_startup/scripts/launch-email.py +++ b/jsk_unitree_robot/jsk_unitree_startup/scripts/launch-email.py @@ -6,15 +6,17 @@ import json import random import socket -import subprocess import emoji import sys import argparse import re +import rospy if sys.version_info.major == 2: import urllib2 else: from urllib import request +from jsk_robot_startup.msg import Email +from jsk_robot_startup.msg import EmailBody class RobotLaunchEmail: @@ -24,6 +26,7 @@ class RobotLaunchEmail: def __init__(self): # Character code differences between python versions # See https://stackoverflow.com/questions/54153986/handling-encode-when-converting-from-python2-to-python3 #NOQA + rospy.init_node('robot_launch_email') if sys.version_info.major == 2: reload(sys) sys.setdefaultencoding('utf-8') @@ -32,6 +35,7 @@ def __init__(self): self.appid = f.read().split('\n')[0] self.constellation = "sagittarius" self.constellation_jpn = "いて" + self.pub = rospy.Publisher("email", Email, queue_size=10) def get_tips(self): """ @@ -240,33 +244,47 @@ def add_comment_business(point): def send_mail(self): """ - Send mail with mailutils + Send mail with email node """ - sender_address = "unitreepro@jsk.imi.i.u-tokyo.ac.jp" - receiver_address = "unitree@jsk.imi.i.u-tokyo.ac.jp" + msg = Email() + + greeting = EmailBody() + greeting.type = 'text' + greeting.message = "おはよう。" current_time = datetime.datetime.now() - mail_title = "{} が起動しました".format(socket.gethostname()) - message = "" - message += "おはよう。" - message += "{}時{}分です。\\n".format( + greeting.message += "{}時{}分です。\n".format( current_time.hour, current_time.minute) - message += "{} \\n".format(self.get_weather_forecast(lang="ja")) - message += "\\n" + greeting.message += "{} \n".format( + self.get_weather_forecast(lang="ja")) + + content_tips = EmailBody() + content_tips.type = 'text' + tips_title, tips, detail_url = self.get_tips() + content_tips.message = "今日の豆知識 \n" + content_tips.message += "{}: {} \n".format(tips_title, tips) + content_tips.message += "詳細: {} \n".format(detail_url) + + content_fortune = EmailBody() + content_fortune.type = 'text' + content_fortune.message = self.get_fortune() + + change_line = EmailBody() + change_line.type = 'html' + change_line.message = "
" - contents_title, contents, detail_url = self.get_tips() - message += "今日の豆知識 \\n" - message += "{}: {} \\n".format(contents_title, contents) - message += "詳細: {} \\n".format(detail_url) - message += "\\n" + msg.body = [greeting, + change_line, + content_tips, + change_line, + content_fortune] + msg.subject = "{} が起動しました".format(socket.gethostname()) + msg.sender_address = "unitreepro@jsk.imi.i.u-tokyo.ac.jp" + msg.receiver_address = "unitree@jsk.imi.i.u-tokyo.ac.jp" - message += self.get_fortune() - message += "\\n" + rospy.sleep(1) + rospy.loginfo("Send mail") + self.pub.publish(msg) - # echo -e option is necessary in raspberry pi - cmd = "echo -e '{}'".format(message) - cmd += " | /usr/bin/mail -s '{}' -r {} {}".format( - mail_title, sender_address, receiver_address) - exit_code = subprocess.call(cmd, shell=True) if __name__ == '__main__': RobotLaunchEmail = RobotLaunchEmail() From 52ce98ed0eff5b3fa057f2b7b4e90fed60ff5c77 Mon Sep 17 00:00:00 2001 From: Naoto Tsukamoto Date: Wed, 8 Jun 2022 22:09:43 +0900 Subject: [PATCH 13/15] [jsk_unitree_startup] Use unitree's autostart process to send email --- .../launch/unitree_bringup.launch | 5 +++++ .../scripts/launch-email.service | 19 ------------------- 2 files changed, 5 insertions(+), 19 deletions(-) delete mode 100644 jsk_unitree_robot/jsk_unitree_startup/scripts/launch-email.service diff --git a/jsk_unitree_robot/jsk_unitree_startup/launch/unitree_bringup.launch b/jsk_unitree_robot/jsk_unitree_startup/launch/unitree_bringup.launch index 6ae4f34cf1..81694e32ae 100644 --- a/jsk_unitree_robot/jsk_unitree_startup/launch/unitree_bringup.launch +++ b/jsk_unitree_robot/jsk_unitree_startup/launch/unitree_bringup.launch @@ -36,4 +36,9 @@ + + + + diff --git a/jsk_unitree_robot/jsk_unitree_startup/scripts/launch-email.service b/jsk_unitree_robot/jsk_unitree_startup/scripts/launch-email.service deleted file mode 100644 index 404f8328b1..0000000000 --- a/jsk_unitree_robot/jsk_unitree_startup/scripts/launch-email.service +++ /dev/null @@ -1,19 +0,0 @@ -[Unit] -Description=Unitree launched email Service -Requires=dhcpcd.service -Requires=networking.service -Requires=dnsmasq.service - - -[Service] -Type=simple -ExecStart=/home/pi/jsk_catkin_ws/src/jsk_robot/jsk_unitree_robot/jsk_unitree_startup/scripts/launch-email.py -RestartSec=10s -Restart=on-failure -# KillMode=control-group -# KillSignal=SIGTERM -User=pi -Group=pi - -[Install] -WantedBy=multi-user.target From 2a7469a809629c7486880bf2601b29a74c8387bf Mon Sep 17 00:00:00 2001 From: Naoto Tsukamoto Date: Wed, 8 Jun 2022 22:25:32 +0900 Subject: [PATCH 14/15] [jsk_unitree_startup] Fix format --- .../scripts/launch-email.py | 50 +++++++++++-------- 1 file changed, 30 insertions(+), 20 deletions(-) diff --git a/jsk_unitree_robot/jsk_unitree_startup/scripts/launch-email.py b/jsk_unitree_robot/jsk_unitree_startup/scripts/launch-email.py index b825af0124..9dbca74fdc 100755 --- a/jsk_unitree_robot/jsk_unitree_startup/scripts/launch-email.py +++ b/jsk_unitree_robot/jsk_unitree_startup/scripts/launch-email.py @@ -1,22 +1,22 @@ #!/usr/bin/env python3 # -*- coding: utf-8 -*- +import argparse from bs4 import BeautifulSoup import datetime +import emoji import json -import random import socket -import emoji import sys -import argparse +import random import re import rospy +from jsk_robot_startup.msg import Email +from jsk_robot_startup.msg import EmailBody if sys.version_info.major == 2: import urllib2 else: from urllib import request -from jsk_robot_startup.msg import Email -from jsk_robot_startup.msg import EmailBody class RobotLaunchEmail: @@ -111,9 +111,10 @@ def add_emoji(self, mode): 8: 恐怖, 9: 好き, 10: ウインク・おふざけ, 11: 退屈, 12: 混乱 ref: https://www.webfx.com/tools/emoji-cheat-sheet/ """ - dic = {0: ":neutral_face:", 1: ":smile:", 2: ":relieved:", 3: ":smirk:" , - 4: ":astonished:", 5: ":cry:", 6: ":angry:", 7: ":flushed:", - 8: ":scream:", 9: ":heart_eyes:", 10: ":wink:", 11: ":sleepy:", 12: ":sweat:"} + dic = {0: ":neutral_face:", 1: ":smile:", 2: ":relieved:", + 3: ":smirk:", 4: ":astonished:", 5: ":cry:", + 6: ":angry:", 7: ":flushed:", 8: ":scream:", + 9: ":heart_eyes:", 10: ":wink:", 11: ":sleepy:", 12: ":sweat:"} return emoji.emojize(dic[mode], language='alias') @@ -161,7 +162,8 @@ def set_constellation(self): self.constellation = "sagittarius" self.constellation_jpn = "いて" else: - print("Inappropriate birthday. Set default constellation: sagittarius") + rospy.logerr("Inappropriate birthday." + "Set default constellation: sagittarius") def get_fortune(self): """ @@ -175,7 +177,9 @@ def add_comment_rank(rank): elif rank <= 3: message = str(rank) + "位!いい感じ" + self.add_emoji(1) elif rank == 12: - message = "最下位..." + self.add_emoji(5) + "ラッキーアイテムをチェックしなきゃ!!" + message = "最下位..."\ + + self.add_emoji(5)\ + + "ラッキーアイテムをチェックしなきゃ!!" else: message = str(rank) + "位かぁ。そこそこかな" + self.add_emoji(0) return " " + message @@ -209,18 +213,21 @@ def add_comment_business(point): message = "今日は研究さぼっちゃおうかな〜" + self.add_emoji(3) return " " + message - url = 'https://fortune.yahoo.co.jp/12astro/'+ self.constellation + url = 'https://fortune.yahoo.co.jp/12astro/' + self.constellation if sys.version_info.major == 2: response = urllib2.urlopen(url) else: response = request.urlopen(url) soup = BeautifulSoup(response, "html.parser") fortune = soup.find('div', id="jumpdtl").find_all('td') - f_contents = soup.find('div', class_="yftn12a-md48").find_all('dd')[0].contents[0] + f_contents = soup.find( + 'div', class_="yftn12a-md48").find_all('dd')[0].contents[0] try: - rank = re.sub(r"\D", "", fortune[-5].contents[0].contents[0]) + "位" - except: - rank = re.sub(r"\D", "", fortune[-5].contents[0].attrs['alt']) + "位" + rank = re.sub( + r"\D", "", fortune[-5].contents[0].contents[0]) + "位" + except BaseException: + rank = re.sub( + r"\D", "", fortune[-5].contents[0].attrs['alt']) + "位" point_overall = fortune[-4].contents[0].attrs['alt'] point_love = fortune[-3].contents[0].attrs['alt'] point_money = fortune[-2].contents[0].attrs['alt'] @@ -229,15 +236,18 @@ def add_comment_business(point): point_love_int = int(re.sub(r"\D", "", point_love)[2:]) point_money_int = int(re.sub(r"\D", "", point_money)[2:]) point_business_int = int(re.sub(r"\D", "", point_business)[2:]) - message = "今日の星座占い:" - message += self.constellation_jpn + "座の運勢は【" + rank + "】" + add_comment_rank(rank_int) + "\n" + message = "今日の星座占い:" + self.constellation_jpn\ + + "座の運勢は【" + rank + "】" + add_comment_rank(rank_int) + "\n" message += f_contents + "\n" message += "だって!\n" message += "\n" message += "総合運: " + point_overall + "\n" - message += "恋愛運: " + point_love + add_comment_love(point_love_int) + "\n" - message += "金運: " + point_money + add_comment_money(point_money_int) + "\n" - message += "仕事運: " + point_business + add_comment_business(point_business_int) + "\n" + message += "恋愛運: " + point_love\ + + add_comment_love(point_love_int) + "\n" + message += "金運: " + point_money\ + + add_comment_money(point_money_int) + "\n" + message += "仕事運: " + point_business\ + + add_comment_business(point_business_int) + "\n" response.close() return message From 2ee9f4bb3e52e60e7b7f144ad0c495178dbc5c4a Mon Sep 17 00:00:00 2001 From: Naoto Tsukamoto Date: Wed, 8 Jun 2022 22:30:55 +0900 Subject: [PATCH 15/15] [jsk_unitree_startup] Change file name launch-email.py -> launch_email.py --- .../jsk_unitree_startup/launch/unitree_bringup.launch | 2 +- .../scripts/{launch-email.py => launch_email.py} | 0 2 files changed, 1 insertion(+), 1 deletion(-) rename jsk_unitree_robot/jsk_unitree_startup/scripts/{launch-email.py => launch_email.py} (100%) diff --git a/jsk_unitree_robot/jsk_unitree_startup/launch/unitree_bringup.launch b/jsk_unitree_robot/jsk_unitree_startup/launch/unitree_bringup.launch index 81694e32ae..5c3284b694 100644 --- a/jsk_unitree_robot/jsk_unitree_startup/launch/unitree_bringup.launch +++ b/jsk_unitree_robot/jsk_unitree_startup/launch/unitree_bringup.launch @@ -38,7 +38,7 @@ name="wakeup" output="log" /> - diff --git a/jsk_unitree_robot/jsk_unitree_startup/scripts/launch-email.py b/jsk_unitree_robot/jsk_unitree_startup/scripts/launch_email.py similarity index 100% rename from jsk_unitree_robot/jsk_unitree_startup/scripts/launch-email.py rename to jsk_unitree_robot/jsk_unitree_startup/scripts/launch_email.py