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