114 lines
3.3 KiB
Dart

// Copyright (C) 2022 WYATT GROUP
// Please see the AUTHORS file for details.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
import 'package:http/http.dart';
import 'package:webfeed/webfeed.dart';
import 'package:wyatt_medium_feeds/src/medium_article.dart';
class MediumFeed {
final String? username;
final bool mediumSubdomain;
final String? publicationName;
final String? customDomain;
final String? tagName;
final String? topicName;
final String url;
RssFeed? _feed;
RssFeed get feed {
if (_feed == null) {
throw StateError('Feed has not been parsed yet.');
}
return _feed!;
}
String get title => feed.title ?? '';
String get description => feed.description ?? '';
String get image => feed.image?.url ?? '';
List<MediumArticle> get articles {
final articles = <MediumArticle>[];
for (final i in feed.items ?? <RssItem>[]) {
articles.add(MediumArticle(i));
}
return articles;
}
MediumFeed.fromUsername(
this.username, {
bool subdomain = false,
String proxy = 'https://cros-anywhere.herokuapp.com/',
}) : mediumSubdomain = subdomain,
publicationName = null,
customDomain = null,
tagName = null,
topicName = null,
url = subdomain
? '${proxy}https://$username.medium.com/feed'
: '${proxy}https://medium.com/feed/@$username';
MediumFeed.fromPublicationName(
this.publicationName, {
String proxy = 'https://cros-anywhere.herokuapp.com/',
}) : username = null,
mediumSubdomain = false,
customDomain = null,
tagName = null,
topicName = null,
url = '${proxy}https://medium.com/feed/$publicationName';
MediumFeed.fromCustomDomain(
this.customDomain, {
String proxy = 'https://cros-anywhere.herokuapp.com/',
}) : username = null,
mediumSubdomain = false,
publicationName = null,
tagName = null,
topicName = null,
url = '${proxy}https://$customDomain/feed';
MediumFeed.fromTagName({
this.publicationName,
this.tagName,
String proxy = 'https://cros-anywhere.herokuapp.com/',
}) : username = null,
mediumSubdomain = false,
customDomain = null,
topicName = null,
url =
'${proxy}https://medium.com/feed/$publicationName/tagged/$tagName';
MediumFeed.fromTopicName(
this.topicName, {
String proxy = 'https://cros-anywhere.herokuapp.com/',
}) : username = null,
mediumSubdomain = false,
customDomain = null,
publicationName = null,
tagName = null,
url = '${proxy}https://medium.com/feed/tag/$topicName';
Future<RssFeed> parse() async {
final xmlString = await read(Uri.parse(url));
_feed = RssFeed.parse(xmlString);
return _feed!;
}
}