-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathbing.py
106 lines (87 loc) · 3.81 KB
/
bing.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import asyncio
from demo.code.config import IMAGE_BASE_URL, PROXIES, get_image_path, logger
from PicImageSearch import Bing, Network
from PicImageSearch.model import BingResponse
from PicImageSearch.sync import Bing as BingSync
http2 = True
url = f"{IMAGE_BASE_URL}/test08.jpg"
file = get_image_path("test08.jpg")
@logger.catch()
async def test_async() -> None:
async with Network(proxies=PROXIES, http2=http2) as client:
bing = Bing(client=client)
# resp = await bing.search(url=url)
resp = await bing.search(file=file)
show_result(resp)
@logger.catch()
def test_sync() -> None:
bing = BingSync(proxies=PROXIES, http2=http2)
resp = bing.search(url=url)
# resp = bing.search(file=file)
show_result(resp) # type: ignore
def show_result(resp: BingResponse) -> None:
logger.info(f"Search URL: {resp.url}")
if resp.pages_including:
logger.info("Pages Including:")
for page_item in resp.pages_including:
logger.info(f" Name: {page_item.name}")
logger.info(f" URL: {page_item.url}")
logger.info(f" Thumbnail URL: {page_item.thumbnail}")
logger.info(f" Image URL: {page_item.image_url}")
logger.info("-" * 20)
if resp.visual_search:
logger.info("Visual Search:")
for visual_item in resp.visual_search:
logger.info(f" Name: {visual_item.name}")
logger.info(f" URL: {visual_item.url}")
logger.info(f" Thumbnail URL: {visual_item.thumbnail}")
logger.info(f" Image URL: {visual_item.image_url}")
logger.info("-" * 20)
if resp.related_searches:
logger.info("Related Searches:")
for search_item in resp.related_searches:
logger.info(f" Text: {search_item.text}")
logger.info(f" Thumbnail URL: {search_item.thumbnail}")
logger.info("-" * 20)
if resp.travel:
logger.info("Travel:")
logger.info(f" Destination: {resp.travel.destination_name}")
logger.info(f" Travel Guide URL: {resp.travel.travel_guide_url}")
if resp.travel.attractions:
logger.info(" Attractions:")
for attraction in resp.travel.attractions:
logger.info(f" Title: {attraction.title}")
logger.info(f" URL: {attraction.url}")
logger.info(f" Requery URL: {attraction.search_url}")
logger.info(
f" Interest Types: {', '.join(attraction.interest_types)}"
)
logger.info("-" * 20)
if resp.travel.travel_cards:
logger.info(" Travel Cards:")
for card in resp.travel.travel_cards:
logger.info(f" Card Type: {card.card_type}")
logger.info(f" Title: {card.title}")
logger.info(f" Click URL: {card.url}")
logger.info(f" Image URL: {card.image_url}")
logger.info(f" Image Source URL: {card.image_source_url}")
logger.info("-" * 20)
if resp.entities:
logger.info("Entities:")
for entity in resp.entities:
logger.info(f" Name: {entity.name}")
logger.info(f" Thumbnail URL: {entity.thumbnail}")
logger.info(f" Description: {entity.description}")
logger.info(f" Short Description: {entity.short_description}")
if entity.profiles:
logger.info(" Profiles:")
for profile in entity.profiles:
logger.info(
f" {profile.get('social_network')}: {profile.get('url')}"
)
logger.info("-" * 20)
if resp.best_guess:
logger.info(f"Best Guess: {resp.best_guess}")
if __name__ == "__main__":
asyncio.run(test_async())
# test_sync()