Skip to content

A free, self-hostable news aggregator…

Issue #9166: Detect JSON Feed URLs when subscribing via the API (#9167)

John Brayton551427ce388c
diff --git a/CREDITS.md b/CREDITS.md
index d2e09fc74..381d27370 100644
--- a/CREDITS.md
+++ b/CREDITS.md
@@ -146,6 +146,7 @@ People are sorted by name so please keep this order.
* [jlefler](https://github.com/jlefler): [contributions](https://github.com/FreshRSS/FreshRSS/pulls?q=is:pr+author:jlefler)
* [Joe Stump](https://github.com/joestump): [contributions](https://github.com/FreshRSS/FreshRSS/pulls?q=is:pr+author:joestump), [Web](http://stu.mp)
* [Joel Garcia](https://github.com/joelchrono12): [contributions](https://github.com/FreshRSS/FreshRSS/pulls?q=is:pr+author:joelchrono12), [Web](https://joelchrono12.xyz)
+* [John Brayton](https://github.com/jbrayton): [contributions](https://github.com/FreshRSS/FreshRSS/pulls?q=is:pr+author:jbrayton), [Web](https://www.virtualsanity.com)
* [Jonas Östanbäck](https://github.com/cez81): [contributions](https://github.com/FreshRSS/FreshRSS/pulls?q=is:pr+author:cez81)
* [Jordi Garcia](https://github.com/jgtorcal): [contributions](https://github.com/FreshRSS/FreshRSS/pulls?q=is:pr+author:jgtorcal)
* [Joris Kinable](https://github.com/jkinable): [contributions](https://github.com/FreshRSS/FreshRSS/pulls?q=is:pr+author:jkinable)
diff --git a/p/api/greader.php b/p/api/greader.php
index 8fb9780f6..74c97407a 100644
--- a/p/api/greader.php
+++ b/p/api/greader.php
@@ -451,7 +451,8 @@ final class GReaderAPI {
if ($feedId <= 0) {
$http_auth = '';
try {
- FreshRSS_feed_Controller::addFeed($streamUrl, $title, $addCatId, '', $http_auth);
+ $kind = self::detectFeedKind($streamUrl);
+ FreshRSS_feed_Controller::addFeed($streamUrl, $title, $addCatId, '', $http_auth, [], $kind);
continue 2;
} catch (Exception $e) {
Minz_Log::error('subscriptionEdit error subscribe: ' . $e->getMessage(), API_LOG);
@@ -482,13 +483,23 @@ final class GReaderAPI {
exit('OK');
}
+ /**
+ * Guess the kind of feed (RSS/ATOM vs. JSON) based on URL.
+ * The Google Reader API does not provide any way for the client to specify the feed format.
+ */
+ private static function detectFeedKind(string $url): int {
+ return preg_match('/(?:\b|_)json(?:\b|_)/i', $url) === 1
+ ? FreshRSS_Feed::KIND_JSONFEED
+ : FreshRSS_Feed::KIND_RSS;
+ }
+
private static function quickadd(string $url): never {
try {
$url = htmlspecialchars($url, ENT_COMPAT, 'UTF-8');
if (str_starts_with($url, 'feed/')) {
$url = substr($url, 5);
}
- $feed = FreshRSS_feed_Controller::addFeed($url);
+ $feed = FreshRSS_feed_Controller::addFeed($url, kind: self::detectFeedKind($url));
exit(json_encode([
'numResults' => 1,
'query' => $feed->url(),