Last active
July 22, 2021 01:43
-
-
Save dntf-studio/83cc7837f5d9bc58e4456abe09322440 to your computer and use it in GitHub Desktop.
kawaii_garden-eel bot
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- conding: utf-8 -*- | |
import discord | |
from discord.ext import tasks | |
import asyncio | |
import lib_garden | |
import display | |
import time | |
import datetime | |
import subprocess | |
import RPi.GPIO as GPIO | |
GPIO.setmode(GPIO.BCM) | |
loading_led = 26 | |
loading = False | |
TOKEN = 'ゆあーとーくん。' | |
client = discord.Client() | |
try: | |
@tasks.loop(seconds=10) | |
async def loop(): | |
_now = datetime.datetime.now() | |
re = lib_garden.run_shell("vcgencmd measure_temp") | |
await display.lcd_display("Kawaii_gardeneel","CPU_temp:"+re) | |
@client.event | |
async def on_ready(): | |
print('ログインしました') | |
@client.event | |
async def on_message(message): | |
# メッセージ送信者がBotだった場合は無視する | |
if message.author.bot: | |
return | |
if message.content == '?news': | |
global loading | |
if lib_garden.check_network() and not loading: | |
loading = True | |
topics = lib_garden.news() | |
await message.channel.send(topics[0]+"\n返答速度:"+str(topics[1])+"秒") | |
loading = False | |
elif not lib_garden.check_network(): | |
await message.channel.send('インターネットに接続できないちん!') | |
else : | |
await message.channel.send('ロード中だちん!') | |
elif message.content == '?info': | |
await message.channel.send('(僕は)ちん(あなごだ)ちん!\nServer: Raspberry Pi Zero WH\nMade by dntf-studio') | |
elif message.content == '?status': | |
re = lib_garden.run_shell("vcgencmd measure_temp") | |
re2 = lib_garden.run_shell("vcgencmd measure_clock arm") | |
re3 = lib_garden.run_shell("vcgencmd measure_volts") | |
re4 = lib_garden.run_shell("vcgencmd get_mem arm") | |
await message.channel.send("CPU温度:"+re+"\nCPU周波数:"+re2+"\nCPU電圧:"+re3+"\nメモリ使用量:"+re4) | |
elif message.content == '?init': | |
GPIO.cleanup() | |
except KeyboardInterrupt: | |
print('logout') | |
GPIO.cleanup() | |
# loop start | |
loop.start() | |
# Botの起動とDiscordサーバーへの接続 | |
client.run(TOKEN) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
from bs4 import BeautifulSoup | |
import time | |
import socket | |
import subprocess | |
def news(): | |
s = time.time() | |
load_url = "https://news.yahoo.co.jp/" | |
html = requests.get(load_url) | |
soup = BeautifulSoup(html.content, "html.parser") | |
topics = soup.find_all('li',class_="sc-ksYbfQ gSBRIC") | |
# HTML全体を表示する | |
list_ = [] | |
for i in topics: | |
list_.append(i.text) | |
str_ = "\n".join(list_) | |
es = time.time() - s | |
return str_,es | |
#print(str(es)) | |
def check_network(): | |
s = socket.socket(socket.AF_INET,socket.SOCK_DGRAM) | |
try: | |
s.connect(('192.168.0.1',22)) | |
s.close() | |
return True | |
except socket.error as e: | |
return False | |
def run_shell(s): | |
proc = subprocess.run(s,shell=True,stdin=subprocess.PIPE,stdout=subprocess.PIPE,text=True) | |
result = proc.stdout.split('=') | |
return result[1].replace('\n','') | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment