commit
986b46f558
1 changed files with 65 additions and 0 deletions
@ -0,0 +1,65 @@
|
||||
import requests |
||||
import time |
||||
from datetime import datetime |
||||
import sys |
||||
|
||||
class Puppet: |
||||
def __init__(self, token): |
||||
self.token = token |
||||
self.api = r'https://hole.thu.monster/_api/v1/' |
||||
self.ua = r'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.93 Safari/537.36' |
||||
|
||||
def post(self, content, *, cw='', allow_search=False): |
||||
payload = { |
||||
'cw': cw, |
||||
'text': content, |
||||
'allow_search': '1' if allow_search else '', |
||||
'type': 'text', |
||||
} |
||||
headers = { |
||||
'user-agent': self.ua, |
||||
'user-token': self.token, |
||||
} |
||||
url = self.api + 'dopost' |
||||
r = requests.post(url, data=payload, headers=headers) |
||||
print(r) |
||||
|
||||
|
||||
def getClock(now): |
||||
sentence_api = 'https://international.v1.hitokoto.cn/' |
||||
params = [ |
||||
['c', 'd'], |
||||
['c', 'i'], |
||||
['c', 'k'], |
||||
] # 见https://developer.hitokoto.cn/sentence/#%E8%BF%94%E5%9B%9E%E6%A0%BC%E5%BC%8F |
||||
try: |
||||
r = requests.get(sentence_api, params=params) |
||||
data = r.json() |
||||
except Exception as e: |
||||
print(e) |
||||
data = None |
||||
if data: |
||||
hitokoto = data['hitokoto'] |
||||
place = data['from'] |
||||
author = data['from_who'] |
||||
sentence = f'{hitokoto}\n ——{author}《{place}》' |
||||
else: |
||||
sentence = '' |
||||
|
||||
now_str = now.strftime(r'%Y-%m-%d %H:%M') |
||||
content = f'#报时 **北京时间 {now_str}**\n{sentence}' |
||||
return content |
||||
|
||||
|
||||
delay = 10 |
||||
puppet = Puppet(sys.argv[1]) |
||||
|
||||
while True: |
||||
now = datetime.now() |
||||
# print(getClock(now)) |
||||
print(now) |
||||
if now.minute == 0 or now.minute == 30: |
||||
content = getClock(now) |
||||
puppet.post(content, allow_search=True) |
||||
delay = 1800 - now.second |
||||
time.sleep(delay) |
Loading…
Reference in new issue