Merge pull request #103 from rootmelo92118/patch-1

Create twnic_rpz.py
This commit is contained in:
踢低吸 2023-07-29 23:57:37 +08:00 committed by GitHub
commit 0201697075
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 59 additions and 1 deletions

26
.github/workflows/twnic_rpz.yml vendored Normal file
View File

@ -0,0 +1,26 @@
name: TWNIC RPZ
on:
schedule:
- cron: '0 0 * * *'
workflow_dispatch:
jobs:
update_twnic_rpz_data:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
with:
python-version: '3.9'
cache: 'pip' # caching pip dependencies
- run: pip install -r AutoBuild/requirements.txt && python AutoBuild/twnic_rpz.py
- name: push
uses: github-actions-x/commit@v2.9
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
push-branch: 'master'
commit-message: '🤖 自動更新 TWNIC RPZ 封鎖域名'
files: TWNIC-RPZ.txt
name: tdc
email: tdc@sudo.host

View File

@ -2,4 +2,4 @@ certifi==2023.7.22
charset-normalizer==2.1.1
idna==3.3
requests==2.28.1
urllib3==1.26.12
urllib3==1.26.12

32
AutoBuild/twnic_rpz.py Normal file
View File

@ -0,0 +1,32 @@
import requests
import json
import logging
from json.decoder import JSONDecodeError
import sys
from typing import List
logger = logging.getLogger(__name__)
def main():
r = requests.get('https://rpz.twnic.tw/e.html')
if r.status_code != 200:
logger.critical('Fetch Data Err')
sys.exit(1)
# split text from <script> tag
raw: str = r.text.split('<script>')[1].split(';')[0].split('= ')[1]
parse_data: List[dict] = [dict()]
try:
parse_data = json.loads(raw)
except JSONDecodeError:
logger.critical('Parse JSON Err')
sys.exit(1)
output = [domain for in_dic in parse_data for domain in in_dic['domains']]
with open('TWNIC-RPZ.txt', 'w') as f:
f.write(''.join(f'||{e}^\n' for e in output))
if __name__ == '__main__':
main()