Skip to content

Commit

Permalink
提交初版本
Browse files Browse the repository at this point in the history
支持以下api
getFavicon api
google api
iowen api
url+/favicon.ico
  • Loading branch information
HowieHz committed Jan 31, 2024
1 parent 0665c51 commit 4468bf0
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 2 deletions.
11 changes: 11 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
.vscode
favicons
links.txt
main.build
main.dist
main.onefile-build
get_favicon.build
get_favicon.dist
get_favicon.onefile-build
get_favicon.exe

# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
Expand Down
33 changes: 31 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,31 @@
# get_favicon
The script is used to automatically get the favicon
# 自动获取网站favicon

The script is used to automatically get the favicon
这个脚本是用于自动获取网站favicon的

建站后我将博客收藏夹转化为[我的博客](https://howiehz.top/links)的单向友链时,需要获取网站的favicon,因为一个个获取太累了(不够优雅),所以我就写了一个脚本来快捷获取

## 使用说明

下载python版本应>3.10
第一次运行脚本`get_favicon.py`会在同目录下生成links.txt,在其中放置你要获取favicon的网站
(或者在[releases](https://github.com/HowieHz/get_favicon/releases)获取最新的二进制文件)
以下形式都是允许的:

```txt
https://howiehz.top
howiehz.top
https://howiehz.top/
howiehz.top/
```

之后继续执行脚本,即可完成favicon下载

## 反馈

如有建议,添加api申请或其他讨论请进入项目[issue](https://github.com/HowieHz/get_favicon/issues)

## 一些可能出现的问题

Q:我的文件无法查看
A:可能是无法从此api中获得该网站的favicon。如果文件大小不是特别小(比如217字节),ico后缀的文件可以尝试改为png后缀查看,png后缀的文件可以尝试改为ico后缀查看。
1 change: 1 addition & 0 deletions build.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
python -m nuitka get_favicon.py --onefile
72 changes: 72 additions & 0 deletions get_favicon.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import requests,os

version = 'v1.0.0'

print(f'自动下载favicon工具 版本{version}')

links_file_path = './links.txt'
if not os.path.exists(links_file_path): # 初始化文件
open(f"{links_file_path}",'w')
input('''
请在该脚本执行目录下的links.txt文件内填入你需要获取favicon的网站的链接,一行一个
例:
a.com (请确保a.com可使用https://a.com访问)
a.com/ (请确保a.com/可使用https://a.com/访问)
http://a.com
https://a.com
http://a.com/
https://a.com/
填写完毕后回车
''')

default_dir_path: str = './favicons/'
raw_dir_path = input(f"输入保存目录,最后一个字符如果不是 / 会自动添加,不输入直接回车即为默认,默认位置为 {default_dir_path} :")
if raw_dir_path:
dir_path: str = f"{raw_dir_path.removesuffix('/')}/"
else:
dir_path: str = default_dir_path

choice_api: str = input('''
使用哪个api
请输入0后回车 使用getFavicon api (请确保可以通畅的连接http://www.getfavicon.org)
请输入1后回车 使用google api(16*16 png)(请确保可以通畅的连接http://www.google.com)
请输入2后回车 直接在链接最后加上/favicon.ico获取
请输入3后回车 使用iowen api (介绍 https://www.iowen.cn/faviconwangzhantubiaozhuaquapijiekou/)
默认为0:''')

if not os.path.exists(dir_path): # 初始化文件夹
os.makedirs(dir_path)

with open('./links.txt') as links_file_stream: #读取links.txt
for link in links_file_stream.readlines():
link: str = link.removesuffix('\n').removesuffix('/') # 去除末尾换行符和可能的/
if not link.startswith('https://') and not link.startswith('http://'):
link = f'https://{link}'

ico_file_name = link.replace('/','_').replace(':','_').replace('___','_') # 处理文件名
# link形式 [http://,https://]aaa.com
print(f'正在下载 {link} 的图标')
match choice_api:
case '1':
ico_file_name: str = f"{ico_file_name}.favicon.png" # 处理文件名
r: requests.Response = requests.get(f'http://www.google.com/s2/favicons?domain={link}')
case '2':
ico_file_name: str = f"{ico_file_name}.favicon.ico" # 处理文件名
r: requests.Response = requests.get(f'{link}/favicon.ico')
case '3':
ico_file_name: str = f"{ico_file_name}.favicon.png" # 处理文件名
link = link.removeprefix('https://').removeprefix('http://')
# link形式 aaa.com
r: requests.Response = requests.get(f'https://api.iowen.cn/favicon/{link}.png')
case _:
ico_file_name: str = f"{ico_file_name}.favicon.ico" # 处理文件名
r: requests.Response = requests.get(f'http://www.getfavicon.org/get.pl?url={link}&submitget=get+favicon')
# f'http://www.getfavicon.org/?url={link}/favicon.png')

# 保存文件
with open(f"{dir_path}{ico_file_name}","wb") as ico_file_stream:
ico_file_stream.write(r.content)
print(f'已保存到 {dir_path}{ico_file_name}')

input('完成图标下载,按回车结束程序 (如有问题,请进入 https://github.com/HowieHz/get_favicon/issues 反馈)')

0 comments on commit 4468bf0

Please sign in to comment.