Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
guodongdonga committed Jul 4, 2024
1 parent c91a126 commit 3b98da9
Show file tree
Hide file tree
Showing 3 changed files with 151 additions and 0 deletions.
42 changes: 42 additions & 0 deletions .github/workflows/convert_and_push.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
name: Scheduled Update

on:
schedule:
- cron: '0 0 * * *' # 每天UTC时间0点运行

jobs:
update:
runs-on: ubuntu-latest
steps:

- name: Checkout repo
uses: actions/checkout@v4

- name: Checkout upstream repository
uses: actions/checkout@v4
with:
repository: privacy-protection-tools/anti-AD
ref: master # 或者指定其他分支
path: code # 指定克隆到的工作目录路径

- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.11'

- name: Install dependencies
run: pip install requests

- name: Run conversion script
run: python ./convert_ros_dns_adlist.py

- name: Commit and push changes
run: |
git config --local user.email "action@github.com"
git config --local user.name "GitHub Action"
git add anti-ad-for-ros.conf
git commit -m "Scheduled update" || exit 0
git push
env:
TOKEN: ${{ secrets.GITHUB_TOKEN }}
working-directory: '.'
43 changes: 43 additions & 0 deletions .github/workflows/manual.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
name: OnPush Update

on:
push:
branches:
- main

jobs:
update:
runs-on: ubuntu-latest
steps:

- name: Checkout repo
uses: actions/checkout@v4

- name: Checkout upstream repository
uses: actions/checkout@v4
with:
repository: privacy-protection-tools/anti-AD
ref: master # 或者指定其他分支
path: code # 指定克隆到的工作目录路径

- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.11'

- name: Install dependencies
run: pip install requests

- name: Run conversion script
run: python ./convert_ros_dns_adlist.py

- name: Commit and push changes
run: |
git config --local user.email "action@github.com"
git config --local user.name "GitHub Action"
git add anti-ad-for-ros.conf
git commit -m "Scheduled update" || exit 0
git push
env:
TOKEN: ${{ secrets.GITHUB_TOKEN }}
working-directory: '.'
66 changes: 66 additions & 0 deletions convert_ros_dns_adlist.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# @bilibili:果冻冻啊
# https://space.bilibili.com/7974766
# 从anti-ad获取过滤列表,转换为ros可用的adlist
import os
import requests


def down_load_from_anti_ad():
# 从https://github.com/privacy-protection-tools/anti-AD 获取最新的广告过滤列表
# 我们可以从https://anti-ad.net/domains.txt这里下载

# 设置要下载的文件的URL
url = 'https://anti-ad.net/domains.txt'

# 发送HTTP请求获取文件内容
response = requests.get(url)

# 检查请求是否成功
if response.status_code == 200:
# 文件名用url里面的
filename = url.split('/')[-1]
# 保存文件到当前目录
with open(filename, 'wb') as file:
file.write(response.content)
print(f'文件已下载并保存为 {filename}')
else:
print('文件下载失败,状态码:', response.status_code)


def convert():
# 检查当前目录是否有domains.txt
if os.path.exists('/home/runner/work/anti-ad-ros/anti-ad-ros/code/anti-ad-domains.txt'):
# 执行转换
# 打开文件进行读取
with open('/home/runner/work/anti-ad-ros/anti-ad-ros/code/anti-ad-domains.txt', 'r', encoding='utf-8') as file:
lines = file.readlines()

# 打开文件进行写入,创建新文件anti-ad-ros.conf
with open('anti-ad-for-ros.conf', 'w', encoding='utf-8') as file:
# 写入一行注释说明
# file.write('# ROS dns adlist, converted from anti-ad.net\n')
# 遍历每一行,跳过注释行,并在非注释行前加上0.0.0.0
for line in lines:
if not line.startswith('#'):
# 删除行首尾的空白字符(如果有的话)
line = line.strip()
# 跳过空行
if line:
# 在行前加上0.0.0.0 和一个空格
file.write('0.0.0.0 {}\n'.format(line))

print("文件转换完成。")
else:
print('没有找到anti-ad-domains.txt,可能clone失败了')
# print当前目录内容
print(os.listdir())
# print /home/runner/work/anti-ad-ros/anti-ad-ros/目录的内容
print(os.listdir('/home/runner/work/anti-ad-ros/anti-ad-ros/'))


if __name__ == '__main__':
# 1. 下载文件
# down_load_from_anti_ad()
# 2. 格式转换
convert()
# 3. 导入ros即可使用

0 comments on commit 3b98da9

Please sign in to comment.