Test Failed
Push — master ( 8192fe...83ef1f )
by Steffen
02:25
created

saucenao.worker   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 129
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 21
eloc 63
dl 0
loc 129
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
B Worker.move_to_categories() 0 33 5
B Worker.run() 0 26 5
A Worker.excludes() 0 10 3
A Worker.files() 0 14 3
A Worker.get_similar_title() 0 16 4
A Worker.__init__() 0 10 1
1
#!/usr/bin/python
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
# -*- coding: utf-8 -*-
3
import time
4
5
from titlesearch import get_similar_titles
0 ignored issues
show
introduced by
Unable to import 'titlesearch'
Loading history...
6
7
from saucenao import SauceNao, FileHandler
8
9
10
class Worker(SauceNao):
0 ignored issues
show
Unused Code introduced by
The variable __class__ seems to be unused.
Loading history...
11
    """
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
12
    Worker class for checking a list of files
13
    """
14
15
    def __init__(self, files, *args, **kwargs):
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
16
        """
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
17
        initializing function
18
19
        :type files: list|tuple|Generator
20
        :param args:
21
        :param kwargs:
22
        """
23
        super().__init__(*args, **kwargs)
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
24
        self.complete_file_list = files
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
25
26
    def run(self):
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
27
        """Check all files with SauceNao and execute the specified tasks
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
28
29
        :return:
30
        """
31
        for file_name in self.files:
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
32
            start_time = time.time()
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
33
34
            filtered_results = self.check_file(file_name)
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
35
36
            if not filtered_results:
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
37
                self.logger.info('No results found for image: {0:s}'.format(file_name))
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (87/80).

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

Loading history...
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
38
                continue
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
39
40
            if self._move_to_categories:
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
41
                self.move_to_categories(file_name=file_name, results=filtered_results)
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (86/80).

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

Loading history...
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
42
            else:
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
43
                yield {
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
44
                    'filename': file_name,
45
                    'results': filtered_results
46
                }
47
48
            duration = time.time() - start_time
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
49
            if duration < (30 / SauceNao.LIMIT_30_SECONDS):
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
50
                self.logger.debug("sleeping '{:.2f}' seconds".format((30 / SauceNao.LIMIT_30_SECONDS) - duration))
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (114/80).

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

Loading history...
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
51
                time.sleep((30 / SauceNao.LIMIT_30_SECONDS) - duration)
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
52
53
    @property
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
54
    def excludes(self):
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
55
        """Property for excludes
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
56
57
        :return:
58
        """
59
        if self._exclude_categories:
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
unused-code introduced by
Unnecessary "else" after "return"
Loading history...
60
            return [l.lower() for l in self._exclude_categories.split(",")]
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
61
        else:
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
62
            return []
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
63
64
    @property
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
65
    def files(self):
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
66
        """Property for files
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
67
68
        :return:
69
        """
70
        if self._start_file:
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
71
            # change files from generator to list
72
            files = list(self.complete_file_list)
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
73
            try:
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
74
                return files[files.index(self._start_file):]
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
75
            except ValueError:
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
76
                return self.complete_file_list
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
77
        return self.complete_file_list
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
78
79
    def move_to_categories(self, file_name: str, results):
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
80
        """Check the file for categories and move it to the corresponding folder
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
81
82
        :type file_name: str
83
        :type results: list|tuple|Generator
84
        :return: bool
85
        """
86
        categories = self.get_content_value(results, SauceNao.CONTENT_CATEGORY_KEY)
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (83/80).

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

Loading history...
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
87
88
        if not categories:
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
89
            self.logger.info("no categories found for file: {0:s}".format(file_name))
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (85/80).

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

Loading history...
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
90
            return False
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
91
92
        self.logger.debug('categories: {0:s}'.format(', '.join(categories)))
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
93
94
        # since many pictures are tagged as original and with a proper category
95
        # we remove the original category if we have more than 1 category
96
        if len(categories) > 1 and 'original' in categories:
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
97
            categories.remove('original')
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
98
99
        # take the first category
100
        category = categories[0]
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
101
102
        category = self.get_similar_title(category)
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
103
104
        # sub categories we don't want to move like original etc
105
        if category.lower() in self.excludes:
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
106
            self.logger.info("skipping excluded category: {0:s} ({1:s})".format(category, file_name))
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (101/80).

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

Loading history...
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
107
            return False
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
108
109
        self.logger.info("moving {0:s} to category: {1:s}".format(file_name, category))
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (87/80).

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

Loading history...
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
110
        FileHandler.move_to_category(file_name, category, base_directory=self._directory)
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (89/80).

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

Loading history...
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
111
        return True
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
112
113
    def get_similar_title(self, category):
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
114
        """
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
115
116
        :param category:
117
        :return:
118
        """
119
        if get_similar_titles:
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
120
            similar_titles = get_similar_titles(category)
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
121
122
            if similar_titles and similar_titles[0]['similarity'] * 100 >= self._title_minimum_similarity:
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (106/80).

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

Loading history...
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
123
                self.logger.info(
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
124
                    "Similar title found: {0:s}, {1:s} ({2:.2f}%)".format(
125
                        category, similar_titles[0]['title'], similar_titles[0]['similarity'] * 100))
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (101/80).

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

Loading history...
126
                return similar_titles[0]['title']
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
127
128
        return category
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
129