Ammonite/Статистика відвідувань вікісторінок: відмінності між версіями

Вилучено вміст Додано вміст
Ilya (обговорення | внесок)
Ilya (обговорення | внесок)
Рядок 312:
import scala.io.Source
 
defcase getMonthlyViewsclass PageViews(projectval name: String, year:val Int, monthviews: Int): List[(String, Int)] = {
 
val monthStr = (if (month < 10) "0" else "") + month
def getMonthlyViews(project: String, year: Int, month: Int): List[PageViews] = {
val url = s"https://wikimedia.org/api/rest_v1/metrics/pageviews/top/$project/all-access/$year/$monthStr/all-days"
def url: String = {
val monthStr = (if (month < 10) "0" else "") + month
val url = s"https://wikimedia.org/api/rest_v1/metrics/pageviews/top/$project/all-access/$year/$monthStr/all-days"
}
val text = Source.fromURL(url).mkString
val doc = parse(text).getOrElse(Json.Null)
val articles = root.items.each.articles.each.article.string.getAll(doc)
val views = root.items.each.articles.each.views.int.getAll(doc)
articles.zip(views).map{ case (name, views) => new PageViews(name, views) }
}
 
val monthly = (1 to 12).map(month => getMonthlyViews("uk.wikibooks.org", 2016, month))
 
val groupedByArticle = monthly.flatten.groupBy{case (_.article, views) => article}
val articleToViews = groupedByArticle.mapValues{seq => seq(_.map{case (article, _.views) => views}.sum})
 
val yearlyByBookSeq = articleToViews.toSeq.map{case (article, views)pv => pv.copy(articlename = name.split("/").head, views)) }
 
val groupedByBook = yearlyByBookSeq.groupBy{ case (book, views) => _.article })
val bookToViews = groupedByBook.mapValues{seq => seq(_.map{case (book, views_views) => views}.sum})
 
val orderedBookViews = bookToViews.toSeq.sortBy{ case (book,- _.views) => -views}
</source>