ignorance09
4/21/2016 - 3:24 PM

room.py

# -*- coding: utf-8 -*-
import requests
import re
import time

def get_xml():
    try:
        global l
        doc = requests.get("http://bbsrss.mirrors.cqupt.edu.cn/bbs/forum.php?mod=rss&fid=68&auth=0").text
        l = [[x, y] for x, y in zip(re.findall(re.compile(r'<title>(.*?)</title>'), doc)[2:],
                                    re.findall(re.compile(r'<\!\[CDATA\[([\s\S]*?)\]\]>', flags=re.M), doc))]
        print("[INFO] "+time.strftime("%Y-%m-%d-%H:%m:%S")+" "+str(len(l))+" "+str(l[0][0]))
    except Exception as e:
        print("[ERROR] " + time.strftime("%Y-%m-%d-%H:%m:%S") + " " + str(e))


def send_simple_message():
    try:
        requests.post(
        "https://api.mailgun.net/v3/mg.49.gs/messages",
        auth=("api", "key-xxxxxxxxxxxxxxxxx"),
        data={"from": "Excited User <mailgun@mg.49.gs>",
              "to": ["zjgood96@gmail.com"],
              "subject": l[0][0],
              "text": l[0][1]})
        print("[INFO] " + time.strftime("%Y-%m-%d-%H:%m:%S") + "Email Sent subject:" + str(l[0][0]))
    except Exception as e:
        print("[ERROR] " + time.strftime("%Y-%m-%d-%H:%m:%S") + " " + str(e))

f = None
while True:
    try:
        get_xml()
        if f == None:
            f = l[0][0]
        if f == l[0][0]:
            print("[INFO] " + time.strftime("%Y-%m-%d-%H:%m:%S") + " (´ ・ω・`)  ╮(๑╹◡╹๑)╭ ㄟ( ▔,▔ )ㄏ")
        else:
            f = l[0][0]
            if "出租" in l[0][0]:
                send_simple_message()
        time.sleep(30)
    except Exception as e:
        print("[ERROR] " + time.strftime("%Y-%m-%d-%H:%m:%S") + " " + str(e))
        time.sleep(5)