|
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, format='jpg', name=None, width=720, height=480, category='', **kwargs): |
|
|
|
|
|
|
66
|
|
|
""" |
|
67
|
|
|
Downloads an image to the specified directory. |
|
68
|
|
|
:param dir: the directory where to be placed the image |
|
69
|
|
|
:param width: the width of the image |
|
70
|
|
|
:param height: the height of the image |
|
71
|
|
|
:param category: string with the category name |
|
72
|
|
|
:param kwargs: |
|
73
|
|
|
:return: |
|
74
|
|
|
""" |
|
75
|
|
|
url = self.image_url(width, height, category, **kwargs) |
|
76
|
|
|
|
|
77
|
|
|
if dir is None: |
|
78
|
|
|
dir = tempfile.gettempdir() |
|
79
|
|
|
|
|
80
|
|
|
if filename is None: |
|
|
|
|
|
|
81
|
|
|
base62_chars = string.ascii_letters + string.digits |
|
82
|
|
|
filename = ''.join(sample(base62_chars, len(base62_chars))) + '.' + format |
|
83
|
|
|
|
|
84
|
|
|
full_path = os.path.join(dir, filename) |
|
85
|
|
|
file = open(full_path, 'wb') |
|
|
|
|
|
|
86
|
|
|
req = urllib.request.Request(url) |
|
|
|
|
|
|
87
|
|
|
|
|
88
|
|
|
def gif(self, dir=None, filename=None): |
|
|
|
|
|
|
89
|
|
|
""" |
|
90
|
|
|
Downloads a gif to the specified directory. |
|
91
|
|
|
:param dir: the directory to store the gif. Default to None means |
|
92
|
|
|
that takes the default OS temporary directory. |
|
93
|
|
|
:param filename: the name to store the gif. Default to None means |
|
94
|
|
|
that takes a random name. |
|
95
|
|
|
:return: string with the full path name |
|
96
|
|
|
""" |
|
97
|
|
|
url = self.gif_url() |
|
98
|
|
|
|
|
99
|
|
|
if dir is None: |
|
100
|
|
|
dir = tempfile.gettempdir() |
|
101
|
|
|
|
|
102
|
|
|
if filename is None: |
|
103
|
|
|
base62_chars = string.ascii_letters + string.digits |
|
104
|
|
|
filename = ''.join(sample(base62_chars, len(base62_chars))) + '.gif' |
|
105
|
|
|
|
|
106
|
|
|
full_path = os.path.join(dir, filename) |
|
107
|
|
|
file = open(full_path, 'wb') |
|
108
|
|
|
|
|
109
|
|
|
req = urllib.request.Request(url) |
|
110
|
|
|
req_handler = urllib.request.urlopen(req) |
|
111
|
|
|
file.write(req_handler.read()) |
|
112
|
|
|
file.close() |
|
113
|
|
|
|
|
114
|
|
|
return full_path |
|
115
|
|
|
|
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.