Download

Write a source

Optional capabilities

A source adds an interface for each thing its site can do beyond the basics, and Katari shows the matching feature only for sources that have it.

What each one adds #

interface ConfigurableSource : UnifiedSource
A settings screen for the source, as in Settings.

Implement

fun setupPreferenceScreen(screen: EntryPreferenceScreen)
interface RelatedEntriesSource : UnifiedSource
Related entries on a title's page, in the order returned. They can be of any kind, so set each one's type.

Implement

suspend fun getRelatedEntries(entry: SEntry): List<RelatedEntryGroup>
Each RelatedEntryGroup(title, entries) shows as a row of covers under its title, such as Sequels. If the site doesn't say how titles relate, return one group with a null title.
interface ResolvableSource : UnifiedSource
Addresses on the site open as the title or chapter they point to, as in Opening links.

Implement

fun getUriType(uri: String): EntryUriType
suspend fun getEntry(uri: String): SEntry?
suspend fun getChapter(uri: String): SEntryChapter?
interface MergedEntrySource : UnifiedSource
A title made of other titles of the source, such as a show of several seasons. See Merged entries.

Implement

suspend fun getMergedEntries(entry: SEntry): List<SEntry>
interface DeferredStreamSource : UnifiedSource
Video links that take extra requests to find, looked up only for the stream about to play. See Links found later.

Implement

suspend fun resolveStream(chapter: SEntryChapter, stream: VideoStream): VideoStream
interface EntryPreviewSource : UnifiedSource
Still images of a video title, such as frames from a film, in its Preview, which the user opens by pressing and holding it in a list. Previews show on Android only for now.

Implement

suspend fun getEntryPreview(entry: SEntry): List<EntryPreviewImage>
Each image is an EntryPreviewImage(index, imageUrl, title = null, url = null).
interface ChapterListPartsSource : UnifiedSource
A long chapter list read in parts, the newest first and the older ones in the background. See Long chapter lists.

Implement

suspend fun getOlderChapters(entry: SEntry, token: String): ChapterListPart
interface ArtworkSource : UnifiedSource
Covers and chapter stills loaded with the source's client and headers, for a host that serves images only with the site's Referer or cookies. EntryHttpSource and EntryImageSource already have it; without it Katari loads them with its own client and no source headers.

Implement

val client: OkHttpClient
val headers: Headers
interface SiteStorageSource : UnifiedSource
The site's web storage, for a site that keeps a login or other state in localStorage or sessionStorage, which unlike cookies Katari's client never sees. While the user browses the source's site in WebView, Katari reads the page's storage after each page load and hands over the last reading when WebView closes.

Implement

fun saveSiteStorage(storage: SiteStorage)
Called on the main thread, so keep it short, such as saving what you need in the source's settings. SiteStorage holds the page's url, which may be another site the user went on to, such as a login provider, and its local and session maps.
interface WebPageSource : UnifiedSource
Pages of a site that only works with its scripts running, which Katari keeps open out of sight between the source's calls, as in Sites that need scripts.

Implement

fun webPageSpec(name: String): WebPageSpec
suspend fun prepareWebPage(name: String, page: WebPage)
interface ChapterWebViewSource : WebViewSource
WebView for a single chapter. EntryImageHttpSource already has it.

Implement

fun getContentUrl(entry: SEntry): String
fun getChapterUrl(chapter: SEntryChapter): String
interface ResumableEntryImageSource : EntryImageSource
Comic page downloads that pick up where they stopped: Katari asks for the rest with a Range header and keeps what it has only when the host answers 206 Partial Content. A marker; there is nothing to implement.
interface EntryItemOrientationProvider
Wide covers in lists with HORIZONTAL, for sites whose thumbnails are landscape.

Implement

val itemOrientation: EntryItemOrientation
VERTICAL unless overridden.
interface UnmeteredSource
No warning for this source when a library update holds more than 60 of its titles, or comic downloads queue more than 30 chapters or 15 from it. For sources that can take the load, such as your own server. A marker; there is nothing to implement.
interface ChapterNumberRecognitionSource
Katari reads chapter numbers from the names of chapters whose chapterNumber is -1. A marker; there is nothing to implement.
interface EmptyChapterListSource
An empty chapter list is accepted rather than treated as an error. A marker; there is nothing to implement.

The immersive view, which plays comics and videos one after another, needs none of these: it works with every source. Books don't take part, and a source whose supportedEntryTypes has neither MANGA nor ANIME doesn't offer it.

Settings #

Settings are AndroidX preferences, and Katari keeps their values for each source and each profile. Read them with getSourcePreferences():

Kotlin
override fun setupPreferenceScreen(screen: EntryPreferenceScreen) {
    screen.addPreference(
        ListPreference(screen.context).apply {
            key = "mirror"
            title = "Mirror"
            entries = arrayOf("shows.example", "mirror.shows.example")
            entryValues = entries
            setDefaultValue("shows.example")
            summary = "%s"
        },
    )
}

override val baseUrl: String
    get() = "https://" + getSourcePreferences().getString("mirror", "shows.example")

Users reach them through Browse › Extensions, the extension, then the source's settings button. Keep every key once published, or users lose what they chose. ⋮ › Clear data on the extension's screen deletes its settings, stored logins and saved data in the open profile.

