Skip to content

Instantly share code, notes, and snippets.

@fzls
Last active May 8, 2019 14:43
Show Gist options
  • Save fzls/b39fa8597fb3db612e39c170ab417094 to your computer and use it in GitHub Desktop.
Save fzls/b39fa8597fb3db612e39c170ab417094 to your computer and use it in GitHub Desktop.
cc98上发帖批量散魔力值的辅助脚本

在cc98上散魔力值用的小脚本

使用环境

  1. 使用python3
  2. 按照requests和beautifulsoup4库,若已安装pip,则可直接 pip install -r requirements.txt

使用说明

  1. python batch_exchange.py
  2. 按照提示依次输入各值,说明如下
    1. cookie的获取方法(以chrome为例):先登录NexusHD,然后按F12打开开发者工具,按F5刷新界面,在网络tab中查找名称为www.nexushd.org的请求,单击后,在右侧小窗口中点击headers,在下方的request headers中找到cookie条目,将:右侧的一长串值copy下来。
      1. 示例:Cookie: c_secure_uid=Nzkw*******; c_secure_pass=cfd36aabbddad8d2bae*******; c_secure_ssl=bm9w*******; c_secure_tracker_ssl=bm9w*******; c_secure_login=bm*******
      2. 为了隐私考虑,上面示例中部分内容被隐去,用*********代替,不影响理解
    2. board_id: 点击你发表的帖子,观察其地址栏中的url,假设url为 http://www.cc98.org/dispbbs.asp?boardid=135&id=4716087 ,那么其中的 boardid= 后面的135就是我们想要的值
    3. post_id: 上面的例子中的url中的 id= 后的 4716087
    4. message: 你想让对方接收礼物时看到的信息内容
    5. magic_amount: 每人赠送的魔力值个数,应为正整数
  3. 由于N站限制,请在下列时间外使用该脚本
    1. 对不起,魔力值交换功能暂停开放中。每天暂停开放时间:2:00am - 6:00am

版权声明

powered by 风之凌殇

