Python Markdown using MkDocs: Kodi¶
Important¶
This WILL NOT give you access to films that you do not currently have access to. Kodi is being used to download information about the films from The Movie Database and then for this information to be extracted through the Kodi API. At no point will the films themselves be downloaded!
A simple example that uses t-python-markdown and MkDocs to create a simple static website using metadata about films extracted from Kodi.
This example uses VirtualBox to both host Kodi and create/run the example code. If you want to run the example code outside, then you may need to adjust the networking for the VM to allow external access.
Excluding download times for VirtualBox and Ubuntu, this should take roughly 30-45 minutes to complete.
This is not meant to be a tutorial for MkDocs, Kodi, VirtualBox or Ubuntu, but should give you a basic setup to work from.
Pre-Requisites¶
This example has been written and tested on Linux.
You will need Kodi, but if you don't already have it available, that will be covered below.
Also, if you haven't used Kodi before, then please read Kodi Basic Controls as that will help navigating Kodi to add and remove media.
Install and Configure Kodi¶
If you already have access to Kodi and are happy to use it for this then you can skip this step.
This example requires a running copy of Kodi. The following instructions should get you up and running with Kodi:
Go to VirtualBox and follow the appropriate instructions for your OS and install VirtualBox.
Go to this Ubuntu Tutorial and follow the instructions to install Ubuntu Desktop in VirtualBox.
If possible, try to allocate a minimum of 2 cores, 4GB RAM and 50GB hard disk.
- Start the Ubuntu VM and login
- Start the terminal and
su -
to login as root (the password will be the same as the account you created during "Install Ubunti Desktop") - At the prompt, now type the following and press «enter»
apt install virtualenv -y
- Once installed, close the terminal
- Start the Ubuntu VM and login
- Open "Ubuntu Software", search for "Kodi" and install it
- Once installed, close "Ubuntu Software"
The following steps will take you through adding the media folder, enabling clean up (useful when testing and when you remove media) and also the web interface (required to enable access the JSON-RPC API).
- Start the Ubuntu VM and login
- Start the File Manager and ensure that folder
~/Videos/films
exists - Start Kodi
- Add
~/Videos/films
folder to Kodi:- Go to the "Movies" section and select "Enter files section"
- Select "Files" and "Add videos..."
- Click "Browse" then "Home folder" / "Videos" / "films" and click "OK"
- Click "OK"
- Then on "Set content" change "This directory contains" to "Movies"
- Click "OK" to add it
- On "Change content" click "No" as at present there is no content
- Finally, press ESC a couple of times to return to the Kodi home screen
- Enable clean up:
- Find and click on the "Settings" icon () at the top of the left menu
- Now click on "Media"
- In the bottom left corner is a settings icon with "Standard" next to it. Click on this until it changes to "Advanced"
- Finally, press ESC a couple of times to return to the Kodi home screen
- Enable web interface:
- Find and click on the "Settings" icon () at the top of the left menu
- Now click on "Services" and then "Control"
- Click on "Password" and set this, for now, set it to
kodi
- Enable "Allow remote control via HTTP". You will get a "Warning!" message about access to the web interface and that this device should never be exposted on the Internet. Good advice which should be followed. Click "Yes" to continue
- Finally, press ESC a couple of times to return to the Kodi home screen
MkDocs Installation¶
Use the following to initialise and configure a basic MkDocs setup:
- Start the Ubuntu VM and login
- Start the terminal
- Change to a directory where the folder
python-markdown-mkdocs-kodi
will be created -
Copy the following, paste it into the terminal and then press «enter»:
virtualenv python-markdown-mkdocs-kodi cd python-markdown-mkdocs-kodi . ./bin/activate pip install t-python-markdown mkdocs mkdocs-material mkdir -p src/docs/film src/docs/people
Now create the following files:
Copy and save the following to python-markdown-mkdocs-kodi/src/mkdocs.yml
:
site_name: !ENV [SITE_NAME, "My Kodi Media"]
site_author: t-python-markdown
use_directory_urls: false
theme:
name: material
markdown_extensions:
- pymdownx.emoji:
emoji_index: !!python/name:materialx.emoji.twemoji
emoji_generator: !!python/name:materialx.emoji.to_svg
- md_in_html
- attr_list
plugins:
- search
- tags:
tags_file: tags.md
extra_css:
- extra.css
nav:
- Home: "index.md"
Copy and save the following to python-markdown-mkdocs-kodi/src/docs/tags.md
:
---
title: Tags
search:
exclude: true
hide:
- navigation
---
# Tags
[TAGS]
Copy and save the following to python-markdown-mkdocs-kodi/src/docs/extra.css
:
hr {
margin-top: 8px !important;
margin-bottom: 8px !important;
}
a:hover {
text-decoration: underline;
text-decoration-color: #4e4e4e;
}
.plot {
font-size: 1.2em;
}
.year-rating-runtime {
font-size: 0.9em;
}
table {
table-layout: fixed;
display: table !important;
border: 0px !important;
}
.genre-list>ul {
list-style: none !important;
margin-left: 0px !important;
line-height: 40px;
}
.genre-list>ul>li {
text-align: center;
float: left;
white-space: nowrap;
text-decoration: none;
}
.genre-list>ul>li>a {
color: inherit;
text-decoration: none;
}
.cast-table>div>div>table>tbody>tr>td:nth-child(1) {
height: 2.5em;
}
.cast-table>div>div>table>tbody>tr>td:nth-child(n+1) {
vertical-align: middle;
font-size: 1.2em;
}
.people-table>div>div>table>thead>tr>th:nth-child(1) {
width: 130px;
}
.people-table>div>div>table>tbody>tr>td:nth-child(1) {
height: 2.5em;
}
.people-table>div>div>table>tbody>tr>td:nth-child(n+1) {
vertical-align: middle;
font-size: 1.2em;
}
th {
padding-top: 8px !important;
padding-bottom: 8px !important;
border-left: 0px !important;
border-right: 0px !important;
background-color: #f0f0f0 !important
}
td {
padding-top: 2px !important;
padding-bottom: 2px !important;
border-left: 0px !important;
border-right: 0px !important;
border-top: 1px solid #f0f0f0 !important;
border-bottom: 1px solid #f0f0f0 !important;
}
Starting MkDocs¶
Change directory to python-markdown-mkdocs-kodi/src
and start mkdocs
:
mkdocs serve
Once done, open a browser to http://127.0.0.1:8000. It should show, correctly, a 404 - Not found
message. This means that the basics are now in place.
What The Code Does¶
This example uses the Kodi JSON-RPC API to retrieve a list of films (movies) which it then iterates through creating a single page for each film. In addition, for every cast member, writer and director, a single page is created for each that lists all the films they are involved with.
It demonstrates how to use t-python-markdown
to quickly and easily generate markdown documents.
Python Code¶
Create a new file python-markdown-mkdocs-kodi/src/generate_site.py
, then copy and save the following to it:
generate_site.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 |
|
Add Films to Kodi¶
Important¶
Just a reminder. This WILL NOT give you access to films that you do not currently have access to. Kodi is being used to download information about the films from The Movie Database and then for this information to be extracted through the Kodi API. At no point will the films themselves be downloaded!
To get Kodi to retrieve the details of a film, all that is required is for an appropriately named file (name + year) to be created in ~/Videos/films/
. For example, Das Boot (1981).mkv
, Seven Samurai (1954).mkv
, etc.
The following will simplify that by processing the contents of a file where each line is the name of a film. For example, if you like Lord of the Rings, then create a file called ~/Videos/films/lotr.txt
with the following:
The Lord of the Rings The Fellowship of the Ring (2001)
The Lord of the Rings The Return of the King (2003)
The Lord of the Rings The Two Towers (2002)
Then at the terminal, change directory to ~/Videos/films/
and paste the following changing {{FILE}}
for lotr.txt
.
while IFS= read -r line; do touch "$line.mkv"; done < {{FILE}}
Once done, go to Kodi and run an update.
Note that the more films you add, the longer the site could take to start. Start small and add more once you have things up and running
A few film lists to get you started...
How to update Kodi
When you add any films, you will need to update Kodi. When you remove films, you will need to clean the library to remove those that you removed. There are multiple ways of achieving both these activities, the following should help you get started if you haven't used Kodi before:
- Add or Remove Films Select "Movies" from the Kodi home screen which should take you to "Movie / Titles". Now press the left arrow on the keyboard or click on "Options". From the menu that pops out, under "Actions" select "Update library". Wait until the update completes before proceeding
- Remove Films
From the Kodi home screen, click on the "Settings" icon () at the top of the left menu. Click on "Media", then in "Library" you should see "Clean library", select this. When prompted, select "Yes" to clean the library and wait. Also, remove any generated files in
src/docs/film
andsrc/docs/people
before running the python code (see below)
Extract The Films and Build the Website¶
Start the terminal, change directory to python-markdown-mkdocs-kodi
and ensure that you are running in a virtual environment as follows:
. ./bin/activate
Then change directory to src
and run the following:
python generate_site.py
If you don't run this on the Kodi VM or you are using your own Kodi, then you will need to use the --kodi-url
argument to pass the IP address and port of your Kodi instance. Also, if you used different credentials for Kodi (kodi
/kodi
if you followed the instructions above), then you will need to use the --user
and --password
arguments to pass the correct credentials.
Afterwards, the website should change to show a list of the films added to Kodi.
If you don't currently have mkdocs
running, see Starting MkDocs above
Further Exercises¶
Improve the Layout¶
Change the layout of the site. This example has a very basic layout. See what you can do to improve it.
Add Support for TV¶
Extend the generator to include TV shows from Kodi. I have included the Kodi API calls below to get you started. But you will need to add the relevant calls to these and integrate the responses in the page rendering.
Retrieve TV shows and Episodes from Kodi
def __kodi_get_tv_shows(self):
"""Retrieve list of TV shows from Kodi"""
params = {"jsonrpc": "2.0", "method": "VideoLibrary.GetTVShows", "id": 0,
"params": {
"properties": [
"title", "genre", "year", "rating", "plot",
"studio", "mpaa", "cast", "playcount", "episode",
"imdbnumber", "premiered", "votes", "lastplayed", "fanart",
"thumbnail", "originaltitle", "sorttitle", "episodeguide", "season",
"watchedepisodes", "dateadded", "tag", "art", "userrating",
"ratings", "runtime", "uniqueid"
]}}
return self.__get_kodi(params)["result"]["tvshows"]
def __get_tv_episodes(self, show_id: int):
"""Retrieve list of TV episodes from Kodi"""
params = {"jsonrpc": "2.0", "method": "VideoLibrary.GetEpisodes", "id": 0,
"params": {
"tvshowid": show_id,
"properties": [
"title", "plot", "votes", "rating", "writer",
"firstaired", "playcount", "runtime", "director", "productioncode",
"season", "episode", "originaltitle", "showtitle", "cast",
"streamdetails", "lastplayed", "fanart", "thumbnail", "file",
"resume", "tvshowid", "dateadded", "uniqueid", "art",
"specialsortseason", "specialsortepisode", "userrating", "seasonid", "ratings"
]}}
return self.__get_kodi(params)["result"].get("episodes", [])
Build a Website¶
Use MkDocs to build a website instead:
mkdocs build
and then take the generated output and deploy that using a web server of your choice.
GitLab Pages¶
If like me, you use GitLab, add this code to a GitLab project and create a Gitlab pipeline to deploy this via GitLab Pages.
Be careful, dependent on how many films you add to Kodi, the result could be sizeable and not upload correctly without adjusting the Pages configuration in Gitlab
Download Images from Kodi¶
Download the poster or fanart images from Kodi and include them on the media page.
Downloading images takes up a lot of disk space. If you do all of this in the VM, then these images will be stored 2 or 3 times. Once in Kodi, once in the docs
folder and if you build the site, again in the site
folder. Just make sure you have sufficient space available before including them.