Skip to content

Commit 918de8a

Browse files
committed
refactor: log
1 parent fc10c6f commit 918de8a

File tree

4 files changed

+65
-58
lines changed

4 files changed

+65
-58
lines changed

README.md

Lines changed: 44 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,36 @@ pip install bing-images
2727

2828
### Fetch Image Urls
2929

30+
fetch_image_urls.py
31+
3032
```py
3133
from bing_images import bing
3234

33-
urls = bing.fetch_image_urls("cat", limit=100, file_type='png', filters='+filterui:aspect-square+filterui:color2-bw')
35+
urls = bing.fetch_image_urls("cat", limit=10, file_type='png', filters='+filterui:aspect-square+filterui:color2-bw')
3436
print("{} images.".format(len(urls)))
3537
counter = 1
3638
for url in urls:
3739
print("{}: {}".format(counter, url))
3840
counter += 1
3941
```
4042

43+
Run
44+
45+
```
46+
❯ python fetch_image_urls.py
47+
10 images.
48+
1: http://pngimg.com/uploads/cat/cat_PNG50521.png
49+
2: http://pngimg.com/uploads/cat/cat_PNG1616.png
50+
3: https://pngimg.com/uploads/cat/cat_PNG50532.png
51+
4: https://pngimg.com/uploads/cat/cat_PNG1621.png
52+
5: https://pngimg.com/uploads/cat/cat_PNG1618.png
53+
6: http://pngimg.com/uploads/cat/cat_PNG1624.png
54+
7: http://www.pngmart.com/files/5/Black-Cat-PNG-Transparent.png
55+
8: http://www.myiconfinder.com/uploads/iconsets/256-256-a96249f4c8a9753fd904f8be023dc25c-cat.png
56+
9: https://pngimg.com/uploads/cat/cat_PNG1619.png
57+
10: http://pngimg.com/uploads/cat/cat_PNG50521.png
58+
```
59+
4160
> **filters**: You can visit <https://cn.bing.com/images/search?q=cat> to operate filters through the UI, and then observe the link changes.
4261
4362
### Download using multithreading
@@ -63,48 +82,31 @@ Run
6382
```shell
6483
❯ python download.py
6584
Save path: /Users/catchzeng/Desktop/cat
66-
Downloading image #1 from http://pngimg.com/uploads/cat/cat_PNG100.png
67-
Downloading image #2 from http://www.freepngimg.com/download/cat/22193-3-adorable-cat.png
68-
Downloading image #3 from http://pngimg.com/uploads/cat/cat_PNG106.png
69-
Downloading image #5 from https://pngimg.com/uploads/cat/cat_PNG50433.png
70-
Downloading image #6 from https://pngimg.com/uploads/cat/cat_PNG50434.png
71-
Downloading image #4 from http://pngimg.com/uploads/cat/cat_PNG50509.png
72-
Downloading image #8 from http://pngimg.com/uploads/cat/cat_PNG50480.png
73-
Downloading image #9 from https://pngimg.com/uploads/cat/cat_PNG50447.png
74-
Downloading image #10 from http://pngimg.com/uploads/cat/cat_PNG50529.png
75-
Downloading image #7 from http://pngimg.com/uploads/cat/cat_PNG119.png
76-
Downloading image #11 from https://pngimg.com/uploads/cat/cat_PNG50438.png
77-
Image #4 Downloaded
78-
Downloading image #12 from http://pngimg.com/uploads/cat/cat_PNG50498.png
79-
Image #3 Downloaded
80-
Downloading image #13 from https://pngimg.com/uploads/cat/cat_PNG50465.png
81-
Image #12 Downloaded
82-
Downloading image #14 from https://pngimg.com/uploads/cat/cat_PNG50417.png
83-
Image #2 Downloaded
84-
Downloading image #15 from https://pngimg.com/uploads/cat/cat_PNG50440.png
85-
Image #6 Downloaded
86-
Downloading image #16 from http://pngimg.com/uploads/cat/cat_PNG113.png
87-
Image #8 Downloaded
88-
Downloading image #17 from http://pngimg.com/uploads/cat/cat_PNG50442.png
89-
Image #1 Downloaded
90-
Downloading image #18 from https://purepng.com/public/uploads/large/purepng.com-catanimalscat-981524673949tj5ns.png
91-
Image #7 Downloaded
92-
Downloading image #19 from https://pngimg.com/uploads/cat/cat_PNG50435.png
93-
Image #9 Downloaded
94-
Downloading image #20 from https://cdn.pixabay.com/photo/2017/02/22/16/55/cat-2089916_960_720.png
95-
Image #15 Downloaded
96-
Image #20 Downloaded
97-
Image #14 Downloaded
98-
Image #17 Downloaded
99-
Image #16 Downloaded
100-
Image #18 Downloaded
101-
Image #11 Downloaded
102-
Image #13 Downloaded
103-
Image #19 Downloaded
104-
Image #5 Downloaded
105-
Image #10 Downloaded
85+
Downloading images
86+
#1 http://pngimg.com/uploads/cat/cat_PNG50509.png Downloaded
87+
#2 https://pngimg.com/uploads/cat/cat_PNG50498.png Downloaded
88+
#3 http://www.freepngimg.com/download/cat/22193-3-adorable-cat.png Downloaded
89+
#4 http://pngimg.com/uploads/cat/cat_PNG106.png Downloaded
90+
#5 https://pngimg.com/uploads/cat/cat_PNG50465.png Downloaded
91+
#6 https://pngimg.com/uploads/cat/cat_PNG50417.png Downloaded
92+
#7 https://pngimg.com/uploads/cat/cat_PNG50480.png Downloaded
93+
#8 http://pngimg.com/uploads/cat/cat_PNG119.png Downloaded
94+
#9 https://pngimg.com/uploads/cat/cat_PNG50438.png Downloaded
95+
#10 http://pngimg.com/uploads/cat/cat_PNG100.png Downloaded
96+
#11 https://pngimg.com/uploads/cat/cat_PNG50447.png Downloaded
97+
#12 https://pngimg.com/uploads/cat/cat_PNG50440.png Downloaded
98+
#13 https://pngimg.com/uploads/cat/cat_PNG50433.png Downloaded
99+
#14 https://www.pngarts.com/files/1/Baby-Cat-PNG-Free-Download.png Downloaded
100+
#15 https://cdn.pixabay.com/photo/2017/02/22/16/55/cat-2089916_960_720.png Downloaded
101+
#16 https://pngimg.com/uploads/cat/cat_PNG50434.png Downloaded
102+
#17 http://pngimg.com/uploads/cat/cat_PNG50529.png Downloaded
103+
#18 http://pngimg.com/uploads/cat/cat_PNG113.png Downloaded
104+
#19 https://purepng.com/public/uploads/large/purepng.com-catanimalscat-981524673949tj5ns.png Downloaded
105+
#20 https://pngimg.com/uploads/cat/cat_PNG50435.png Downloaded
106+
Renaming images
107+
Finished renaming
106108
Done
107-
Elapsed Time: 29.91s
109+
Elapsed time: 20.76s
108110
```
109111

