Acfun自动老司机:自动按正则回复私信

有人提议,我就写个。
老规矩,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)

 

5 thoughts on “Acfun自动老司机:自动按正则回复私信

    1. Beining Post author

      但是发车方法还是有限。毕竟常见的便于解密的encode方法就那么几种 而对称加密。。。我觉得没人能猜出密码(
      话说就没人发现其实我的这点code跑不起来?

      Reply
      1. 板砖加身

        自适应链接,前几天换公共库时学的。
        算法加密已经弱爆了,这两天用莫尔斯电码,五线谱,八卦发车的比比皆是,想上车就得当文武全才,佩服劳动人民的智慧。
        我觉得没人发现的原因是没人知道这段 code/没人会用……

        Reply

Leave a Reply

Your email address will not be published. Required fields are marked *