Skip to content

Commit

Permalink
mime
Browse files Browse the repository at this point in the history
  • Loading branch information
intracer committed Jul 6, 2024
1 parent 5f2c882 commit 49acd10
Show file tree
Hide file tree
Showing 6 changed files with 136 additions and 110 deletions.
9 changes: 6 additions & 3 deletions scalawiki-core/src/main/scala/org/scalawiki/dto/Image.scala
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ case class Image(
pageId: Option[Long] = None,
metadata: Option[ImageMetadata] = None,
categories: Set[String] = Set.empty,
specialNominations: Set[String] = Set.empty
specialNominations: Set[String] = Set.empty,
mime: Option[String] = None
) extends Ordered[Image] {

def compare(that: Image): Int = title.compareTo(that.title)
Expand Down Expand Up @@ -192,7 +193,8 @@ object Image {
url: Option[String],
pageUrl: Option[String],
pageId: Option[Long],
metadata: Option[Map[String, String]] = None
metadata: Option[Map[String, String]] = None,
mime: Option[String] = None
) =
new Image(
title = title,
Expand All @@ -204,7 +206,8 @@ object Image {
url = url,
pageUrl = pageUrl,
pageId = pageId,
metadata = metadata.map(ImageMetadata.apply)
metadata = metadata.map(ImageMetadata.apply),
mime = mime
)

def gallery(
Expand Down
18 changes: 8 additions & 10 deletions scalawiki-core/src/main/scala/org/scalawiki/dto/Page.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,29 +20,29 @@ case class Page(
) /*extends HasId[Page]*/ {
val history = new History(revisions)

def titleWithoutNs = title.split("\\:").last
def titleWithoutNs: String = title.split("\\:").last

def withText(text: String) =
def withText(text: String): Page =
copy(revisions = Page.revisionsFromText(Some(text)))

def text: Option[String] = revisions.headOption.flatMap(_.content)

def isTalkPage = ns.exists(_ % 2 == 1)
def isTalkPage: Boolean = ns.exists(_ % 2 == 1)

def isArticle = ns.contains(Namespace.MAIN)
def isArticle: Boolean = ns.contains(Namespace.MAIN)

def withId(id: Long): Page = copy(id = Some(id))

def lastRevisionUser: Option[Contributor] =
revisions.headOption.flatMap(_.user)

def appendLists(other: Page) = copy(
def appendLists(other: Page): Page = copy(
revisions = this.revisions ++ other.revisions,
langLinks = this.langLinks ++ other.langLinks,
links = this.links ++ other.links
)

def withoutContent = copy(revisions = revisions.map(_.withoutContent))
def withoutContent: Page = copy(revisions = revisions.map(_.withoutContent))

}

Expand Down Expand Up @@ -120,11 +120,9 @@ object Page {
ns: Option[Int],
title: String,
editToken: Option[String]
) = {
new Page(id, ns, title, Seq.empty, Seq.empty, editToken)
}
): Page = new Page(id, ns, title, Nil, Nil, editToken)

def revisionsFromText(text: Option[String]) =
def revisionsFromText(text: Option[String]): Seq[Revision] =
text.fold(Seq.empty[Revision])(content => Revision.many(content))

def groupById(pages: Seq[Page]): Map[Long, Seq[Page]] =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ package iiprop {
Size,
Dimensions,
Sha1,
Mime,
ThumbMime,
MediaType,
Metadata,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,8 @@ case class ImageReads(
Reads.pure[Option[Long]](pageId) ~
(__ \ "metadata")
.readNullable[Seq[JsObject]]
.map(_.map(_.map(readKv).toMap))
.map(_.map(_.map(readKv).toMap)) ~
(__ \ "mime").readNullable[String]
)(Image.basic _)

override def reads(json: JsValue): JsResult[Image] = imagesRead.reads(json)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import org.scalawiki.dto.cmd.Action
import org.scalawiki.dto.cmd.query.{PageIdsParam, Query}
import org.scalawiki.dto.cmd.query.prop.{ImageInfo, Prop}
import org.scalawiki.dto.cmd.query.prop.iiprop.{IiProp, Metadata}
import org.scalawiki.query.PropImageInfoSpec.{page1, page2, response1, response2}
import org.scalawiki.util.TestUtils.resourceAsString
import org.specs2.mutable.Specification
import spray.util.pimpFuture
Expand All @@ -17,95 +18,6 @@ import scala.concurrent.ExecutionContext.Implicits.global

class PropImageInfoSpec extends Specification with MockBotSpec {

def response1(generatorPrefix: String = "cm") =
s""" {
| "query": {
| "pages": {
| "32885574": {
| "pageid": 32885574,
| "ns": 6,
| "title": "File:Dovbush-rocks 01.JPG",
| "imagerepository": "local",
| "imageinfo": [
| {
| "timestamp": "2014-05-20T20:54:33Z",
| "user": "Taras r",
| "size": 4270655,
| "width": 3648,
| "height": 2736,
| "url": "https://upload.wikimedia.org/wikipedia/commons/e/ea/Dovbush-rocks_01.JPG",
| "descriptionurl": "https://commons.wikimedia.org/wiki/File:Dovbush-rocks_01.JPG"
| }]}}},
| "continue": {
| "g${generatorPrefix}continue": "file|44454d45524749373631312e4a50470a44454d45524749373631312e4a5047|32763876",
| "continue": "g${generatorPrefix}continue||"}}
""".stripMargin

val response2 =
""" {
| "query": {
| "pages": {
| "32885597": {
| "pageid": 32885597,
| "ns": 6,
| "title": "File:Dovbush-rocks 02.JPG",
| "imagerepository": "local",
| "imageinfo": [
| {
| "timestamp": "2014-05-20T20:55:12Z",
| "user": "Taras r",
| "size": 4537737,
| "width": 2736,
| "height": 3648,
| "url": "https://upload.wikimedia.org/wikipedia/commons/2/26/Dovbush-rocks_02.JPG",
| "descriptionurl": "https://commons.wikimedia.org/wiki/File:Dovbush-rocks_02.JPG"
| }]}}}}
| """.stripMargin

val page1 = Page(
Some(32885574),
Some(6),
"File:Dovbush-rocks 01.JPG",
Seq.empty,
Seq(
Image.basic(
"File:Dovbush-rocks 01.JPG",
Some(Timestamp.parse("2014-05-20T20:54:33Z")),
Some("Taras r"),
Some(4270655),
Some(3648),
Some(2736),
Some(
"https://upload.wikimedia.org/wikipedia/commons/e/ea/Dovbush-rocks_01.JPG"
),
Some("https://commons.wikimedia.org/wiki/File:Dovbush-rocks_01.JPG"),
Some(32885574)
)
)
)

val page2 = Page(
Some(32885597),
Some(6),
"File:Dovbush-rocks 02.JPG",
Seq.empty,
Seq(
Image.basic(
"File:Dovbush-rocks 02.JPG",
Some(Timestamp.parse("2014-05-20T20:55:12Z")),
Some("Taras r"),
Some(4537737),
Some(2736),
Some(3648),
Some(
"https://upload.wikimedia.org/wikipedia/commons/2/26/Dovbush-rocks_02.JPG"
),
Some("https://commons.wikimedia.org/wiki/File:Dovbush-rocks_02.JPG"),
Some(32885597)
)
)
)

"get image info in generator" should {
"query by category members" in {

Expand All @@ -117,7 +29,7 @@ class PropImageInfoSpec extends Specification with MockBotSpec {
"gcmtitle" -> "Category:SomeCategory",
"gcmlimit" -> "max",
"prop" -> "imageinfo",
"iiprop" -> "timestamp|user|comment",
"iiprop" -> "timestamp|user|comment|mime",
"continue" -> ""
),
response1("cm")
Expand All @@ -129,7 +41,7 @@ class PropImageInfoSpec extends Specification with MockBotSpec {
"gcmtitle" -> "Category:SomeCategory",
"gcmlimit" -> "max",
"prop" -> "imageinfo",
"iiprop" -> "timestamp|user|comment",
"iiprop" -> "timestamp|user|comment|mime",
"continue" -> "gcmcontinue||",
"gcmcontinue" -> "file|44454d45524749373631312e4a50470a44454d45524749373631312e4a5047|32763876"
),
Expand All @@ -142,10 +54,10 @@ class PropImageInfoSpec extends Specification with MockBotSpec {
val future = bot
.page("Category:SomeCategory")
.imageInfoByGenerator(
"categorymembers",
"cm",
Set.empty,
Set("timestamp", "user", "comment")
generator = "categorymembers",
generatorPrefix = "cm",
namespaces = Set.empty,
props = Set("timestamp", "user", "comment", "mime")
)
.map(_.toSeq)

Expand Down Expand Up @@ -248,3 +160,100 @@ class PropImageInfoSpec extends Specification with MockBotSpec {
}
}
}

object PropImageInfoSpec {
private def response1(generatorPrefix: String = "cm") =
s""" {
| "query": {
| "pages": {
| "32885574": {
| "pageid": 32885574,
| "ns": 6,
| "title": "File:Dovbush-rocks 01.JPG",
| "imagerepository": "local",
| "imageinfo": [
| {
| "timestamp": "2014-05-20T20:54:33Z",
| "user": "Taras r",
| "size": 4270655,
| "width": 3648,
| "height": 2736,
| "mime": "image/jpeg",
| "url": "https://upload.wikimedia.org/wikipedia/commons/e/ea/Dovbush-rocks_01.JPG",
| "descriptionurl": "https://commons.wikimedia.org/wiki/File:Dovbush-rocks_01.JPG"
| }]}}},
| "continue": {
| "g${generatorPrefix}continue": "file|44454d45524749373631312e4a50470a44454d45524749373631312e4a5047|32763876",
| "continue": "g${generatorPrefix}continue||"}}
""".stripMargin

private val response2 =
""" {
| "query": {
| "pages": {
| "32885597": {
| "pageid": 32885597,
| "ns": 6,
| "title": "File:Dovbush-rocks 02.JPG",
| "imagerepository": "local",
| "imageinfo": [
| {
| "timestamp": "2014-05-20T20:55:12Z",
| "user": "Taras r",
| "size": 4537737,
| "width": 2736,
| "height": 3648,
| "mime": "image/jpeg",
| "url": "https://upload.wikimedia.org/wikipedia/commons/2/26/Dovbush-rocks_02.JPG",
| "descriptionurl": "https://commons.wikimedia.org/wiki/File:Dovbush-rocks_02.JPG"
| }]}}}}
| """.stripMargin

private val page1 = Page(
id = Some(32885574),
ns = Some(6),
title = "File:Dovbush-rocks 01.JPG",
revisions = Seq.empty,
images = Seq(
Image
.basic(
title = "File:Dovbush-rocks 01.JPG",
timestamp = Some(Timestamp.parse("2014-05-20T20:54:33Z")),
uploader = Some("Taras r"),
size = Some(4270655),
width = Some(3648),
height = Some(2736),
url = Some(
"https://upload.wikimedia.org/wikipedia/commons/e/ea/Dovbush-rocks_01.JPG"
),
pageUrl = Some("https://commons.wikimedia.org/wiki/File:Dovbush-rocks_01.JPG"),
pageId = Some(32885574)
)
.copy(mime = Some("image/jpeg"))
)
)

private val page2 = Page(
id = Some(32885597),
ns = Some(6),
title = "File:Dovbush-rocks 02.JPG",
revisions = Seq.empty,
images = Seq(
Image
.basic(
title = "File:Dovbush-rocks 02.JPG",
timestamp = Some(Timestamp.parse("2014-05-20T20:55:12Z")),
uploader = Some("Taras r"),
size = Some(4537737),
width = Some(2736),
height = Some(3648),
url = Some(
"https://upload.wikimedia.org/wikipedia/commons/2/26/Dovbush-rocks_02.JPG"
),
pageUrl = Some("https://commons.wikimedia.org/wiki/File:Dovbush-rocks_02.JPG"),
pageId = Some(32885597)
)
.copy(mime = Some("image/jpeg"))
)
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package org.scalawiki.wlx.stat.reports
import org.scalawiki.dto.markup.Table
import org.scalawiki.wlx.stat.ContestStat

import java.nio.file.{Files, Path, Paths}
import java.time.ZonedDateTime
import scala.io.Codec

class RecentlyTaken(val stat: ContestStat) extends Reporter {

Expand All @@ -15,8 +17,20 @@ class RecentlyTaken(val stat: ContestStat) extends Reporter {
.map { db =>
val images = db.images.filter { i =>
i.metadata.exists(_.date.exists(_.isAfter(jun30))) &&
!i.specialNominations.contains(s"WLM${contest.year}-UA-interior")
!i.specialNominations.contains(s"WLM${contest.year}-UA-interior")
}
// INSERT INTO selection (round_id, page_id, jury_id, rate)
// VALUES (1, 2, 3, 4)
// , (4, 5, 6, 7)
// , (7, 8, 9, 10);
val sql = "INSERT INTO selection (round_id, page_id, jury_id, rate) \n" +
images
.map { i =>
s"(1347, ${i.pageId.get}, 2036, 0)"
}
.mkString("VALUES ", ",\n", ";")
Files.write(Paths.get("RecentlyTaken.sql"), sql.getBytes(Codec.UTF8.charSet))

images.map { i =>
List(
s"[[:${i.title}]]",
Expand Down

0 comments on commit 49acd10

Please sign in to comment.