import re
import requests
from bs4 import BeautifulSoup
# 赠送的网址(post)
exchange_url = "http://www.nexushd.org/mybonus.php?action=exchange"
# 98的帖子网址
cc98_url = "http://www.cc98.org/dispbbs.asp"
board_id = 744
post_id = 4716223
# 用户信息(不同用户此处不同)
cookie = ""
# 头信息,最重要的是Cookie的设置,其代表自己的用户信息,更新该部分即可为另一个用户更新
headers = {
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
"Accept-Encoding": "gzip, deflate",
"Accept-Language": "en,zh-CN;q=0.8,zh;q=0.6,zh-TW;q=0.4,en-GB;q=0.2,ja;q=0.2",
"Cache-Control": "max-age=0",
# "Connection": "keep-alive",
# "Content-Length": "35",
# "Content-Type": "application/x-www-form-urlencoded",
"Cookie": cookie,
"DNT": "1",
"Host": "www.nexushd.org",
"Origin": "http://www.nexushd.org",
"Referer": "http://www.nexushd.org/mybonus.php",
"Upgrade-Insecure-Requests": "1",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) "
"Chrome/56.0.2924.87 Safari/537.36",
}
session = requests.session()
session.headers = headers
def get_current_magic_point():
pa_magic = '\[<a href="mybonus.php">使用</a>\]: ([0-9.,]+)'
# 获取最初魔力值
res = session.get('http://www.nexushd.org/mybonus.php')
text = res.text
magic = re.search(pa_magic, text).group(1)
return magic
def exchange(idx, user, amount, message):
old_magic = get_current_magic_point()
# 发送请求
res = session.post(exchange_url, {
"option": 7,
"username": user,
"bonusgift": amount,
"message": message + "from auto exchange script powered by 风之凌殇",
"submit": "赠送"
})
text = res.text
current_magic = get_current_magic_point()
# print(old_magic, current_magic)
from datetime import datetime
current_time = datetime.now()
if current_magic != old_magic:
print("{idx:4d}: 向{user:^20s}赠送了{amount:^5d}个魔力值,赠送前魔力值为{old:^15s},赠送后剩余魔力值为{remain:^15s},赠送于{now}".format(
idx=idx,
user=str(user),
amount=amount,
old=old_magic,
remain=current_magic,
now=current_time
))
else:
print("{idx:4d}: 向{user:^20s}赠送失败,赠送前魔力值为{old:^15s},赠送后剩余魔力值为{remain:^15s},赠送于{now}".format(
idx=idx,
user=str(user),
amount=amount,
old=old_magic,
remain=current_magic,
now=current_time
))
pass
def batch_exchange(amount=5000, message="test", users_path='users.yaml', get_realtime_users=False):
if get_realtime_users:
users = get_users(dump=False)
else:
# load exchange users from exchange.yaml
import yaml
with open(users_path, encoding='utf-8') as users_f:
users = yaml.load(users_f)
print(users)
for idx, user in enumerate(users):
exchange(idx + 1, user, amount, message)
# time.sleep(1)
pass
def get_pages():
res = requests.get(cc98_url, {
"boardID": board_id,
"ID": post_id,
"page": "",
"replyID": post_id,
"star": 1
})
pa_pages = "本主题贴数 <b>(\d+)</b>"
total = int(re.search(pa_pages, res.text).group(1))
pages = (total + 9) // 10
print('total post count: {post_count}, total pages: {pages}'.format(
post_count=total,
pages=pages
))
return pages
def unique(seq):
seen = set()
seen_add = seen.add
return [x for x in seq if not (x in seen or seen_add(x))]
def get_users(dump=True):
users = []
for page in range(1, get_pages() + 1):
res = requests.get(cc98_url, {
"boardID": board_id,
"ID": post_id,
"page": "",
"replyID": post_id,
"star": page
})
text = res.text
# print(text)
selector = 'td.content-cell-v1 > div'
soup = BeautifulSoup(text, 'lxml')
# 1
for idx, reply in enumerate(soup.select(selector)):
# print(reduce(lambda s,a: s+a,[str(c) for c in reply.children]))
reply = re.sub(r"(xx|xiexie ?)?lz|id(:|:|=)|gxgx|nh(d|g)(id)?(:|:| )|ym|&nbsp;|\[/?.+\]", "",
re.sub(r"thx", "", reply.text, flags=re.IGNORECASE),
flags=re.IGNORECASE)
# print(reply)
user_id = re.search('[a-zA-Z0-9.-]{3,}', reply)
if user_id:
print(10 * (page - 1) + idx + 1, user_id.group())
users.append(user_id.group())
# print('******************我是一条萌萌的分隔线******************')
# print()
users = unique(users)
if dump:
import yaml
with open('users_full.yaml', 'w', encoding='utf-8') as save:
yaml.dump(users, save)
else:
from pprint import pprint
pprint(sorted(users))
print("total count: %d" % len(users))
return users
pass
def main():
global cookie, board_id, post_id
print('------------------------------------------'
'------友情提示:使用前请阅读readme.md------'
'------------------------------------------')
cookie = input('请输入cookie: ')
board_id = input('请输入board_id:')
post_id = input('请输入帖子id:')
message = input('请输入赠送附带的消息内容:')
magic_amount = int(input('请输入每人赠送的魔力值的个数:'))
batch_exchange(magic_amount, message, get_realtime_users=True)
pass
if __name__ == '__main__':
# batch_exchange(2000, '喵喵喵喵喵', get_realtime_users=True)
get_users(dump=False)
# main()
# print(get_pages())
# print(re.search('[a-zA-Z0-9]+', "谢谢楼主bitch007好人一生平安[right][color=gray]From  MI 6 via MyCC98[/color][/right]"))
# print(re.match('a{3,}', 'aaaaaabbb'))
# import yaml
# with open('users.yaml', encoding='utf-8') as users_f:
# users = yaml.load(users_f)
# users = [1,1,1,2,3,4,5]
# print(users)
# print(list(set(users)))
pass
requests
beautifulsoup4
@zjuchenyuan
Copy link

这个正则厉害了

@fzls
Copy link
Author

fzls commented Jun 30, 2017

@zjuchenyuan 主要是回复格式千奇百怪,当时是在浏览器直接console写js用正则来匹配,每翻一页基本就要修改一下正则23333

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment