Passed
Push — master ( 2e889c...c9e1e3 )
by Siro Díaz
01:19
created

RandImgProvider.image_url()   A

Complexity

Conditions 4

Size

Total Lines 22
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 22
rs 9.85
c 0
b 0
f 0
cc 4
nop 5
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, format='jpg', name=None, width=720, height=480, category='', **kwargs):
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (101/100).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
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...
Bug Best Practice introduced by
This seems to re-define the built-in format.

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 (7/5)
Loading history...
Unused Code introduced by
The argument name seems to be unused.
Loading history...
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:
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable filename does not seem to be defined.
Loading history...
Bug introduced by
The variable filename was used before it was assigned.
Loading history...
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')
0 ignored issues
show
Unused Code introduced by
The variable file seems to be unused.
Loading history...
86
        req = urllib.request.Request(url)
0 ignored issues
show
Unused Code introduced by
The variable req seems to be unused.
Loading history...
87
88
    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...
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