Last active
March 5, 2016 15:41
-
-
Save Q2h1Cg/5fc36d14330c6ebeaaaf to your computer and use it in GitHub Desktop.
Telegram 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 | |
# -*- coding:utf-8 -*- | |
import logging | |
import random | |
import time | |
import requests | |
import telepot | |
from lxml import html | |
# logger | |
logger_console_handler = logging.StreamHandler() | |
logger_console_formatter = logging.Formatter("[%(asctime)s] [%(levelname)s] %(message)s", "%Y-%m-%d %H:%M:%S") | |
logger = logging.getLogger("Bot") | |
logger_console_handler.setFormatter(logger_console_formatter) | |
logger.addHandler(logger_console_handler) | |
logger.setLevel(logging.INFO) | |
# global variables | |
KEYWORDS = (u"japan", u"american", u"china", u"sexy", u"asia") | |
WUUU = ( | |
u"脏B!", | |
u"真 TM 脏!", | |
u"怎么这么污啊!", | |
u"注意身体!", | |
u"你家有钱也不能这么浪费纸啊!", | |
u"年少不知精子贵,老来望逼空流泪!", | |
u"迟早精尽人亡!", | |
u"凤凰傻逼", | |
u"凤凰:爸爸操我" | |
) | |
Count = {} | |
Bot = telepot.Bot("xxxoooxxxooo") | |
def get_girl(query): | |
page = random.randint(1, 10) | |
url = u"http://www.sex.com/search/pictures?query={}&page={}".format(query, page) | |
try: | |
resp = requests.get(url, timeout=3) | |
resp.close() | |
url = random.choice([i.get("data-src") for i in html.fromstring(resp.content).xpath("//img[@alt='Photo']")]) | |
filename = "/tmp/" + url.split("/")[-1] | |
resp = requests.get(url, timeout=3) | |
resp.close() | |
with open(filename, "wb") as fd: | |
fd.write(resp.content) | |
return filename | |
except Exception as err: | |
logger.error(err) | |
return get_girl(random.choice(KEYWORDS)) | |
def handle_message(msg): | |
if "chat" in msg: | |
if "username" in msg["from"]: | |
username = msg["from"]["username"] | |
else: | |
username = ".".join((msg["from"]["first_name"], msg["from"]["last_name"])) | |
userid, query = msg["chat"]["id"], msg["text"] | |
query = query.replace("@TGBBBBBBBot", "").strip() | |
# count | |
logger.info(Count) | |
user_count = Count.setdefault(username, {"time": time.time(), "count": 0}) | |
if time.time() - user_count["time"] < 300 : | |
if user_count["count"] == 3: | |
Bot.sendMessage(userid, u"{} {}".format(username, random.choice(WUUU))) | |
user_count["time"] = time.time() | |
user_count["count"] = 0 | |
else: | |
user_count["time"] = time.time() | |
if not query: | |
query = random.choice(KEYWORDS) | |
logger.info("@{} search for {}".format(username.encode("utf-8"), query.encode("utf-8"))) | |
girl = get_girl(query) | |
if girl: | |
Bot.sendMessage(userid, u"{}'s {}:".format(username, query.replace(u"你", u"我"))) | |
with open(girl, "rb") as fd: | |
Bot.sendPhoto(userid, fd) | |
logger.info("sent {} to @{}".format(girl, username)) | |
else: | |
Bot.sendMessage(userid, "no such photo") | |
Count[username]["count"] += 1 | |
Bot.notifyOnMessage(handle_message, run_forever=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment