Okay, let me start off by saying that I’m, like, an extreme data nerd and that it’s a large part of what I love so much about this site and also how that manifests as enjoyment when I’m able to help deepen existing stores of knowledge such as at TMDB, the Wikimedia projects and (of course) here. Perhaps that provides some context for why I’ve long been supremely irked by the fact that when I click on the Wikipedia link button presented on Trakt entity pages, I’m taken to the CirrusSearch results for the entity’s name with some appended generic keywords rather than the actual article page, even when the Wikidata Q number is a part of the TMDB record for the entity in question.
I’d like to use a timely example of last night’s season finale episode of Severance, entitled “Cold Harbor” along with my decidedly rudimentary grasp of the TMDB and Wikibase REST APIs to illustrate how I would like to see Trakt approach this integration. We’ll begin at the entity page linked above for the episode Cold Harbor. To the best of my knowledge, all of the information presented here is sourced almost in its entirety from TMDB, which includes among the potential data points for any entity its Wikidata Q number, exposed in the TMDB API as wikidata_id
. Those of you with TMDB accounts can input into the preceding link just three simple a priori datums available right from the Trakt page that identify this specific television episode there and view for yourself the information that is returned:
series_id
: 95396
(this is the third-level element in the TMDB link button target present on the Trakt episode page)season_number
: 2
(the fifth-level element from the above-referenced link)episode_number
: 10
(the same link’s seventh-level and terminal element)
If you’re following along at home you need to click the “Try It” button on the page to see what is returned by this particular query, and for the Sweathogs up in the cheap seats, I’ll place a copy of it here, too.
{
"id": 5469142,
"imdb_id": "tt15242998",
"freebase_mid": null,
"freebase_id": null,
"tvdb_id": 10592768,
"tvrage_id": null,
"wikidata_id": "Q132145883"
}
This establishes that even now, less than 24 hours after the episode first aired, some beneficent soul (that wasn’t me!) has already done the Lord’s work by recording the episode’s Wikidata Q number on TMDB. Now though this is not the direct URL to episode’s English Wikipedia article, it might as well be, as I’m about to demonstrate.
One of Wikidata’s foundational purposes is to record the page names for articles about any given entity across all of the Wikimedia projects and language variants that are known as “interwiki links” to my fellow wiki-nerds. To request those links from a given Wikidata entry is usually done as a SPARQL query against its Q number, but to keep things easy to reproduce I’m going to use an incredible tool called wikibase-cli
to keep SPARQL behind the curtains and transform that TMDB wikidata_id
response directly into the URL we’re looking for as just one action.
Proceeding under the assumption that you have a Wikimedia account and already added it to the settings file for wikibase-cli (whose command name is actually just wb
) now installed on your device, let’s get to work. Honestly, calling it “work” may be a bit of an overstatement in this case, as having the right tools is 90% of any job, and all we have left to do is execute this command using the wikidata_id
from TMDB:
$ wb data --simplify --props sitelinks Q132145883
{
"id": "Q132145883",
"type": "item",
"sitelinks": {
"enwiki": "Cold Harbor (Severance)"
}
}
Bingo! There’s our English Wikipedia page name sitting right there as the value for the sitelinks.enwiki
key. To really come full circle and arrive at a valid target URL for the Wikipedia page, complete with percent encoding for the reserved characters, all we need to add to our “one-liner” are a couple of the usual suspects known to command line junkies everywhere: jq
and tr
:
$ wb data --simplify --props sitelinks Q132145883 |
jq '@uri "https://en.wikipedia.org/wiki/\(.sitelinks.enwiki)"' |
tr -d '"'
https://en.wikipedia.org/wiki/Cold%20Harbor%20%28Severance%29
Is that too much work for a Wikipedia button on Trakt pages that takes you right to the page you’re wanting to read? I’ll let you be the judge. I’ve been here plenty long enough to see the Trakt team tackle probably a hundred or more problems of far greater magnitude than I could ever hope to solve myself that when I see one I can figure out, I can’t help but think they could manage it as easily as blowing their nose. It’s probably not entirely fair to let my respect for them manifest as expectation in this way, but I don’t make the rules here, and I do always pay for my VIP renewals on time.
While I do hope this manages to become implemented in some way here, it was a pleasure in itself getting a chance to demonstrate my approach to it, and if you’re still with me, thank you for having the patience and curiosity to follow along. If you see any way I could have arrived at a better approach to this endeavor, I do hope you’ll share them. Cheers, everyone.
— Peter, a/k/a RogueScholar