root/trunk/flexget/plugins/input_csv.py @ 724

Revision 724, 1.7 KB (checked in by paranoidi, 14 months ago)

tvsubtitles resolving built in, updated InputCVS (used old api)

  • Property svn:eol-style set to native
Line 
1import urllib2
2import logging
3from flexget.feed import Entry
4from flexget.plugin import *
5
6log = logging.getLogger('csv')
7
8class InputCSV:
9    """
10        Adds support for CSV format. Configuration may seem a bit complex,
11        but this has advantage of being universal solution regardless of CSV
12        and internal entry fields.
13
14        Configuration format:
15
16        csv:
17          url: <url>
18          values:
19            <field>: <number>
20
21        Example DB-fansubs:
22
23        csv:
24          url: http://www.dattebayo.com/t/dump
25          values:
26            title: 3  # title is in 3th field
27            url: 1    # download url is in 1st field
28
29        Fields title and url are mandatory. First field is 1.
30        List of other common (optional) fields can be found from wiki.
31    """
32    def validator(self):
33        from flexget import validator
34        config = validator.factory('dict')
35        config.accept('url', key='url', required=True)
36        values = config.accept('dict', key='values', required=True)
37        values.accept_any_key('number')
38        return config
39
40    def feed_input(self, feed):
41        url = feed.config['csv'].get('url', None)
42        if not url: raise Exception('CSV in %s is missing url' % feed.name)
43        page = urllib2.urlopen(url)
44        for line in page.readlines():
45            data = line.split(",")
46            entry = Entry()
47            for name, index in feed.config['csv'].get('values', {}).items():
48                try:
49                    entry[name] = data[index-1]
50                except IndexError:
51                    raise Exception('Field %s index is out of range' % name)
52            feed.entries.append(entry)
53
54register_plugin(InputCSV, 'csv')
Note: See TracBrowser for help on using the browser.