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