Skip to content

Commit 4468bf0

Browse files
committed
提交初版本
支持以下api getFavicon api google api iowen api url+/favicon.ico
1 parent 0665c51 commit 4468bf0

File tree

4 files changed

+115
-2
lines changed

4 files changed

+115
-2
lines changed

.gitignore

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
.vscode
2+
favicons
3+
links.txt
4+
main.build
5+
main.dist
6+
main.onefile-build
7+
get_favicon.build
8+
get_favicon.dist
9+
get_favicon.onefile-build
10+
get_favicon.exe
11+
112
# Byte-compiled / optimized / DLL files
213
__pycache__/
314
*.py[cod]

README.md

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,31 @@
1-
# get_favicon
2-
The script is used to automatically get the favicon
1+
# 自动获取网站favicon
2+
3+
The script is used to automatically get the favicon
4+
这个脚本是用于自动获取网站favicon的
5+
6+
建站后我将博客收藏夹转化为[我的博客](https://howiehz.top/links)的单向友链时,需要获取网站的favicon,因为一个个获取太累了(不够优雅),所以我就写了一个脚本来快捷获取
7+
8+
## 使用说明
9+
10+
下载python版本应>3.10
11+
第一次运行脚本`get_favicon.py`会在同目录下生成links.txt,在其中放置你要获取favicon的网站
12+
(或者在[releases](https://github.com/HowieHz/get_favicon/releases)获取最新的二进制文件)
13+
以下形式都是允许的:
14+
15+
```txt
16+
https://howiehz.top
17+
howiehz.top
18+
https://howiehz.top/
19+
howiehz.top/
20+
```
21+
22+
之后继续执行脚本,即可完成favicon下载
23+
24+
## 反馈
25+
26+
如有建议,添加api申请或其他讨论请进入项目[issue](https://github.com/HowieHz/get_favicon/issues)
27+
28+
## 一些可能出现的问题
29+
30+
Q:我的文件无法查看
31+
A:可能是无法从此api中获得该网站的favicon。如果文件大小不是特别小(比如217字节),ico后缀的文件可以尝试改为png后缀查看,png后缀的文件可以尝试改为ico后缀查看。

build.bat

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
python -m nuitka get_favicon.py --onefile

get_favicon.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import requests,os
2+
3+
version = 'v1.0.0'
4+
5+
print(f'自动下载favicon工具 版本{version}')
6+
7+
links_file_path = './links.txt'
8+
if not os.path.exists(links_file_path): # 初始化文件
9+
open(f"{links_file_path}",'w')
10+
input('''
11+
请在该脚本执行目录下的links.txt文件内填入你需要获取favicon的网站的链接,一行一个
12+
例:
13+
a.com (请确保a.com可使用https://a.com访问)
14+
a.com/ (请确保a.com/可使用https://a.com/访问)
15+
http://a.com
16+
https://a.com
17+
http://a.com/
18+
https://a.com/
19+
20+
填写完毕后回车
21+
''')
22+
23+
default_dir_path: str = './favicons/'
24+
raw_dir_path = input(f"输入保存目录,最后一个字符如果不是 / 会自动添加,不输入直接回车即为默认,默认位置为 {default_dir_path} :")
25+
if raw_dir_path:
26+
dir_path: str = f"{raw_dir_path.removesuffix('/')}/"
27+
else:
28+
dir_path: str = default_dir_path
29+
30+
choice_api: str = input('''
31+
使用哪个api
32+
请输入0后回车 使用getFavicon api (请确保可以通畅的连接http://www.getfavicon.org)
33+
请输入1后回车 使用google api(16*16 png)(请确保可以通畅的连接http://www.google.com)
34+
请输入2后回车 直接在链接最后加上/favicon.ico获取
35+
请输入3后回车 使用iowen api (介绍 https://www.iowen.cn/faviconwangzhantubiaozhuaquapijiekou/)
36+
默认为0:''')
37+
38+
if not os.path.exists(dir_path): # 初始化文件夹
39+
os.makedirs(dir_path)
40+
41+
with open('./links.txt') as links_file_stream: #读取links.txt
42+
for link in links_file_stream.readlines():
43+
link: str = link.removesuffix('\n').removesuffix('/') # 去除末尾换行符和可能的/
44+
if not link.startswith('https://') and not link.startswith('http://'):
45+
link = f'https://{link}'
46+
47+
ico_file_name = link.replace('/','_').replace(':','_').replace('___','_') # 处理文件名
48+
# link形式 [http://,https://]aaa.com
49+
print(f'正在下载 {link} 的图标')
50+
match choice_api:
51+
case '1':
52+
ico_file_name: str = f"{ico_file_name}.favicon.png" # 处理文件名
53+
r: requests.Response = requests.get(f'http://www.google.com/s2/favicons?domain={link}')
54+
case '2':
55+
ico_file_name: str = f"{ico_file_name}.favicon.ico" # 处理文件名
56+
r: requests.Response = requests.get(f'{link}/favicon.ico')
57+
case '3':
58+
ico_file_name: str = f"{ico_file_name}.favicon.png" # 处理文件名
59+
link = link.removeprefix('https://').removeprefix('http://')
60+
# link形式 aaa.com
61+
r: requests.Response = requests.get(f'https://api.iowen.cn/favicon/{link}.png')
62+
case _:
63+
ico_file_name: str = f"{ico_file_name}.favicon.ico" # 处理文件名
64+
r: requests.Response = requests.get(f'http://www.getfavicon.org/get.pl?url={link}&submitget=get+favicon')
65+
# f'http://www.getfavicon.org/?url={link}/favicon.png')
66+
67+
# 保存文件
68+
with open(f"{dir_path}{ico_file_name}","wb") as ico_file_stream:
69+
ico_file_stream.write(r.content)
70+
print(f'已保存到 {dir_path}{ico_file_name}')
71+
72+
input('完成图标下载,按回车结束程序 (如有问题,请进入 https://github.com/HowieHz/get_favicon/issues 反馈)')

0 commit comments

Comments
 (0)