Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
intracer committed Mar 16, 2024
1 parent 990d8e8 commit 9d08250
Show file tree
Hide file tree
Showing 10 changed files with 65 additions and 110 deletions.
24 changes: 0 additions & 24 deletions .travis.yml

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,18 @@ import scala.util.matching.Regex.Match
*/
object CountryParser {

val countries = Country.fromJavaLocales
val countries: Seq[Country] = Country.fromJavaLocales

val contestCategory = "Category\\:Images from ([a-zA-Z ]+) (\\d+)"
val contestCategory: String = "Category\\:Images from ([a-zA-Z ]+) (\\d+)"

val contestRegex = (contestCategory + "$").r
val contestWithCountryRegex =
val contestRegex: Regex = (contestCategory + "$").r
val contestWithCountryRegex: Regex =
(contestCategory + " in ([a-zA-Z\\&\\- ]+)($|\\|)").r

val contestLinkRegex =
val contestLinkRegex: Regex =
"\\[\\[Commons\\:([a-zA-Z ]+) (\\d+) in ([a-zA-Z\\& ]+)\\|".r

def isContestCategory(s: String) = contestRegex.pattern.matcher(s).matches()
def isContestCategory(s: String): Boolean = contestRegex.pattern.matcher(s).matches()

val parser = new RegexCampaignParser(contestWithCountryRegex)

Expand Down
10 changes: 4 additions & 6 deletions scalawiki-wlx/src/main/scala/org/scalawiki/wlx/ImageDB.scala
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ class Grouping[T, F](name: String, val f: F => T, data: Iterable[F]) {

val grouped: Map[T, Seq[F]] = data.groupBy(f).mapValues(_.toSeq).toMap

def by(key: T): Seq[F] = grouped.getOrElse(key, Seq.empty)
def by(key: T): Seq[F] = grouped.getOrElse(key, Nil)

def contains(id: T): Boolean = grouped.contains(id)

Expand Down Expand Up @@ -196,8 +196,7 @@ object ImageGrouping {

def byMonument: Image => String = (i: Image) => i.monumentId.getOrElse("")

def byRegion: Image => String = (i: Image) =>
Monument.getRegionId(i.monumentId)
def byRegion: Image => String = (i: Image) => Monument.getRegionId(i.monumentId)

def byAuthor: Image => String = (i: Image) => i.author.getOrElse("")

Expand All @@ -213,9 +212,8 @@ object ImageDB {
monumentDb: Option[MonumentDB],
minMpx: Option[Float] = None
): Future[ImageDB] = {
imageQuery.imagesFromCategoryAsync(contest.imagesCategory, contest).map {
images =>
new ImageDB(contest, images, monumentDb, minMpx)
imageQuery.imagesFromCategoryAsync(contest.imagesCategory, contest).map { images =>
new ImageDB(contest, images, monumentDb, minMpx)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import org.scalawiki.MwBot
import org.scalawiki.dto.markup.SwTemplate
import org.scalawiki.edit.{PageUpdateTask, PageUpdater}
import org.scalawiki.wikitext.SwebleParser
import org.scalawiki.wlx.dto.Monument
import org.scalawiki.wlx.dto.{Monument, UploadConfig}
import org.sweble.wikitext.engine.config.WikiConfig
import org.sweble.wikitext.engine.utils.DefaultConfigEnWp
import org.sweble.wikitext.parser.nodes.WtTemplate

object ListFileNSRemover {

def updateLists(monumentDb: MonumentDB) {
def updateLists(monumentDb: MonumentDB): Unit = {
val task = new ListFileNSRemoverTask(MwBot.ukWiki, monumentDb)
val updater = new PageUpdater(task)
updater.update()
Expand All @@ -24,17 +24,17 @@ class ListFileNSRemoverTask(val host: String, monumentDb: MonumentDB)

val config: WikiConfig = DefaultConfigEnWp.generate

val titles = pagesToFix(monumentDb)
val titles: Set[String] = pagesToFix(monumentDb)

val uploadConfig = monumentDb.contest.uploadConfigs.head
val uploadConfig: UploadConfig = monumentDb.contest.uploadConfigs.head

override def updatePage(title: String, pageText: String): (String, String) = {
val template = uploadConfig.listTemplate
val wlxParser = new WlxTemplateParser(uploadConfig.listConfig, title)
var added: Int = 0

def mapper(wtTemplate: WtTemplate) = {
val swTemplate = new SwTemplate(wtTemplate)
def mapper(wtTemplate: WtTemplate): Unit = {
val swTemplate = SwTemplate(wtTemplate)
val monument = wlxParser.templateToMonument(swTemplate.template)

if (needsUpdate(monument)) {
Expand All @@ -56,9 +56,7 @@ class ListFileNSRemoverTask(val host: String, monumentDb: MonumentDB)
}

def needsUpdate(m: Monument): Boolean =
m.photo.exists(photo =>
photo.trim.startsWith("File:") || photo.trim.startsWith("Файл:")
)
m.photo.exists(photo => photo.trim.startsWith("File:") || photo.trim.startsWith("Файл:"))

def pagesToFix(monumentDb: MonumentDB): Set[String] = {

Expand Down
46 changes: 15 additions & 31 deletions scalawiki-wlx/src/main/scala/org/scalawiki/wlx/MonumentDB.scala
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package org.scalawiki.wlx

import java.time.{ZoneOffset, ZonedDateTime}

import org.scalawiki.dto.markup.Table
import org.scalawiki.wlx.dto.{AdmDivision, Contest, Monument}
import org.scalawiki.wlx.dto.{AdmDivision, Contest, Country, Monument}
import org.scalawiki.wlx.query.MonumentQuery
import org.scalawiki.wlx.stat.rating.PerPlaceStat

Expand All @@ -13,12 +12,12 @@ class MonumentDB(
withFalseIds: Boolean = true
) {

val monuments = allMonuments.filter(m => withFalseIds || isIdCorrect(m.id))
val wrongIdMonuments = allMonuments.filterNot(m => isIdCorrect(m.id))
val withArticles = allMonuments
val monuments: Seq[Monument] = allMonuments.filter(m => withFalseIds || isIdCorrect(m.id))
val wrongIdMonuments: Seq[Monument] = allMonuments.filterNot(m => isIdCorrect(m.id))
val withArticles: Map[String, Seq[Monument]] = allMonuments
.filter(m => m.name.contains("[["))
.groupBy(m => Monument.getRegionId(m.id))
val country = contest.country
val country: Country = contest.country

val _byId: Map[String, Iterable[Monument]] = monuments.groupBy(_.id)
val _byRegion: Map[String, Iterable[Monument]] =
Expand All @@ -37,9 +36,9 @@ class MonumentDB(

def ids: Set[String] = _byId.keySet

def byId(id: String) = _byId.getOrElse(id, Seq.empty[Monument]).headOption
def byId(id: String): Option[Monument] = _byId.getOrElse(id, Seq.empty[Monument]).headOption

def byRegion(regId: String) = {
def byRegion(regId: String): Iterable[Monument] = {
if (regId.length == 2) {
_byRegion.getOrElse(regId, Seq.empty[Monument])
} else {
Expand All @@ -49,21 +48,21 @@ class MonumentDB(
}
}

def regionIds =
def regionIds: Seq[String] =
_byRegion.keySet.toSeq.filter(contest.country.regionIds.contains).sorted

def isIdCorrect(id: String) = {
def isIdCorrect(id: String): Boolean = {
val idRegex = """(\d\d)-(\d\d\d)-(\d\d\d\d)"""
id.matches(idRegex) && contest.country.regionIds.contains(
Monument.getRegionId(id)
)
}

def withImages = monuments.filter(_.photo.isDefined)
def withImages: Seq[Monument] = monuments.filter(_.photo.isDefined)

def picturedIds = withImages.map(_.id).toSet
def picturedIds: Set[String] = withImages.map(_.id).toSet

def picturedInRegion(regionId: String) =
def picturedInRegion(regionId: String): Set[String] =
byRegion(regionId).map(_.id).toSet intersect picturedIds

def getAdmDivision(monumentId: String): Option[AdmDivision] = {
Expand Down Expand Up @@ -155,14 +154,15 @@ case class UnknownPlace(
def parents: Set[String] =
candidates.map(_.parent().map(_.name).getOrElse("")).toSet

override def toString = {
override def toString: String = {
val candidatesStr = candidates
.map { c =>
c.parent()
.map(p => s"${p.name}(${p.code})/")
.getOrElse("") + s"${c.name}(${c.code})"
}
.mkString(", ")

s"* [[$page]]/$regionId/$name. \n** ${monuments.size} monument(s): ${monuments
.map(_.id)
.mkString(", ")}" + (if (candidates.nonEmpty)
Expand All @@ -181,8 +181,7 @@ object MonumentDB {
var allMonuments = monumentQuery.byMonumentTemplate(date = date)

if (contest.country.code == "ru") {
allMonuments =
allMonuments.filter(_.page.contains("Природные памятники России"))
allMonuments = allMonuments.filter(_.page.contains("Природные памятники России"))
}

new MonumentDB(contest, allMonuments.toSeq)
Expand All @@ -191,19 +190,4 @@ object MonumentDB {
def getMonumentDb(contest: Contest, date: Option[ZonedDateTime]): MonumentDB =
getMonumentDb(contest, MonumentQuery.create(contest), date)

def getMonumentDbRange(
contest: Contest
): (Option[MonumentDB], Option[MonumentDB]) = {
if (contest.uploadConfigs.nonEmpty) {
val date =
ZonedDateTime.of(contest.year, 9, 1, 0, 0, 0, 0, ZoneOffset.UTC)
(
Some(getMonumentDb(contest, None)),
Some(getMonumentDb(contest, Some(date)))
)
} else {
(None, None)
}
}

}
19 changes: 9 additions & 10 deletions scalawiki-wlx/src/main/scala/org/scalawiki/wlx/RegionFixer.scala
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
package org.scalawiki.wlx

import org.scalawiki.wlx.dto.Monument
import org.scalawiki.wlx.dto.{AdmDivision, Contest, Country, Monument}

object RegionFixer {

def fixLists(monumentDb: MonumentDB) {
def fixLists(monumentDb: MonumentDB): Unit = {
ListUpdater.updateLists(monumentDb, new RegionFixerUpdater(monumentDb))
}
}

class RegionFixerUpdater(monumentDb: MonumentDB) extends MonumentUpdater {

val contest = monumentDb.contest
val country = contest.country
val oblasts = country.regions.filter(adm =>
!Set("Київ", "Севастополь").contains(adm.name)
)
val raions = oblasts.flatMap(_.regions).filter(_.name.endsWith("район"))
val raionNames = raions.map(_.name).toSet
val contest: Contest = monumentDb.contest
val country: Country = contest.country
val oblasts: Seq[AdmDivision] =
country.regions.filter(adm => !Set("Київ", "Севастополь").contains(adm.name))
val raions: Seq[AdmDivision] = oblasts.flatMap(_.regions).filter(_.name.endsWith("район"))
val raionNames: Set[String] = raions.map(_.name).toSet

val nameParam = contest.uploadConfigs.head.listConfig.namesMap("city")
val nameParam: String = contest.uploadConfigs.head.listConfig.namesMap("city")
val maxIndex = 2

def updatedParams(m: Monument): Map[String, String] = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ object SubsetCreator {

import scala.concurrent.ExecutionContext.Implicits.global

def main(args: Array[String]) {
def main(args: Array[String]): Unit = {

val specialNomination = "конкурс статей"

Expand All @@ -34,7 +34,7 @@ object SubsetCreator {
contest: Contest,
specialNomination: String,
monumentFilter: Monument => Boolean
) = {
): Unit = {
val ukWiki = MwBot.fromHost("uk.wikipedia.org")

val subset = monuments.filter(monumentFilter)
Expand Down Expand Up @@ -62,7 +62,7 @@ object SubsetCreator {
contest: Contest,
byRegion: Map[String, Seq[Monument]],
regionIds: SortedSet[String]
) {
): Unit = {
val buf = new StringBuffer
buf.append("__TOC__\n")

Expand Down Expand Up @@ -112,7 +112,7 @@ object SubsetCreator {
contest: Contest,
byRegion: Map[String, Iterable[Monument]],
regionIds: SortedSet[String]
) {
): Unit = {
for (regionId <- regionIds) {

val regionTitle = contest.country.regionName(regionId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class WlxTableParser(val config: ListConfig) {

val headers = table.headers.toIndexedSeq

def getIndex(name: String) = {
def getIndex(name: String): Option[Int] = {
config.namesMap.get(name).flatMap { mappedName =>
val index = headers.indexOf(mappedName)
if (index >= 0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,25 @@ import org.scalawiki.wlx.dto.lists.ListConfig
*/
class WlxTemplateParser(val config: ListConfig, val page: String) {

def getMappedName(name: String) = config.namesMap.get(name)
private def getMappedName(name: String): Option[String] = config.namesMap.get(name)

val id = getMappedName("ID")
val name = getMappedName("name")
val year = getMappedName("year")
val description = getMappedName("description")
val city = getMappedName("city")
val cityType = getMappedName("cityType")
val place = getMappedName("place")
val user = getMappedName("user")
val area = getMappedName("area")
val lat = getMappedName("lat")
val lon = getMappedName("lon")
val image = getMappedName("photo")
val gallery = getMappedName("gallery")
val stateId = getMappedName("stateId")
val typ = getMappedName("type")
val subType = getMappedName("subType")
val resolution = getMappedName("resolution")
private val id = getMappedName("ID")
private val name = getMappedName("name")
private val year = getMappedName("year")
private val description = getMappedName("description")
private val city = getMappedName("city")
private val cityType = getMappedName("cityType")
private val place = getMappedName("place")
private val user = getMappedName("user")
private val area = getMappedName("area")
private val lat = getMappedName("lat")
private val lon = getMappedName("lon")
private val image = getMappedName("photo")
private val gallery = getMappedName("gallery")
private val stateId = getMappedName("stateId")
private val typ = getMappedName("type")
private val subType = getMappedName("subType")
private val resolution = getMappedName("resolution")

def parse(wiki: String): Iterable[Monument] = {
val templates = TemplateParser.parse(wiki, config.templateName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ case class Coordinate(lat: String, lon: String) {
}

object Coordinate {
def apply(latOpt: Option[String], lonOpt: Option[String]) = for (
def apply(latOpt: Option[String], lonOpt: Option[String]): Option[Coordinate] = for (
lat <- latOpt; lon <- lonOpt
)
yield new Coordinate(lat, lon)
Expand Down

0 comments on commit 9d08250

Please sign in to comment.