Skip to content

Commit

Permalink
Add support for Guardian puzzles.
Browse files Browse the repository at this point in the history
See #46
  • Loading branch information
jpd236 committed Oct 29, 2024
1 parent 2ecb0ef commit 4b4caa4
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ kotlin {
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json-js:1.6.2")
implementation("com.github.ajalt.colormath:colormath-js:3.3.3")

implementation("com.jeffpdavidson.kotwords:kotwords-js:1.4.0")
implementation("com.jeffpdavidson.kotwords:kotwords-js:1.4.2")

// TODO: Migrate to kotlinx-datetime if parsing/formatting support is added.
implementation("com.soywiz.korlibs.klock:klock-js:4.0.10")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import com.jeffpdavidson.crosswordscraper.sources.BostonGlobeSource
import com.jeffpdavidson.crosswordscraper.sources.CrosshareSource
import com.jeffpdavidson.crosswordscraper.sources.CrosswordCompilerSource
import com.jeffpdavidson.crosswordscraper.sources.CrosswordNexusSource
import com.jeffpdavidson.crosswordscraper.sources.GuardianSource
import com.jeffpdavidson.crosswordscraper.sources.NewYorkTimesSource
import com.jeffpdavidson.crosswordscraper.sources.PuzzleLinkSource
import com.jeffpdavidson.crosswordscraper.sources.PuzzleSocietySource
Expand Down Expand Up @@ -69,6 +70,7 @@ object CrosswordScraper {
CrosshareSource,
CrosswordCompilerSource,
CrosswordNexusSource,
GuardianSource,
NewYorkTimesSource,
PuzzleSocietySource,
PzzlSource,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.jeffpdavidson.crosswordscraper.sources

import com.jeffpdavidson.crosswordscraper.Scraping
import com.jeffpdavidson.crosswordscraper.sources.Source.Companion.hostIsDomainOrSubdomainOf
import com.jeffpdavidson.kotwords.formats.Guardian
import org.w3c.dom.url.URL

object GuardianSource : FixedHostSource() {

override val sourceName: String = "The Guardian"
override fun neededHostPermissions(url: URL) = listOf("https://*.theguardian.com/*")

override fun matchesUrl(url: URL): Boolean = url.hostIsDomainOrSubdomainOf("theguardian.com")
override suspend fun scrapePuzzlesWithPermissionGranted(url: URL, tabId: Int, frameId: Int): ScrapeResult {
val scrapeFn = js(
"""function() {
var crosswordElems = document.getElementsByClassName("js-crossword");
if (crosswordElems.length == 0 || !crosswordElems[0].dataset["crosswordData"]) {
return '';
}
return crosswordElems[0].dataset["crosswordData"];
}"""
)
val puzzleJson = Scraping.executeFunctionForString(tabId, frameId, scrapeFn)
if (puzzleJson.isNotEmpty()) {
return ScrapeResult.Success(listOf(Guardian(puzzleJson)))
}
return ScrapeResult.Success(listOf())
}
}

0 comments on commit 4b4caa4

Please sign in to comment.