Created
April 23, 2017 19:42
-
-
Save woganmay/57703eaf0243ad207317e589d300cef6 to your computer and use it in GitHub Desktop.
PHP code for downloading an entire ATOM timeline
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// composer require phpoffice/phpexcel | |
require 'vendor/autoload.php'; | |
// Webfinger | |
$url = "https://wogan.im/@wogan"; | |
/** | |
* Start Webfinger lookup | |
*/ | |
echo "Scanning: $url\n"; | |
$parsed = parse_url($url); | |
$webfinger = sprintf("%s://%s/.well-known/webfinger?resource=%s", $parsed['scheme'], $parsed['host'], urlencode($url)); | |
$json = file_get_contents($webfinger); | |
$result = json_decode($json); | |
/** | |
* Get ATOM feed | |
*/ | |
$atom_link = ""; | |
foreach($result->links as $link) | |
{ | |
if ($link->type == "application/atom+xml") | |
{ | |
$atom_link = $link->href; | |
} | |
} | |
/** | |
* Pull all pages of ATOM feed | |
*/ | |
$resolved = false; | |
$items = []; | |
while(!$resolved) | |
{ | |
echo "Fetching: $atom_link\n"; | |
$content = file_get_contents($atom_link); | |
$xml = simplexml_load_string($content); | |
foreach($xml->entry as $item) | |
{ | |
$self = ""; | |
$mentions = []; | |
foreach($item->link as $link) | |
{ | |
$link = (array)$link; | |
switch($link["@attributes"]['rel']) | |
{ | |
case "mentioned": $mentions[] = $link["@attributes"]["href"]; break; | |
case "self": $self = $link["@attributes"]["href"]; break; | |
} | |
} | |
// At this point you could do anything with this data | |
$items[] = [ | |
$item->id, | |
$item->published, | |
$self, | |
implode(", ", $mentions), | |
$item->content | |
]; | |
} | |
// Find Next link | |
$nextLinkSet = false; | |
foreach($xml->link as $link) | |
{ | |
$link = (array)$link; | |
if ($link["@attributes"]['rel'] == "next") { | |
$atom_link = $link['@attributes']['href']; | |
$nextLinkSet = true; | |
} | |
} | |
if (!$nextLinkSet) $resolved = true; | |
} | |
echo count($items) . " items found\n"; | |
/** | |
* Generate XLSX file from $items | |
*/ | |
$objPHPExcel = new PHPExcel(); | |
$activeSheet = $objPHPExcel->setActiveSheetIndex(0); | |
$activeSheet->setCellValue('A1', "ID"); | |
$activeSheet->setCellValue('B1', "Date Published"); | |
$activeSheet->setCellValue('C1', "Self URL"); | |
$activeSheet->setCellValue('D1', "Mentions"); | |
$activeSheet->setCellValue('E1', "HTML Content"); | |
for($i = 0; $i < count($items); $i++) | |
{ | |
$rc = $i + 2; | |
$activeSheet->setCellValue('A'.$rc, $items[$i][0]); | |
$activeSheet->setCellValue('B'.$rc, $items[$i][1]); | |
$activeSheet->setCellValue('C'.$rc, $items[$i][2]); | |
$activeSheet->setCellValue('D'.$rc, $items[$i][3]); | |
$activeSheet->setCellValue('E'.$rc, $items[$i][4]); | |
} | |
// Rename worksheet | |
$objPHPExcel->getActiveSheet()->setTitle('Export'); | |
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007'); | |
$objWriter->save('atomfeed.xlsx'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment