Skip to content

Commit c9d9908

Browse files
committed
修改部分api避免出现ico后缀的html文件
添加requirements.txt文件
1 parent 6310bcc commit c9d9908

File tree

6 files changed

+31
-8
lines changed

6 files changed

+31
-8
lines changed

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
requests

src/api/append_char_api.py

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,33 @@
88

99
class API(APIBase):
1010
@staticmethod
11-
def get(url: str) -> tuple[Union[str, bytes], str, str]:
11+
def get(url: str, browser_emulation_api_flag:bool = False) -> tuple[Union[str, bytes], str, str]:
1212
"""api规范
1313
1414
Args:
1515
url (str): [http://,https://]aaa.com
16+
browser_emulation_api_flag (bool): 如果是通过模拟浏览器api调用的api,这个flag为True
1617
1718
Returns:
1819
tuple[Union[str, bytes], str, str]: 需要写入文件的值,文件后缀,写入模式('binary' / 'text')
1920
"""
20-
r: requests.Response = requests.get(url=f'{url}/favicon.ico',headers=headers)
21-
return r.content, 'ico', 'binary'
22-
21+
r: requests.Response = requests.get(url=f'{url}/favicon.ico', headers=headers)
22+
content: bytes = r.content
23+
24+
if not r.encoding: # r.encoding是None那多半不是html文件了
25+
return content, 'ico', 'binary'
26+
27+
print(r.encoding)
28+
content_str: str = content.decode(r.encoding)
29+
30+
for line in content_str.splitlines():
31+
if not line: continue # 空行跳过
32+
33+
if line=='<!DOCTYPE html>':
34+
if browser_emulation_api_flag:
35+
return f'该网站无可用favicon\n如您使用实际访问该网站 {url} 有favicon\n请向该项目提出issue: https://github.com/HowieHz/get_favicon/issues/new \n或使用邮箱与作者联系,也可以选择加入讨论群反馈,感谢您的支持', 'txt', 'text'
36+
return f'{url} 无法使用此api获取favicon', 'txt', 'text'
37+
else:
38+
break # 第一个有内容的行不是<!DOCTYPE html>那就不是html文件了
39+
40+
return content, 'ico', 'binary'

src/api/browser_emulation_api.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,6 @@ def get(url: str) -> tuple[Union[str, bytes], str, str]:
1616
"""
1717
ret = html_parser_api.API.get(url)
1818
if ret[1] == 'txt':
19-
ret = append_char_api.API.get(url)
19+
ret = append_char_api.API.get(url=url, browser_emulation_api_flag=True)
2020
return ret
2121

src/api/getfavicon_api.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
from api.api_interface import APIBase
33
from typing import Union
44

5+
headers = {
6+
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:10.0) Gecko/20100101 Firefox/10.0"
7+
}
8+
59
class API(APIBase):
610
@staticmethod
711
def get(url: str) -> tuple[Union[str, bytes], str, str]:
@@ -13,5 +17,5 @@ def get(url: str) -> tuple[Union[str, bytes], str, str]:
1317
Returns:
1418
tuple[Union[str, bytes], str, str]: 需要写入文件的值,文件后缀,写入模式('binary' / 'text')
1519
"""
16-
r: requests.Response = requests.get(f'http://www.getfavicon.org/get.pl?url={url}&submitget=get+favicon') # f'http://www.getfavicon.org/?url={link}/favicon.png')
20+
r: requests.Response = requests.get(f'http://www.getfavicon.org/get.pl?url={url}&submitget=get+favicon', headers=headers) # f'http://www.getfavicon.org/?url={link}/favicon.png')
1721
return r.content, 'ico', 'binary'

src/api/html_parser_api.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ def handle_starttag(self, tag: str, attrs: list[tuple[str, str]]):
8080
3- 只要有type或者href,就不走下面的流程了
8181
4. 有这标签,但是type和href属性都没有,那直接忽略掉了,置空
8282
83-
实验数据:有没有type,type错误,不影响edge正确读取*.png
83+
实验数据:有没有type,type错误,不影响edge正确读取*.png和RFC2397的文件
8484
#TODO 故此提议是否应修改解析流程,先href再type
8585
#TODO 有type,但是是base64的数据呢,直接二进制写入了,不行的
8686

src/main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from typing import Union
33
from api import api_interface, append_char_api, getfavicon_api, google_api, html_parser_api, iowen_api, browser_emulation_api
44

5-
version = 'v1.2.0'
5+
version = 'v1.2.1'
66

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

0 commit comments

Comments
 (0)