1
|
|
|
from faker.providers import BaseProvider |
|
|
|
|
2
|
|
|
from random import randint, sample |
|
|
|
|
3
|
|
|
import urllib.request |
|
|
|
|
4
|
|
|
import urllib.parse |
|
|
|
|
5
|
|
|
import tempfile |
|
|
|
|
6
|
|
|
import os |
|
|
|
|
7
|
|
|
import string |
|
|
|
|
8
|
|
|
|
9
|
|
|
|
10
|
|
|
class RandImgProvider(BaseProvider): |
|
|
|
|
11
|
|
|
""" |
12
|
|
|
The class RandImgProvider contains all the logic for generate random |
13
|
|
|
image urls and also for download it. |
14
|
|
|
""" |
15
|
|
|
base_url = 'http://www.rand-img.com' |
16
|
|
|
|
17
|
|
|
def image_url(self, width=720, height=480, category='', **kwargs): |
18
|
|
|
""" |
19
|
|
|
Generate a random image url. |
20
|
|
|
:param width: int |
21
|
|
|
:param height: int |
22
|
|
|
:param category: string |
23
|
|
|
:param kwargs: Optional list of parameters for the image. |
24
|
|
|
:return: a string containing the full url |
25
|
|
|
""" |
26
|
|
|
started_query = False |
|
|
|
|
27
|
|
|
url = self.base_url |
28
|
|
|
url += "/%d/%d" % (width, height, ) |
29
|
|
|
|
30
|
|
|
if len(category) > 0: |
|
|
|
|
31
|
|
|
url += "/%s" % category |
32
|
|
|
|
33
|
|
|
if len(kwargs) > 0: |
|
|
|
|
34
|
|
|
if kwargs.get('rand', False): |
35
|
|
|
kwargs['rand'] = randint(1, 1000000) |
36
|
|
|
return url + '?' + urllib.parse.urlencode(kwargs) |
37
|
|
|
|
38
|
|
|
return url |
39
|
|
|
|
40
|
|
|
def squared_image_url(self, width=720, category='', **kwargs): |
41
|
|
|
""" |
42
|
|
|
Helper method that generate a squared image url. |
43
|
|
|
:param width: the width (and height) of the image |
44
|
|
|
:param category: string |
45
|
|
|
:param kwargs: Optional list of parameters for the image. |
46
|
|
|
:return: a string containing the full url |
47
|
|
|
""" |
48
|
|
|
return self.image_url(width, width, category, **kwargs) |
49
|
|
|
|
50
|
|
|
def gif_url(self, rand=False): |
51
|
|
|
""" |
52
|
|
|
Generate a random gif url. It can attach |
53
|
|
|
a random number to avoid that multiple gifs loaded |
54
|
|
|
in the page will be all the same gif. |
55
|
|
|
|
56
|
|
|
:param rand: bool if True then a random number will be attached to the URL. |
57
|
|
|
:return: |
58
|
|
|
""" |
59
|
|
|
url = '%s/gif' % self.base_url |
60
|
|
|
if rand: |
61
|
|
|
url += '?rand=%d' % randint(1, 1000000) |
62
|
|
|
|
63
|
|
|
return url |
64
|
|
|
|
65
|
|
|
def image(self, dir=None, filename=None, width=720, height=480, category='', **kwargs): |
|
|
|
|
66
|
|
|
""" |
67
|
|
|
Downloads an image to the specified directory. |
68
|
|
|
:param filename: the file name with the extension ('example.png'). If None |
69
|
|
|
random name will be generated |
70
|
|
|
:param dir: the directory where to be placed the image |
71
|
|
|
:param width: the width of the image |
72
|
|
|
:param height: the height of the image |
73
|
|
|
:param category: string with the category name |
74
|
|
|
:param kwargs: |
75
|
|
|
:return: |
76
|
|
|
""" |
77
|
|
|
url = self.image_url(width, height, category, **kwargs) |
78
|
|
|
|
79
|
|
|
if dir is None: |
80
|
|
|
dir = tempfile.gettempdir() |
81
|
|
|
|
82
|
|
|
if filename is None: |
83
|
|
|
base62_chars = string.ascii_letters + string.digits |
84
|
|
|
filename = ''.join(sample(base62_chars, len(base62_chars))) + '.jpg' |
85
|
|
|
|
86
|
|
|
full_path = os.path.join(dir, filename) |
87
|
|
|
file = open(full_path, 'wb') |
88
|
|
|
req = urllib.request.Request(url) |
89
|
|
|
req_handler = urllib.request.urlopen(req) |
90
|
|
|
file.write(req_handler.read()) |
91
|
|
|
file.close() |
92
|
|
|
|
93
|
|
|
return full_path |
94
|
|
|
|
95
|
|
|
def gif(self, dir=None, filename=None): |
|
|
|
|
96
|
|
|
""" |
97
|
|
|
Downloads a gif to the specified directory. |
98
|
|
|
:param dir: the directory to store the gif. Default to None means |
99
|
|
|
that takes the default OS temporary directory. |
100
|
|
|
:param filename: the name to store the gif. Default to None means |
101
|
|
|
that takes a random name. |
102
|
|
|
:return: string with the full path name |
103
|
|
|
""" |
104
|
|
|
url = self.gif_url() |
105
|
|
|
|
106
|
|
|
if dir is None: |
107
|
|
|
dir = tempfile.gettempdir() |
108
|
|
|
|
109
|
|
|
if filename is None: |
110
|
|
|
base62_chars = string.ascii_letters + string.digits |
111
|
|
|
filename = ''.join(sample(base62_chars, len(base62_chars))) + '.gif' |
112
|
|
|
|
113
|
|
|
full_path = os.path.join(dir, filename) |
114
|
|
|
file = open(full_path, 'wb') |
115
|
|
|
|
116
|
|
|
req = urllib.request.Request(url) |
117
|
|
|
req_handler = urllib.request.urlopen(req) |
118
|
|
|
file.write(req_handler.read()) |
119
|
|
|
file.close() |
120
|
|
|
|
121
|
|
|
return full_path |
122
|
|
|
|
The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:
If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.