Skip to content

Commit

Permalink
0.6.3
Browse files Browse the repository at this point in the history
  • Loading branch information
kitUIN committed Nov 14, 2020
1 parent df4defd commit 1a51898
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 38 deletions.
66 changes: 40 additions & 26 deletions PicImageSearch/saucenao.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def __init__(self, data):
self.index_id: int = result_header['index_id']
self.index_name: str = result_header['index_name']
self.title: str = self._get_title(result_data)
self.urls: str = self._get_urls(result_data)
self.url: str = self._get_url(result_data)
self.author: str = self._get_author(result_data)
self.pixiv_id: str = self._get_pixiv_id(result_data)
self.member_id: str = self._get_member_id(result_data)
Expand All @@ -34,12 +34,12 @@ def _get_title(data):
return data['created_at']

@staticmethod
def _get_urls(data):
def _get_url(data):
if 'ext_urls' in data:
return data['ext_urls'][0]
elif 'getchu_id' in data:
return f'http://www.getchu.com/soft.phtml?id={data["getchu_id"]}'
return []
return ''

@staticmethod
def _get_author(data):
Expand Down Expand Up @@ -114,41 +114,55 @@ class SauceNAO:
def __init__(self,
api_key: str = None,
*,
numres: int = 5,
hide: int = 1,
minsim: int = 30,
output_type: int = 2,
testmode: int = 0,
numres: int = 10
dbmask: int = None,
dbmaski: int = None,
db: int = 999,
) -> None:
"""
:param api_key:用于SauceNAO的访问密钥
:param output_type: 0=正常(默认) html 1=xml api(未实现) 2=json api
:param testmode: 测试模式 0=正常 1=测试
:param numres: 输出数量 默认10
:param api_key:(str)用于SauceNAO的访问密钥 默认=None
:param output_type:(int) 0=正常(默认) html 1=xml api(未实现) 2=json api 默认=2
:param testmode:(int) 测试模式 0=正常 1=测试 默认=0
:param numres:(int)输出数量 默认=5
:param dbmask:(int)用于选择要启用的特定索引的掩码 默认=None
:param dbmaski:(int)用于选择要禁用的特定索引的掩码 默认=None
:param db:(int)搜索特定的索引号或全部索引 默认=999,索引见https://saucenao.com/tools/examples/api/index_details.txt
:param minsim:(int)控制最小相似度 默认=30
:param hide:(int)结果隐藏控制,无=0,明确返回值(默认)=1,怀疑返回值=2,全部返回值=3
"""
# minsim 控制最小相似度
# todo 完善所有类型params
params = dict()
if api_key is not None:
params['api_key'] = api_key
if dbmask is not None:
params['dbmask'] = dbmask
if dbmaski is not None:
params['dbmaski'] = dbmaski
params['testmode'] = testmode
params['numres'] = numres
params['output_type'] = output_type
params['hide'] = hide
params['db'] = db
params['minsim'] = minsim
params['User-Agent'] = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.183 Safari/537.36 Edg/86.0.622.63'
self.params = params

def search(self, url: str, files=None):
try:
params = self.params
if url[:4] == 'http': # 网络url
params['url'] = url
else: # 文件
image = Image.open(url)
imageData = io.BytesIO()
image.save(imageData, format='PNG')
files = {'file': ("image.png", imageData.getvalue())}
imageData.close()
resp = requests.post(self.SauceNAOURL, params=params, files=files)
status_code = resp.status_code
logger.info(status_code)
data = resp.json()
return SauceNAOResponse(data)
except Exception as e:
logger.error(e)
params = self.params
if url[:4] == 'http': # 网络url
params['url'] = url
else: # 文件
image = Image.open(url)
imageData = io.BytesIO()
image.save(imageData, format='PNG')
files = {'file': ("image.png", imageData.getvalue())}
imageData.close()
resp = requests.post(self.SauceNAOURL, params=params, files=files)
status_code = resp.status_code
logger.info(status_code)
data = resp.json()
return SauceNAOResponse(data)
2 changes: 2 additions & 0 deletions test/test1.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#! /usr/bin/env python3
# coding=utf-8
from PicImageSearch.tracemoe import TraceMoe
tracemoe = TraceMoe()
t = tracemoe.search('https://trace.moe/img/tinted-good.jpg')
Expand Down
7 changes: 5 additions & 2 deletions test/test2.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#! /usr/bin/env python3
# coding=utf-8
from loguru import logger

from PicImageSearch.saucenao import SauceNAO

saucenao = SauceNAO()
res = saucenao.search(r'C:\Users\kuluj\Pictures\[Nidy-2D-]11670173.jpg')
saucenao = SauceNAO(api_key='54a8d90c583d3b66b6dd3d7e9001a39b588cd842',testmode=1)
res = saucenao.search('https://cdn.jsdelivr.net/gh/laosepi/setu/pics_original/77702503_p0.jpg')
logger.info(res.origin) # 原始数据
logger.info(res.raw) #
logger.info(res.raw[0]) #
Expand All @@ -13,5 +15,6 @@
logger.info(res.raw[0].similarity) # 92.22
logger.info(res.raw[0].title) # MDR♡
logger.info(res.raw[0].author) # CeNanGam
logger.info(res.raw[0].url) # https://www.pixiv.net/member_illust.php?mode=medium&illust_id=77702503
logger.info(res.raw[0].pixiv_id) # 77702503
logger.info(res.raw[0].member_id) # 4089680
27 changes: 17 additions & 10 deletions wiki/SauceNAO.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ logger.info(res.raw[0].thumbnail) # https://img1.saucenao.com/res/pixiv/7770/777
logger.info(res.raw[0].similarity)# 92.22
logger.info(res.raw[0].title) # MDR♡
logger.info(res.raw[0].author) # CeNanGam
logger.info(res.raw[0].url) # https://www.pixiv.net/member_illust.php?mode=medium&illust_id=77702503
logger.info(res.raw[0].pixiv_id) # 77702503
logger.info(res.raw[0].member_id) # 4089680
```
Expand All @@ -23,29 +24,35 @@ logger.info(res.raw[0].member_id) # 4089680
### SauceNAO主类说明
```
saucenao = SauceNAO(
api_key='', #用于SauceNAO的访问密钥
output_type = 2, #0=正常(默认) html 1=xml api(未实现) 2=json api
testmode = 1, #测试模式 0=正常 1=测试
numres = 10 #输出数量 默认10
api_key='', # (str)用于SauceNAO的访问密钥 默认=None
numres = 5, # (int)输出数量 默认=5
hide = 1, # (int)结果隐藏控制,无=0,明确返回值(默认)=1,怀疑返回值=2,全部返回值=3
minsim = 30, # (int)控制最小相似度 默认=30
output_type = 2, # (int) 0=正常(默认) html 1=xml api(未实现) 2=json api 默认=2
testmode = 0, # (int)测试模式 0=正常 1=测试 默认=0
dbmask = None, # (int)用于选择要启用的特定索引的掩码 默认=None
dbmaski = None, # (int)用于选择要禁用的特定索引的掩码 默认=None
db = 999, # (int)搜索特定的索引号或全部索引 默认=999
# 索引见https://saucenao.com/tools/examples/api/index_details.txt
)
```
## 数据返回值列表
PS:可以去看看**源代码**
PS:可以去看看[**源代码**](https://github.com/kitUIN/PicImageSearch/blob/main/PicImageSearch/saucenao.py)
以上面的`res`为例
|变量 | 内容 | 类型 |
|---- | ---- | ---- |
|.origin|原始返回值|dict|
|.short_remaining|每30秒访问额度|int|
|.long_remaining |每天访问额度|int|
|.user_id|待补充|int|
|.user_id|API|int|
|.account_type|待补充|int|
|.short_limit|待补充|str|
|.long_limit|待补充|str|
|.status|待补充|int|
|.results_requested|待补充|int|
|.status|服务器判断值|int|
|.results_requested|数据返回值数量|int|
|.search_depth|搜索所涉及的数据库数量|str|
|.minimum_similarity|最小相似度|float|
|.results_returned|待补充|int|
|.results_returned|数据返回值数量|int|
|.raw|结果返回值(具体见下表)|list|
- `res.raw` 存储了所有的返回结果
- 例如`res.raw[0]`内存放了第一条搜索结果
Expand All @@ -59,7 +66,7 @@ PS:可以去看看**源代码**
|.index_id|文件id| int|
|.index_name|文件名称| str |
|.title|标题| str |
|.urls|地址| str |
|.url|地址| str |
|.author|作者| str |
|.pixiv_id|pixiv的id(如果有)|str|
|.member_id|pixiv的画师id(如果有)|str|
Expand Down

0 comments on commit 1a51898

Please sign in to comment.