110112
![](./images/cat.jpg)

bing_images/bing.py

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
try:
2-
from util import file_name, rename, make_image_dir, download_image
2+
from util import get_file_name, rename, make_image_dir, download_image
33
except ImportError: # Python 3
4-
from .util import file_name, rename, make_image_dir, download_image
4+
from .util import get_file_name, rename, make_image_dir, download_image
55
from typing import Counter, List
66
from multiprocessing.pool import ThreadPool
77
from time import time as timer
@@ -89,6 +89,7 @@ def download_images(
8989
urls = fetch_image_urls(query, limit, adult, file_type, filters)
9090
entries = get_image_entries(urls, image_dir)
9191

92+
print("Downloading images")
9293
ps = pool_size
9394
if limit < pool_size:
9495
ps = limit
@@ -98,11 +99,13 @@ def download_images(
9899

99100
print("Done")
100101
elapsed = timer() - start
101-
print("Elapsed Time: %.2fs" % elapsed)
102+
print("Elapsed time: %.2fs" % elapsed)
103+
102104

103105
def rename_images(dir, prefix):
104106
files = os.listdir(dir)
105107
index = 1
108+
print("Renaming images")
106109
for f in files:
107110
if f.startswith("."):
108111
print("Escape {}".format(f))
@@ -111,33 +114,35 @@ def rename_images(dir, prefix):
111114
name = rename(f, index, prefix)
112115
dst = os.path.join(dir, name)
113116
os.rename(src, dst)
114-
print("Rename {} to {}".format(src, dst))
115117
index = index + 1
118+
print("Finished renaming")
119+
116120

117121
def download_image_entries(entries, pool_size):
118-
counter = 0
122+
counter = 1
119123
results = ThreadPool(pool_size).imap_unordered(
120124
download_image_with_thread, entries)
121-
for (url, result, path) in results:
125+
for (url, result) in results:
122126
if result:
123-
print("#{} {} Downloaded {}".format(counter, url, path))
127+
print("#{} {} Downloaded".format(counter, url))
124128
counter = counter + 1
125129

130+
126131
def get_image_entries(urls, dir):
127132
entries = []
128133
i = 0
129134
for url in urls:
130-
name = file_name(url, i, "#tmp#")
135+
name = get_file_name(url, i, "#tmp#")
131136
path = os.path.join(dir, name)
132137
entries.append((url, path))
133138
i = i + 1
134139
return entries
135140

141+
136142
def download_image_with_thread(entry):
137143
url, path = entry
138-
print("Downloading image from {}".format(url))
139144
result = download_image(url, path)
140-
return (url, result, path)
145+
return (url, result)
141146

142147

143148
if __name__ == '__main__':

bing_images/util.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,21 @@ def download_image(url, path) -> bool:
1515
shutil.copyfileobj(r.raw, f)
1616
return True
1717
else:
18-
print("[!] download image: {}\n[!] Err :: {}".format(url, r.content))
18+
print("[!] Download image: {}\n[!] Err :: {}".format(url, r.status_code))
1919
return False
2020
except Exception as e:
21-
print("[!] download image: {}\n[!] Err :: {}".format(url, e))
21+
print("[!] Download image: {}\n[!] Err :: {}".format(url, e))
2222
return False
2323

24-
def file_name(url, index, prefix='image') -> str:
24+
def get_file_name(url, index, prefix='image') -> str:
2525
try:
2626
path = urllib.parse.urlsplit(url).path
2727
filename = posixpath.basename(path).split('?')[0]
2828
type, _ = file_data(filename)
2929
result = "{}_{}.{}".format(prefix, index, type)
3030
return result
3131
except Exception as e:
32-
print("[!] Issue getting: {}\n[!] Err :: {}".format(url, e))
32+
print("[!] Get file name: {}\n[!] Err :: {}".format(url, e))
3333
return prefix
3434

3535
def rename(name, index, prefix='image') -> str:
@@ -38,7 +38,7 @@ def rename(name, index, prefix='image') -> str:
3838
result = "{}_{}.{}".format(prefix, index, type)
3939
return result
4040
except Exception as e:
41-
print("[!] Issue getting: {}\n[!] Err :: {}".format(name, e))
41+
print("[!] Rename: {}\n[!] Err :: {}".format(name, e))
4242
return prefix
4343

4444
def file_data(name):

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[metadata]
22
name = bing_images
3-
version = 0.0.4
3+
version = 0.0.5
44
author = CatchZeng
55
author_email = catchzenghh@gmail.com
66
description = Python library to fetch image urls and download using multithreading from Bing.com.

0 commit comments

Comments
 (0)