Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extract Image Links DF API + Test #221

Merged
merged 4 commits into from
May 15, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/main/scala/io/archivesunleashed/DataFrameLoader.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,9 @@ class DataFrameLoader(sc: SparkContext) {
RecordLoader.loadArchives(path, sc)
.extractHyperlinksDF()
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a doc comment here.

def extractImageLinks(path: String): DataFrame = {
RecordLoader.loadArchives(path, sc)
.extractImageLinksDF()
}
}
20 changes: 19 additions & 1 deletion src/main/scala/io/archivesunleashed/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package io

import io.archivesunleashed.data.{ArchiveRecordWritable, ArchiveRecordInputFormat}
import ArchiveRecordWritable.ArchiveFormat
import io.archivesunleashed.matchbox.{DetectLanguage, ExtractDate, ExtractLinks, ExtractDomain, RemoveHTML}
import io.archivesunleashed.matchbox.{DetectLanguage, ExtractDate, ExtractLinks, ExtractImageLinks, ExtractDomain, RemoveHTML}
import io.archivesunleashed.matchbox.ExtractDate.DateComponent
import io.archivesunleashed.matchbox.ExtractDate.DateComponent._

Expand Down Expand Up @@ -120,6 +120,24 @@ package object archivesunleashed {
sqlContext.getOrCreate().createDataFrame(records, schema)
}

def extractImageLinksDF(): DataFrame = {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need a doc comment.

val records = rdd
.keepValidPages()
.flatMap(r => {
val src = r.getUrl
val imageUrls = ExtractImageLinks(src, r.getContentString)
imageUrls.map(url => (src, url))
})
.map(t => Row(t._1, t._2))

val schema = new StructType()
.add(StructField("Src", StringType, true))
.add(StructField("ImageUrl", StringType, true))

val sqlContext = SparkSession.builder();
sqlContext.getOrCreate().createDataFrame(records, schema)
}

/** Removes all data except images. */
def keepImages() = {
rdd.filter(r =>
Expand Down
73 changes: 73 additions & 0 deletions src/test/scala/io/archivesunleashed/df/ExtractImageLinksTest.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* Archives Unleashed Toolkit (AUT):
* An open-source platform for analyzing web archives.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.archivesunleashed

import com.google.common.io.Resources
import io.archivesunleashed.df._
import org.apache.spark.sql.SparkSession
import org.apache.spark.sql.functions._
import org.apache.spark.{SparkConf, SparkContext}
import org.junit.runner.RunWith
import org.scalatest.junit.JUnitRunner
import org.scalatest.{BeforeAndAfter, FunSuite}

@RunWith(classOf[JUnitRunner])
class ExtractImageLinksTest extends FunSuite with BeforeAndAfter {
private val arcPath = Resources.getResource("arc/example.arc.gz").getPath
private val master = "local[4]"
private val appName = "example-df"
private var sc: SparkContext = _

before {
val conf = new SparkConf()
.setMaster(master)
.setAppName(appName)
sc = new SparkContext(conf)
}

test("Fetch image links") {
val df = RecordLoader.loadArchives(arcPath, sc)
.extractImageLinksDF()

// We need this in order to use the $-notation
val spark = SparkSession.builder().master("local").getOrCreate()
import spark.implicits._

val extracted = df.select($"Src".as("Domain"), $"ImageUrl".as("Image"))
.orderBy(desc("Image")).head(2).toList
assert(extracted.size == 2)
assert("http://www.archive.org/index.php" == extracted(0)(0))
assert("http://www.archive.org/services/get-item-image.php?identifier=zh27814&collection=zh27&mediatype=audio" == extracted(0)(1))
assert("http://www.archive.org/index.php" == extracted(1)(0))
assert("http://www.archive.org/services/get-item-image.php?identifier=secretarmiesb00spivrich&collection=americana&mediatype=texts" == extracted(1)(1))
// Results should be:
// +------------------+-----+
// | Domain|count|
// +------------------+-----+
// | www.archive.org| 132|
// | deadlists.com| 2|
// |www.hideout.com.br| 1|
// +------------------+-----+
}

after {
if (sc != null) {
sc.stop()
}
}
}