flexget.plugins.input.input_csv
Covered: 37 lines
Missed: 13 lines
Skipped 12 lines
Percent: 74 %
 1
import logging
 2
import csv
 3
from flexget.entry import Entry
 4
from flexget.plugin import register_plugin, internet
 5
from flexget.utils.cached_input import cached
 6
from flexget.utils.tools import urlopener
 8
log = logging.getLogger('csv')
11
class InputCSV(object):
12
    """
13
        Adds support for CSV format. Configuration may seem a bit complex,
14
        but this has advantage of being universal solution regardless of CSV
15
        and internal entry fields.
17
        Configuration format:
19
        csv:
20
          url: <url>
21
          values:
22
            <field>: <number>
24
        Example DB-fansubs:
26
        csv:
27
          url: http://www.dattebayo.com/t/dump
28
          values:
29
            title: 3  # title is in 3th field
30
            url: 1    # download url is in 1st field
32
        Fields title and url are mandatory. First field is 1.
33
        List of other common (optional) fields can be found from wiki.
34
    """
36
    def validator(self):
37
        from flexget import validator
38
        config = validator.factory('dict')
39
        config.accept('url', key='url', required=True)
40
        values = config.accept('dict', key='values', required=True)
41
        values.accept_any_key('integer')
42
        return config
44
    @cached('csv')
45
    @internet(log)
46
    def on_feed_input(self, feed, config):
47
        entries = []
48
        page = urlopener(config['url'], log)
49
        for row in csv.reader(page):
50
            if not row:
51
                continue
52
            entry = Entry()
53
            for name, index in config.get('values', {}).items():
54
                try:
55
                    entry[name] = row[index-1]
56
                except IndexError:
57
                    raise Exception('Field `%s` index is out of range' % name)
58
            entries.append(entry)
59
        return entries
61
register_plugin(InputCSV, 'csv', api_ver=2)