Repository: gwtak/TieBaSign Branch: main Commit: ee6f53e8726d Files: 4 Total size: 9.3 KB Directory structure: gitextract_doatqy8y/ ├── .github/ │ └── workflows/ │ └── main.yml ├── .gitignore ├── README.md └── main.py ================================================ FILE CONTENTS ================================================ ================================================ FILE: .github/workflows/main.yml ================================================ name: 'Baidu Tieba Auto Sign' on: push: branches: - main schedule: - cron: '5 16,22 * * *' jobs: tieba_sign: runs-on: ubuntu-latest steps: - name: 'Checkout codes' uses: actions/checkout@v2 - name: 'Set python' uses: actions/setup-python@v1 with: python-version: '3.6' - name: 'Install dependencies' run: python -m pip install --upgrade requests - name: 'Start Sign' env: BDUSS: ${{ secrets.BDUSS }} HOST: ${{ secrets.HOST }} FROM: ${{ secrets.FROM }} TO: ${{ secrets.TO }} AUTH: ${{ secrets.AUTH }} run: python main.py ================================================ FILE: .gitignore ================================================ # Byte-compiled / optimized / DLL files __pycache__/ *.py[cod] *$py.class # C extensions *.so # Distribution / packaging .Python build/ develop-eggs/ dist/ downloads/ eggs/ .eggs/ lib/ lib64/ parts/ sdist/ var/ wheels/ pip-wheel-metadata/ share/python-wheels/ *.egg-info/ .installed.cfg *.egg MANIFEST # PyInstaller # Usually these files are written by a python script from a template # before PyInstaller builds the exe, so as to inject date/other infos into it. *.manifest *.spec # Installer logs pip-log.txt pip-delete-this-directory.txt # Unit test / coverage reports htmlcov/ .tox/ .nox/ .coverage .coverage.* .cache nosetests.xml coverage.xml *.cover *.py,cover .hypothesis/ .pytest_cache/ # Translations *.mo *.pot # Django stuff: *.log local_settings.py db.sqlite3 db.sqlite3-journal # Flask stuff: instance/ .webassets-cache # Scrapy stuff: .scrapy # Sphinx documentation docs/_build/ # PyBuilder target/ # Jupyter Notebook .ipynb_checkpoints # IPython profile_default/ ipython_config.py # pyenv .python-version # pipenv # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. # However, in case of collaboration, if having platform-specific dependencies or dependencies # having no cross-platform support, pipenv may install dependencies that don't work, or not # install all needed dependencies. #Pipfile.lock # PEP 582; used by e.g. github.com/David-OConnor/pyflow __pypackages__/ # Celery stuff celerybeat-schedule celerybeat.pid # SageMath parsed files *.sage.py # Environments .env .venv env/ venv/ ENV/ env.bak/ venv.bak/ # Spyder project settings .spyderproject .spyproject # Rope project settings .ropeproject # mkdocs documentation /site # mypy .mypy_cache/ .dmypy.json dmypy.json # Pyre type checker .pyre/ ================================================ FILE: README.md ================================================ # 贴吧签到Github Action版 ## 今日签到状态 ![Baidu Tieba Auto Sign](https://github.com/gwtak/TieBaSign/workflows/Baidu%20Tieba%20Auto%20Sign/badge.svg) ## 使用说明 1. Fork 本仓库,然后点击你的仓库右上角的 Settings,找到 Secrets 这一项,添加一个库秘密变量。其中 `BDUSS` 存放你的 BDUSS。支持同时添加多个帐户,BDUSS 之间用 `#` 隔开即可。 2. 设置好环境变量后点击你的仓库上方的 `Actions` 选项,第一次打开需要点击 `I understand...` 按钮,确认在 Fork 的仓库上启用 GitHub Actions 。 3. 任意发起一次commit,可以参考下图流程修改readme文件。 - 打开`README.md`,点击修改按钮 - 修改任意内容,这里在末尾插入了空格。移动到最下面,点击提交。 4. 至此自动签到就搭建完毕了,可以再次点击`Actions`查看工作记录,如果有`Baidu Tieba Auto Sign`则说明workflow创建成功了。点击右侧记录可以查看详细签到情况。 ================================================ FILE: main.py ================================================ # -*- coding:utf-8 -*- import os import requests import hashlib import time import copy import logging import random import smtplib from email.mime.text import MIMEText logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s') logger = logging.getLogger(__name__) # API_URL LIKIE_URL = "http://c.tieba.baidu.com/c/f/forum/like" TBS_URL = "http://tieba.baidu.com/dc/common/tbs" SIGN_URL = "http://c.tieba.baidu.com/c/c/forum/sign" ENV = os.environ HEADERS = { 'Host': 'tieba.baidu.com', 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.71 Safari/537.36', } SIGN_DATA = { '_client_type': '2', '_client_version': '9.7.8.0', '_phone_imei': '000000000000000', 'model': 'MI+5', "net_type": "1", } # VARIABLE NAME COOKIE = "Cookie" BDUSS = "BDUSS" EQUAL = r'=' EMPTY_STR = r'' TBS = 'tbs' PAGE_NO = 'page_no' ONE = '1' TIMESTAMP = "timestamp" DATA = 'data' FID = 'fid' SIGN_KEY = 'tiebaclient!!!' UTF8 = "utf-8" SIGN = "sign" KW = "kw" s = requests.Session() def get_tbs(bduss): logger.info("获取tbs开始") headers = copy.copy(HEADERS) headers.update({COOKIE: EMPTY_STR.join([BDUSS, EQUAL, bduss])}) try: tbs = s.get(url=TBS_URL, headers=headers, timeout=5).json()[TBS] except Exception as e: logger.error("获取tbs出错" + e) logger.info("重新获取tbs开始") tbs = s.get(url=TBS_URL, headers=headers, timeout=5).json()[TBS] logger.info("获取tbs结束") return tbs def get_favorite(bduss): logger.info("获取关注的贴吧开始") # 客户端关注的贴吧 returnData = {} i = 1 data = { 'BDUSS': bduss, '_client_type': '2', '_client_id': 'wappc_1534235498291_488', '_client_version': '9.7.8.0', '_phone_imei': '000000000000000', 'from': '1008621y', 'page_no': '1', 'page_size': '200', 'model': 'MI+5', 'net_type': '1', 'timestamp': str(int(time.time())), 'vcode_tag': '11', } data = encodeData(data) try: res = s.post(url=LIKIE_URL, data=data, timeout=5).json() except Exception as e: logger.error("获取关注的贴吧出错" + e) return [] returnData = res if 'forum_list' not in returnData: returnData['forum_list'] = [] if res['forum_list'] == []: return {'gconforum': [], 'non-gconforum': []} if 'non-gconforum' not in returnData['forum_list']: returnData['forum_list']['non-gconforum'] = [] if 'gconforum' not in returnData['forum_list']: returnData['forum_list']['gconforum'] = [] while 'has_more' in res and res['has_more'] == '1': i = i + 1 data = { 'BDUSS': bduss, '_client_type': '2', '_client_id': 'wappc_1534235498291_488', '_client_version': '9.7.8.0', '_phone_imei': '000000000000000', 'from': '1008621y', 'page_no': str(i), 'page_size': '200', 'model': 'MI+5', 'net_type': '1', 'timestamp': str(int(time.time())), 'vcode_tag': '11', } data = encodeData(data) try: res = s.post(url=LIKIE_URL, data=data, timeout=5).json() except Exception as e: logger.error("获取关注的贴吧出错" + e) continue if 'forum_list' not in res: continue if 'non-gconforum' in res['forum_list']: returnData['forum_list']['non-gconforum'].append(res['forum_list']['non-gconforum']) if 'gconforum' in res['forum_list']: returnData['forum_list']['gconforum'].append(res['forum_list']['gconforum']) t = [] for i in returnData['forum_list']['non-gconforum']: if isinstance(i, list): for j in i: if isinstance(j, list): for k in j: t.append(k) else: t.append(j) else: t.append(i) for i in returnData['forum_list']['gconforum']: if isinstance(i, list): for j in i: if isinstance(j, list): for k in j: t.append(k) else: t.append(j) else: t.append(i) logger.info("获取关注的贴吧结束") return t def encodeData(data): s = EMPTY_STR keys = data.keys() for i in sorted(keys): s += i + EQUAL + str(data[i]) sign = hashlib.md5((s + SIGN_KEY).encode(UTF8)).hexdigest().upper() data.update({SIGN: str(sign)}) return data def client_sign(bduss, tbs, fid, kw): # 客户端签到 logger.info("开始签到贴吧:" + kw) data = copy.copy(SIGN_DATA) data.update({BDUSS: bduss, FID: fid, KW: kw, TBS: tbs, TIMESTAMP: str(int(time.time()))}) data = encodeData(data) res = s.post(url=SIGN_URL, data=data, timeout=5).json() return res def send_email(sign_list): if ('HOST' not in ENV or 'FROM' not in ENV or 'TO' not in ENV or 'AUTH' not in ENV): logger.error("未配置邮箱") return HOST = ENV['HOST'] FROM = ENV['FROM'] TO = ENV['TO'].split('#') AUTH = ENV['AUTH'] length = len(sign_list) subject = f"{time.strftime('%Y-%m-%d', time.localtime())} 签到{length}个贴吧" body = """ """ for i in sign_list: body += f"""
贴吧名称: { i['name'] }
贴吧简介: { i['slogan'] }

""" msg = MIMEText(body, 'html', 'utf-8') msg['subject'] = subject smtp = smtplib.SMTP() smtp.connect(HOST) smtp.login(FROM, AUTH) smtp.sendmail(FROM, TO, msg.as_string()) smtp.quit() def main(): if ('BDUSS' not in ENV): logger.error("未配置BDUSS") return b = ENV['BDUSS'].split('#') for n, i in enumerate(b): logger.info("开始签到第" + str(n) + "个用户" + i) tbs = get_tbs(i) favorites = get_favorite(i) for j in favorites: time.sleep(random.randint(1,5)) client_sign(i, tbs, j["id"], j["name"]) logger.info("完成第" + str(n) + "个用户签到") send_email(favorites) logger.info("所有用户签到结束") if __name__ == '__main__': main()