Source code for flexget.plugins.input.json
"""Plugin for json files."""
from pathlib import Path
from dateutil import parser
from loguru import logger
from flexget import plugin
from flexget.entry import Entry
from flexget.event import event
from flexget.utils import json
logger = logger.bind(name='json')
[docs]
class Json:
"""Return entries from a json file.
::
file: <path to JSON file>
encoding: <JSON encoding>
field_map:
- <entry field>: <corresponding JSON key>
Note: each entry must have at least two fields, 'title' and 'url'. If not specified in the config,
this plugin assumes that keys named 'title' and 'url' exist within the JSON.
'encoding' defaults to 'utf-8'
Example::
json:
file: entries.json
encoding: utf-8
field_map:
- title: name
"""
schema = {
'type': 'object',
'properties': {
'file': {'type': 'string', 'format': 'file'},
'encoding': {'type': 'string'},
'field_map': {'type': 'object', 'additionalProperties': {'type': 'string'}},
},
'required': ['file'],
'additionalProperties': False,
}
[docs]
@staticmethod
def ds_dt(val):
try:
return parser.parse(val)
except (ValueError, OverflowError):
return val
[docs]
@event('plugin.register')
def register_plugin():
plugin.register(Json, 'json', api_ver=2)