Created
November 10, 2017 06:42
-
-
Save WangYihang/bc1b174afbc37b13f5bb1f33e36efe82 to your computer and use it in GitHub Desktop.
QQ-Bot of SniperOJ
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 | |
# coding:utf-8 | |
from qqbot import QQBotSlot as qqbotslot, RunBot | |
from qqbot import qcontactdb | |
import json | |
import requests | |
import sys | |
import random | |
reload(sys) | |
sys.setdefaultencoding('utf-8') | |
rank_options = ['rank', 'ranking', '排名', '名次'] | |
progress_options = ['战况', 'status', 'progress', '情况'] | |
# tirck_optione = ['帅吗', '美吗', '机器人'] | |
subsrcibe = ['大佬好强~', '大佬好厉害~', '大佬好棒~'] | |
common = ['讨厌~ at 人家干嘛~', '你好讨厌哦!', '`'] | |
def get_options(content): | |
for i in rank_options: | |
if i in content: | |
return "rank" | |
for i in progress_options: | |
if i in content: | |
return 'progress' | |
return 'unknown' | |
def get_content(url): | |
return requests.get(url).text.decode("UTF-8") | |
def get_progress(): | |
url = 'http://sniperoj.cn/challenge/progress_qqbot' | |
data = get_content(url) | |
json_data = json.loads(data) | |
output_data = [] | |
for i in json_data: | |
username = i['username'].replace(" ", "") | |
challenge_name = i['challenge_name'].replace(" ", "") | |
submit_time = i['submit_time'].replace(" ", "") | |
output = "恭喜 : %s大佬在%s解出了题目%s" % (username.decode("UTF-8"), submit_time.decode("UTF-8"), challenge_name.decode("UTF-8")) | |
output_data.append(output) | |
return output_data | |
def build_message(message_list): | |
result = "" | |
for i in message_list: | |
result += i + "\n" | |
if result.endswith("\n"): | |
return result[0:-1] | |
return result | |
def is_progress(content): | |
return 'progress' == get_options(content) | |
def is_rank(content): | |
return 'rank' == get_options(content) | |
def get_rank(): | |
url = 'http://sniperoj.cn/user/rank_qqbot' | |
data = get_content(url) | |
json_data = json.loads(data) | |
output_data = [] | |
counter = 0 | |
for i in json_data: | |
counter += 1 | |
username = i['username'].replace(" ", "") | |
score = i['score'].replace(" ", "") | |
output = "%d. 用户名 : [%s] 分数 : [%s]" % (counter, username, score) | |
output_data.append(output) | |
return output_data | |
@qqbotslot | |
def onQQMessage(bot, contact, member, content): | |
if '@SniperOJ机器人' in content: | |
if is_progress(content): | |
message = build_message(get_progress()) | |
if message != "": | |
bot.SendTo(contact, member.name+'您好 , 目前最新战况如下 : \n' + message, resendOn1202=False) | |
else: | |
bot.SendTo(contact, "哇 , 现在居然没人做题... 这可是超越他们的好机会~", resendOn1202=False) | |
return | |
if is_rank(content): | |
message = build_message(get_rank()) | |
bot.SendTo(contact, member.name+'您好 , 目前最新排名情况如下 : \n' + message, resendOn1202=False) | |
return | |
if 'flag' in content: | |
bot.SendTo(contact, 'SniperOJ{1_aM_A_smart_robot}', resendOn1202=False) | |
return | |
bot.SendTo(contact, '讨厌啦 , @ 人家干嘛 ? 输入[排名|战况]试试吧~', resendOn1202=False) | |
def get_new_progress(): | |
url = 'http://sniperoj.cn/challenge/new_progress_qqbot' | |
data = get_content(url) | |
json_data = json.loads(data) | |
output_data = [] | |
for i in json_data: | |
username = i['username'].replace(" ", "") | |
challenge_name = i['challenge_name'].replace(" ", "") | |
submit_time = i['submit_time'].replace(" ", "") | |
output = "恭喜 : %s大佬在%s解出了题目%s" % (username.decode("UTF-8"), submit_time.decode("UTF-8"), challenge_name.decode("UTF-8")) | |
output_data.append(output) | |
return output_data | |
@qqbotslot | |
def onInterval(bot): | |
# 每隔 5 分钟被调用 | |
# 现在已经被我改成 20 秒了 | |
# bot : QQBot 对象 | |
print "[+] 每 20 秒调用一次 , 获取最新战况" | |
message = build_message(get_new_progress()) | |
if len(message) == 0: | |
print "[+] 暂时没有解题者" | |
return | |
print "[+] Sending..." | |
gl = bot.List('group', 'SniperOJ') | |
if gl is not None: | |
for group in gl: | |
bot.SendTo(group, random.choice(subscribe) + ' orz 膜膜膜!!!' + message, resendOn1202=False) | |
print "[+] Send over!" | |
if __name__ == '__main__': | |
RunBot() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment