Skip to content

Instantly share code, notes, and snippets.

@lss233

lss233/feed.php Secret

Created February 23, 2021 08:09
Show Gist options
  • Save lss233/bd48efbf497f2a7c353dd78f7f247b47 to your computer and use it in GitHub Desktop.
Save lss233/bd48efbf497f2a7c353dd78f7f247b47 to your computer and use it in GitHub Desktop.
Feed retrive API
<?php
set_time_limit(0);
header('Content-Type: application/json');
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Headers: *");
define('INVALID', '<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:atom="http://www.w3.org/2005/Atom" xmlns:georss="http://www.georss.org/georss" xmlns:twitter="http://api.twitter.com" xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0">
<channel>
</channel>
</rss>');
function getRSS($url){
$response = file_get_contents($url);
if (($header = array_values(preg_grep($pattern, $http_response_header))) &&
(preg_match($pattern, $header[0], $match) !== false))
{
$content_type = $match[1];
if(strpos($content_type, 'xml') !== false){
return $response;
}
}
return INVALID;
}
$rss = [];
$feeds = [];
if(isset($_GET['twitter'])){
$rss['twitter'] = getRSS('http://twitrss.me/twitter_user_to_rss/?user='.$_GET['twitter']);
}
if(isset($_GET['instagram'])){
$rss['instagram'] = getRSS('http://rsshub.app/instagram/user/'.$_GET['instagram']);
}
if(isset($_GET['ncm'])){
$rss['ncm'] = getRSS('http://rsshub.app/ncm/user/playlist/'.$_GET['ncm']);
}
if(isset($_GET['github'])){
$rss['github'] = getRSS('http://feedmix.novaclic.com/atom2rss.php?source=https://github.com/'.$_GET['github'].'.atom');
}
if(isset($_GET['telegram'])){
$rss['telegram'] = getRSS('http://rsshub.app/telegram/channel/'.$_GET['telegram']);
}
if(isset($_GET['dryrun'])){
$dryrun = true;
};
foreach ($rss as $key => $xml) {
try{
$node = new SimpleXMLElement($xml);
foreach($node->channel->item as $item){
$counter ++;
array_push($feeds, [
'source' => $key,
'title' => (string)$item->title,
'description' => (string)$item->description,
'date' => (string)$item->pubDate,
'link' => (string)$item->link
]);
}
} catch(Exception $ignored){}
}
function date_compare($a, $b)
{
$t1 = strtotime($a['date']);
$t2 = strtotime($b['date']);
return $t2 - $t1;
}
usort($feeds, 'date_compare');
print(json_encode($feeds));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment