RandImgProvider.squared_image_url()   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 9
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 9
rs 10
c 0
b 0
f 0
cc 1
nop 4
1
from faker.providers import BaseProvider
0 ignored issues
show
Coding Style introduced by
This module should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
2
from random import randint, sample
0 ignored issues
show
introduced by
standard import "from random import randint, sample" should be placed before "from faker.providers import BaseProvider"
Loading history...
3
import urllib.request
0 ignored issues
show
introduced by
standard import "import urllib.request" should be placed before "from faker.providers import BaseProvider"
Loading history...
4
import urllib.parse
0 ignored issues
show
introduced by
standard import "import urllib.parse" should be placed before "from faker.providers import BaseProvider"
Loading history...
5
import tempfile
0 ignored issues
show
introduced by
standard import "import tempfile" should be placed before "from faker.providers import BaseProvider"
Loading history...
6
import os
0 ignored issues
show
introduced by
standard import "import os" should be placed before "from faker.providers import BaseProvider"
Loading history...
7
import string
0 ignored issues
show
introduced by
standard import "import string" should be placed before "from faker.providers import BaseProvider"
Loading history...
8
9
10
class RandImgProvider(BaseProvider):
0 ignored issues
show
Unused Code introduced by
The variable __class__ seems to be unused.
Loading history...
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
0 ignored issues
show
Unused Code introduced by
The variable started_query seems to be unused.
Loading history...
27
        url = self.base_url
28
        url += "/%d/%d" % (width, height, )
29
30
        if len(category) > 0:
0 ignored issues
show
Unused Code introduced by
Do not use len(SEQUENCE) as condition value
Loading history...
31
            url += "/%s" % category
32
33
        if len(kwargs) > 0:
0 ignored issues
show
Unused Code introduced by
Do not use len(SEQUENCE) as condition value
Loading history...
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):
0 ignored issues
show
Bug Best Practice introduced by
This seems to re-define the built-in dir.

It is generally discouraged to redefine built-ins as this makes code very hard to read.

Loading history...
best-practice introduced by
Too many arguments (6/5)
Loading history...
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):
0 ignored issues
show
Bug Best Practice introduced by
This seems to re-define the built-in dir.

It is generally discouraged to redefine built-ins as this makes code very hard to read.

Loading history...
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