-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
545 additions
and
4 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpFoundation\Response; | ||
|
||
class Feeds { | ||
|
||
public $http; | ||
private $_pretty = false; | ||
|
||
public function __construct() { | ||
$this->http = new p3k\HTTP(); | ||
} | ||
|
||
private function respond(Response $response, $code, $params, $headers=[]) { | ||
$response->setStatusCode($code); | ||
foreach($headers as $k=>$v) { | ||
$response->headers->set($k, $v); | ||
} | ||
$response->headers->set('Content-Type', 'application/json'); | ||
$opts = JSON_UNESCAPED_SLASHES; | ||
if($this->_pretty) $opts += JSON_PRETTY_PRINT; | ||
$response->setContent(json_encode($params, $opts)."\n"); | ||
return $response; | ||
} | ||
|
||
public function find(Request $request, Response $response) { | ||
$opts = []; | ||
|
||
if($request->get('timeout')) { | ||
// We might make 2 HTTP requests, so each request gets half the desired timeout | ||
$opts['timeout'] = $request->get('timeout') / 2; | ||
} | ||
|
||
if($request->get('max_redirects')) { | ||
$opts['max_redirects'] = (int)$request->get('max_redirects'); | ||
} | ||
|
||
if($request->get('pretty')) { | ||
$this->_pretty = true; | ||
} | ||
|
||
$url = $request->get('url'); | ||
|
||
if(!$url) { | ||
return $this->respond($response, 400, [ | ||
'error' => 'missing_url', | ||
'error_description' => 'Provide a URL to fetch' | ||
]); | ||
} | ||
|
||
$xray = new p3k\XRay(); | ||
$xray->http = $this->http; | ||
$res = $xray->feeds($url, $opts); | ||
|
||
return $this->respond($response, !empty($res['error']) ? 400 : 200, $res); | ||
} | ||
|
||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
<?php | ||
namespace p3k\XRay; | ||
|
||
use p3k\XRay\Formats; | ||
|
||
class Feeds { | ||
private $http; | ||
|
||
public function __construct($http) { | ||
$this->http = $http; | ||
} | ||
|
||
public function find($url, $opts=[]) { | ||
if(isset($opts['timeout'])) | ||
$this->http->set_timeout($opts['timeout']); | ||
if(isset($opts['max_redirects'])) | ||
$this->http->set_max_redirects($opts['max_redirects']); | ||
|
||
$scheme = parse_url($url, PHP_URL_SCHEME); | ||
if(!in_array($scheme, ['http','https'])) { | ||
return [ | ||
'error' => 'invalid_url', | ||
'error_description' => 'Only http and https URLs are supported' | ||
]; | ||
} | ||
|
||
$host = parse_url($url, PHP_URL_HOST); | ||
if(!$host) { | ||
return [ | ||
'error' => 'invalid_url', | ||
'error_description' => 'The URL provided was not valid' | ||
]; | ||
} | ||
|
||
$url = normalize_url($url); | ||
|
||
$result = $this->http->get($url); | ||
$body = $result['body']; | ||
|
||
$feeds = []; | ||
|
||
// First check the content type of the response | ||
$contentType = isset($result['headers']['Content-Type']) ? $result['headers']['Content-Type'] : ''; | ||
|
||
if(is_array($contentType)) | ||
$contentType = $contentType[count($contentType)-1]; | ||
|
||
if(strpos($contentType, 'application/atom+xml') !== false) { | ||
$feeds[] = [ | ||
'url' => $result['url'], | ||
'type' => 'atom' | ||
]; | ||
} elseif(strpos($contentType, 'application/rss+xml') !== false) { | ||
$feeds[] = [ | ||
'url' => $result['url'], | ||
'type' => 'rss' | ||
]; | ||
} elseif(strpos($contentType, 'application/json') !== false | ||
&& substr($body, 0, 1) == '{' && strpos(substr($body, 0, 100), 'https://jsonfeed.org/version/1')) { | ||
$feeds[] = [ | ||
'url' => $result['url'], | ||
'type' => 'jsonfeed' | ||
]; | ||
} else { | ||
// Some other document was returned, parse the HTML and look for rel alternates and Microformats | ||
|
||
$mf2 = \mf2\Parse($body, $result['url']); | ||
if(isset($mf2['alternates'])) { | ||
foreach($mf2['alternates'] as $alt) { | ||
if(strpos($alt['type'], 'application/json') !== false) { | ||
$feeds[] = [ | ||
'url' => $alt['url'], | ||
'type' => 'jsonfeed' | ||
]; | ||
} | ||
if(strpos($alt['type'], 'application/atom+xml') !== false) { | ||
$feeds[] = [ | ||
'url' => $alt['url'], | ||
'type' => 'atom' | ||
]; | ||
} | ||
if(strpos($alt['type'], 'application/rss+xml') !== false) { | ||
$feeds[] = [ | ||
'url' => $alt['url'], | ||
'type' => 'rss' | ||
]; | ||
} | ||
} | ||
} | ||
|
||
$parsed = Formats\HTML::parse($this->http, $body, $result['url'], array_merge($opts, ['expect'=>'feed'])); | ||
if($parsed && isset($parsed['data']['type']) && $parsed['data']['type'] == 'feed') { | ||
$feeds[] = [ | ||
'url' => $result['url'], | ||
'type' => 'microformats' | ||
]; | ||
} | ||
} | ||
|
||
// Sort feeds by priority | ||
$rank = ['microformats'=>0,'jsonfeed'=>1,'atom'=>2,'rss'=>3]; | ||
usort($feeds, function($a, $b) use($rank) { | ||
return $rank[$a['type']] > $rank[$b['type']]; | ||
}); | ||
|
||
return [ | ||
'url' => $result['url'], | ||
'code' => $result['code'], | ||
'feeds' => $feeds | ||
]; | ||
} | ||
|
||
} |
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
Oops, something went wrong.