有人提议,我就写个。
老规矩,GPLv2,自己测试,风险自担。
https://gist.github.com/cnbeining/b77a4f5762a78fb3878c
#!/usr/bin/env python #coding:utf-8 # Author: Beining --<i@cnbeining.com> # Purpose: Auto reply Acfun's msg # Created: 01/05/2016 # License: GPLv2 import sys import unittest import requests import json import urllib.parse import time cookiepath = '' VER = '0.01' global cookiepath global HEADER global UA import re global regex_expression_list regex_expression_list = [ ('ACICFG', 'Hi!') ] #---------------------------------------------------------------------- def read_cookie(cookiepath): """str->list Original target: set the cookie Target now: Set the global header""" #print(cookiepath) try: cookies_file = open(cookiepath, 'r') cookies = cookies_file.readlines() cookies_file.close() # print(cookies) return cookies except Exception: print('Cannot read cookie, may affect some videos...') return [''] #---------------------------------------------------------------------- def get_unread_list(): """""" req = requests.get('http://www.acfun.tv/api/mail.aspx?name=getGroups&page=1', headers = HEADER) content = json.loads(req.content) return [str(i) for i in b['unReadList']] #---------------------------------------------------------------------- def get_last_msg_by_p2p(p2p): """""" req = requests.get('http://www.acfun.tv/api/mail.aspx?name=getMails&page=1&p2p={p2p}'.format(p2p = p2p), headers = HEADER) content = json.loads(req.content) return json.loads(content['mailList'][0]) #---------------------------------------------------------------------- def get_regex_object_list(regex_list): """""" list_this = [] for i in regex_list: list_this.append(((eval('re.compile(r"{regex}")'.format(regex = i[0]))), i[1])) return list_this #---------------------------------------------------------------------- def get_reply_by_text(text): """""" for i in regex_object_list: if i[0].match(mail): return i[1] #---------------------------------------------------------------------- def send_reply(userId, content): """""" content = urllib.parse.quote(content) data = 'userId={userId}&content={content}'.format(userId = userId, content = content) requests.post('http://www.acfun.tv/api/mail.aspx?name=newMail', headers=HEADER, data=data) #---------------------------------------------------------------------- def main(): """""" unReadList = get_unread_list() for p2p in unReadList: mail = get_last_msg_by_p2p(p2p) reply = get_reply_by_text(mail['text']) fromuId = mail['fromuId'] fromusername = mail['fromusername'] send_reply(fromuId, reply) print('Received: {mail} from {fromusername}, Sent: {reply}') #---------------------------------------------------------------------- def usage(): """""" pass if __name__=='__main__': argv_list = [] argv_list = sys.argv[1:] cookiepath,wait_time = '', 5 try: opts, args = getopt.getopt(argv_list, "hc:t:", ['help', "cookie=", 'time=']) except getopt.GetoptError: usage() exit() for o, a in opts: if o in ('-h', '--help'): usage() exit() if o in ('-c', '--cookie'): cookiepath = a if o in ('-t', '--time'): wait_time = a if cookiepath == '': cookiepath = './accookies' if not os.path.exists(cookiepath): print('Unable to open the cookie\'s file!') print('Please put your cookie in the file \"bilicookies\" or set a path yourself') exit() cookies = read_cookie(cookiepath)[0] UA = 'Acfun Auto Msg/{VER} (cnbeining@gmail.com) (Python-urllib/{python_ver_str}, like libcurl/1.0 NSS-Mozilla/2.0)'.format(VER = VER, python_ver_str = python_ver_str) HEADER = {'User-Agent': UA, 'Cache-Control': 'no-cache', 'Pragma': 'no-cache', 'Cookie': cookies[0]} regex_object_list = get_regex_object_list(regex_expression_list) global regex_object_list while 1: main() time.sleep(wait_time)