Difference between revisions of "Python movie scraper development"

From Official Kodi Wiki
Jump to navigation Jump to search
m (added Category:Scraper using HotCat)
 
(One intermediate revision by the same user not shown)
Line 45: Line 45:
* '''Unique IDs''': set via <code>ListItem.setUniqueIDs()</code> instance method. A scraper should set at least the default unique ID from the movie information site it works with.
* '''Unique IDs''': set via <code>ListItem.setUniqueIDs()</code> instance method. A scraper should set at least the default unique ID from the movie information site it works with.
Optionally it can set unique IDs from other online movie databases.
Optionally it can set unique IDs from other online movie databases.
If an .NFO contains all the necessary info about the respective movie in XML format then this actions may not pass any <code>xbmcgui.ListItem</code> instance to Kodi.
In this case Kodi will use all the information parsed from the XML file and will not call '''getdetails''' action.


==getdetails==
==getdetails==

Latest revision as of 11:47, 5 August 2022

Home icon grey.png   ▶ Scrapers
▶ Development
▶ Python movie scraper development

1 Introduction

Historically, Kodi has been supporting XML scraping addons that allow parsing online information sources about movies, TV shows, music and so on. However, this approach has its limitations. First, XML parsing definitions with regular expressions are difficult to write and maintain. Second, many information sources implemented REST APIs for getting information and regular expressions are not suitable for parsing JSON data. That is why Python scrapers have been introduced.

Python scrapers are written in Python language and have similar structure to media addons (media plugins). They are also called by special plugin:// URLs with query parameters. The main query parameter is action that defines a scraping stage. An example of a URL query sting passed to a scraper plugin: ?action=getdetails&url=foo&pathSettings=%7B%22foo%22%3A+%22bar%22%7D. Like other plugins, scrapers are also interact with Kodi via the functions of xbmcplugin Python API module and pass information to Kodi through xbmcgui.ListItem instances. Basically, a Python scraper is a media plugin that passes information to the Kodi database instead of presenting lists of playable media items.

A movie scraper must support "action" calls that are described below.

2 Actions

2.1 Find

The find action is used for searching for a specific movie by title and optionally a year that are passed as additional query parameters of the plugin call. This action should use xbmcplugin.addDirectoryItem or xbmcplugin.addDirectoryItems to pass xbmcgui.ListItem instances to Kodi. If only one instance is passed then it is considered as a perfect match. Otherwise the media file won't be matched and will need to be resolved manually by selecting a necessary item from a list. The xbmcgui.ListItem instances must be assigned the following properties:

  • label: passed as label parameter to the class constructor. This is the label that is presented to a user during scraping.
  • url: passed as url parameter to the class constructor. This should be some unique string that can be used to request all the necessary movie info from data provider's

website or API. It can be, for example, a link to a movie page on a movie information website or a some unique ID to request movie information from a REST API.

  • thumb (optional): passed via setArtwork() of xbmcgui.ListItem class instance method. This should be a URL of a movie poster, for example.

2.2 NfoUrl

The NfoUrl action is called as an alternative to find if an .NFO file is present along with a movie file. The entire .NFO file contents are passed as nfo parameter. This action should use xbmcplugin.addDirectoryItem to pass a single xbmcgui.ListItem instance to Kodi.

The xbmcgui.ListItem instances must be assigned the following properties:

  • url: passed as url parameter to the class constructor. This should be some unique string that can be used to request all the necessary movie info from data provider's

website or API. It can be, for example, a link to a movie page on a movie information website or a some unique ID to request movie information from a REST API.

  • Unique IDs: set via ListItem.setUniqueIDs() instance method. A scraper should set at least the default unique ID from the movie information site it works with.

Optionally it can set unique IDs from other online movie databases.

If an .NFO contains all the necessary info about the respective movie in XML format then this actions may not pass any xbmcgui.ListItem instance to Kodi. In this case Kodi will use all the information parsed from the XML file and will not call getdetails action.

2.3 getdetails

The getdetails action must pass a single xbmcgui.ListItem via xbmcplugin.setResolvedUrl() function. This action receives url query parameter from the previous stages and should set as much information to the xbmcgui.ListItem instance as possible using appropriate methods.

2.4 getartwork

The getartwork action must pass a single xbmcgui.ListItem via xbmcplugin.setResolvedUrl() function. This action receives id query parameter that is the default unique ID set by NfoUrl or getdetails actions. This action should set available artwork using ListItem.addAvailableArtwork() and ListItem.setAvailableFanart() instance methods.

Warning: if your .NFO file contains the default unique ID that is different from the ID of the movie database your scraper works with, you must set the correct default unique ID in NfoUrl action. Otherwise getdetails will receive the default unique ID from your .NFO file and your scraper may not be able to find any artwork using this ID.

3 Path Settings

A scraper receives the current path settings via pathSettings query parameters as a JSON-encoded string with each action call.

4 Example Movie Scraper

A very high-level example of a Python movie scraper can be found in the Kodi source code as metadata.demo.movie addon.