When an address reaches Katari, such as one another app shares with it, Katari asks each ResolvableSource in turn:

  1. getUriType(uri) answers quickly, without a request, whether the address belongs to the source: EntryUriType.Entry for a title, Chapter for a chapter, or Unknown. The first source that doesn't answer Unknown gets the address. An exception from any source stops the lookup with an error.
  2. getEntry(uri) returns the title, also for a chapter's address, where it is the title the chapter belongs to. Katari stores it as returned, so fill in its title. Null means the address opens nothing.
  3. For a chapter, getChapter(uri) returns it. Katari only uses its url, to find the chapter among the title's, and refreshes the chapter list once when it isn't there yet.
Kotlin
override fun getUriType(uri: String): EntryUriType {
    val url = uri.toHttpUrlOrNull() ?: return EntryUriType.Unknown
    if (url.host != "shows.example") return EntryUriType.Unknown
    return when (url.pathSegments.firstOrNull()) {
        "show" -> EntryUriType.Entry
        "watch" -> EntryUriType.Chapter
        else -> EntryUriType.Unknown
    }
}

override suspend fun getEntry(uri: String): SEntry? {
    val showUrl = when (getUriType(uri)) {
        EntryUriType.Entry -> uri
        EntryUriType.Chapter -> fetch(uri).selectFirst(".breadcrumb a.show")?.absUrl("href") ?: return null
        EntryUriType.Unknown -> return null
    }
    val entry = SEntry.create().apply {
        setUrlWithoutDomain(showUrl)
        title = ""
        type = EntryType.ANIME
    }
    return refresh(entry, emptyList(), fetchDetails = true, fetchChapters = false).entry
}

override suspend fun getChapter(uri: String): SEntryChapter? =
    SEntryChapter.create().apply {
        url = getUrlWithoutDomain(uri)
        name = ""
    }

Here refresh fills in the title, along with the rest of the details.

Sites that need scripts #

Some sites send an empty page and build everything with their scripts, or sign their own API requests in a way only those scripts know. A WebPageSource uses such a site the way a person does. Katari loads the address from webPageSpec in a web view nobody sees, calls prepareWebPage once it has loaded, and keeps the page open between the source's calls, so the site's app starts once and later calls move around inside it.

Kotlin
class ExampleShows : EntryHttpSource(), WebPageSource {
    override fun webPageSpec(name: String) = WebPageSpec(url = "https://shows.example/")

    // The site's app has started once its search field shows
    override suspend fun prepareWebPage(name: String, page: WebPage) {
        page.awaitSelector("input[name=q]")
    }

    override suspend fun getSearchContent(
        page: Int,
        query: String,
        filters: EntryFilterList,
    ): EntryPageResult<SEntry> = webPage { site ->
        site.type("input[name=q]", query)
        val response = site.responseTo({ it.url.contains("/api/search") }) {
            site.press(WebPageKey.ENTER)
        }
        searchResults(response.body)
    }
}

webPage lends the source its page named "main" for the length of the block. Don't keep the page after the block returns, and don't ask for the same page again inside it, which would wait for itself. One call has a page at a time, and a second call for it waits. Here searchResults stands for the source's own reading of the site's JSON.

class WebPageSpec(url: String, size: WebPageSize = WebPageSize.PHONE, userAgent: String? = null, headers: Map<String, String> = emptyMap())
The window the site lays itself out in is the same on Android and Linux, so the source's selectors meet one layout of the site: PHONE is 412 × 915, DESKTOP 1280 × 800. A null userAgent sends the one Katari's own requests send. headers go with the request for url and with every load, not with what the page's scripts request.
suspend fun load(url: String)
Loads a new document and waits for it to finish. Nothing else waits for a page to load, since a site's app changes route without loading one: wait for what the route shows with awaitSelector, where it leads with awaitUrl, or what it requests with responseTo.
suspend fun evaluate(script: String, timeout: Duration = 30.seconds): String
Runs the script as the page's own scripts run and returns the value of its last statement as JSON. When the script is one expression whose value is a promise, such as (async () => { … })(), it returns what the promise resolves to. It fails with what the script threw or the promise was rejected with, and when the promise hasn't settled after timeout.
suspend fun document(): Document
The page as its scripts rendered it, to read with Jsoup as usual.
suspend fun click(selector: String)
Scrolls the element into view and taps its middle as a finger would, which the page can't tell from a person's tap. It returns once the page has seen the tap, and fails where the tap can't land on the element, such as one under a banner.
suspend fun swipe(selector: String, direction: WebPageDirection)
Moves a finger across the element, for what the site's scripts move, such as a carousel. What the browser scrolls itself answers differently on Android and Linux; bring it into view with scrollTo, scrollToEnd or click.
suspend fun type(selector: String, text: String)
Replaces what the field holds with text, all at once as a keyboard's text entry does, and returns once the page has it.
suspend fun press(key: WebPageKey)
Presses a key, such as ENTER, on whatever has the focus.
suspend fun scrollToEnd()
Scrolls to the end of the document, where a site that loads more as you read asks for the next part.
suspend fun responseTo(matches: (WebPageResponse) -> Boolean, timeout: Duration = 30.seconds, action: suspend () -> Unit): WebPageResponse
Runs action and returns the first response the page's scripts receive that matches accepts, with its url, status and text body. The watch starts before the action, so an immediate answer isn't missed. It sees only what the page's own document requests with fetch or XMLHttpRequest.

WebPage also has url, scrollTo, awaitSelector and awaitUrl. Selectors are CSS selectors as document.querySelector takes them, and the first match counts.

Katari closes a page after five minutes without a call, when memory runs short, or when its renderer stops, and makes and prepares a new one for the next call. It calls prepareWebPage again for a kept page when the user switches profile, since the source's settings may differ. It keeps two pages per source on Android and one on Linux, so a second name waits for, or replaces, the source's other page there.