Commit d6ed3a44 authored by Mark Jordan's avatar Mark Jordan
Browse files

Initial commit.

parents
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
For more information, please refer to <http://unlicense.org>
<?php
/**
* Script that scrapes the pages from a newspaper managed by the
* Islandora Newspaper Solution Pack, fetches selected datastreams
* from each page, and saves them in an output directory that can
* then be used as the input for batch loading using the Islandora
* Newspaper Batch Ingest module.
*/
use Goutte\Client;
use Monolog\Logger;
use GuzzleHttp\Psr7;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Exception\ConnectException;
use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Exception\ServerException;
require_once __DIR__ . '/vendor/autoload.php';
$input_file = trim($argv[1]);
$output_dir = getcwd() . DIRECTORY_SEPARATOR . 'output';
mkdir($output_dir);
$datastreams = array('DC');
$path_to_log = $input_file . '.log';
$log = new Monolog\Logger('Islandora Newspaper Crawler');
$log_stream_handler= new Monolog\Handler\StreamHandler($path_to_log, Logger::INFO);
$log->pushHandler($log_stream_handler);
$log->addInfo("Crawl of PIDs from " . $input_file . " starting at ". date("F j, Y, g:i a"));
$client = new Client();
$pids = file($input_file);
foreach ($pids as $pid) {
$page_num = 0;
$issue_url = "http://chinesetimes.lib.sfu.ca/islandora/object/". trim($pid);
$dc_xml = fetch_datastream_content($issue_url . '/datastream/DC');
$date = get_date_from_dc($dc_xml);
$issue_dir = $output_dir . DIRECTORY_SEPARATOR . $date;
mkdir($issue_dir);
print "Harvesting content for $issue_url ($date)\n";
$crawler = $client->request('GET', $issue_url);
$hrefs = $crawler->filter('dt.islandora-object-thumb > a > img')->extract(array('src'));
foreach ($hrefs as &$url) {
$page_num++;
if ($page_num < 10) {
$page_num = '0' . $page_num;
}
$url = preg_replace('#/datastream/TN/view#', '', $url);
$jpeg_content = fetch_datastream_content($url . '/datastream/JPEG');
$jpeg_path = $issue_dir . DIRECTORY_SEPARATOR . 'page_' . $page_num . '.jpg';
file_put_contents($jpeg_path, $jpeg_content);
$ocr_content = fetch_datastream_content($url . '/datastream/OCR');
$ocr_path = $issue_dir . DIRECTORY_SEPARATOR . 'page_' . $page_num . '.txt';
file_put_contents($ocr_path, $ocr_content);
}
// print_r($hrefs);
}
/*
To get date, fetch http://chinesetimes.lib.sfu.ca/islandora/object/ctimes:55273/datastream/DC and parse out dc.date matching yyyy-mm-dd.
*/
/**
* Gets all of the datastream content files and save them in a directory.
*
* @param string $url
* The datastream's URL.
*/
function fetch_datastream_content($url) {
global $log;
$client = new GuzzleHttp\Client();
$ds_response = $client->request('GET', $url, [
'headers' => [
// 'X-Authorization-User' => $user . ':' . $token,
]
]);
return $ds_response->getBody();
}
/**
* Gets the value of the first dc.date element.
*
* @param string $dc_xml
* The contents of the DC.xml file.
*
* @return string
* A label for the object.
*/
function get_date_from_dc($dc_xml) {
$dom = new DOMDocument();
$dom->loadXML($dc_xml);
$xpath = new DOMXPath($dom);
$xpath->registerNamespace('dc', 'http://purl.org/dc/elements/1.1/');
$dates = $xpath->query("//dc:date");
if ($dates->length > 0) {
$date = $dates->item(0)->nodeValue;
}
else {
$date = '[No date available]';
}
return $date;
}
<?php
/**
* Script that scrapes the PIDs for all issues in a newspaper from the
* standard issue list produced by the Islandora Newspaper Solution Pack.
*
* Usage: php newspaper.php > out.txt
*/
use Goutte\Client;
require_once __DIR__ . '/vendor/autoload.php';
$client = new Client();
$url = "http://chinesetimes.lib.sfu.ca/";
$crawler = $client->request('GET', $url);
$pids = $crawler->filter('div.islandora-newspaper-content a')->extract(array('href'));
foreach ($pids as &$pid) {
$pid = preg_replace("#/chinese-times#", '', $pid);
$pid = preg_replace("#\-#", ':', $pid);
$pid = preg_replace("#/#", '', $pid);
print $pid . "\n";
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment