| 1 | import urllib2 |
|---|
| 2 | import logging |
|---|
| 3 | from flexget.feed import Entry |
|---|
| 4 | from flexget.plugin import * |
|---|
| 5 | |
|---|
| 6 | log = logging.getLogger('csv') |
|---|
| 7 | |
|---|
| 8 | class 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', require=True) |
|---|
| 36 | values = config.accept('dict', key='values', require=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 | |
|---|
| 54 | register_plugin(InputCSV, 'csv') |
|---|