tsacdop-podcast-app-android/lib/local_storage/sqflite_localpodcast.dart

1227 lines
44 KiB
Dart
Raw Normal View History

2020-02-09 13:29:09 +01:00
import 'dart:async';
2020-08-10 20:41:22 +02:00
import 'dart:developer' as developer;
2020-08-15 19:43:45 +02:00
import 'dart:io';
2020-07-26 12:20:42 +02:00
import 'package:dio/dio.dart';
2020-07-26 12:20:42 +02:00
import 'package:flutter_downloader/flutter_downloader.dart';
import 'package:intl/intl.dart';
import 'package:path/path.dart';
import 'package:sqflite/sqflite.dart';
2020-07-26 15:48:30 +02:00
import 'package:webfeed/webfeed.dart';
2020-07-26 12:20:42 +02:00
2020-05-06 18:50:32 +02:00
import '../type/episodebrief.dart';
2020-07-26 12:20:42 +02:00
import '../type/play_histroy.dart';
import '../type/podcastlocal.dart';
2020-05-06 18:50:32 +02:00
import '../type/sub_history.dart';
2020-02-09 13:29:09 +01:00
2020-07-23 20:42:25 +02:00
enum Filter { downloaded, liked, search, all }
2020-02-09 13:29:09 +01:00
class DBHelper {
static Database _db;
Future<Database> get database async {
if (_db != null) return _db;
_db = await initDb();
return _db;
}
initDb() async {
var documentsDirectory = await getDatabasesPath();
2020-07-26 12:20:42 +02:00
var path = join(documentsDirectory, "podcasts.db");
var theDb = await openDatabase(path,
2020-06-07 20:42:27 +02:00
version: 3, onCreate: _onCreate, onUpgrade: _onUpgrade);
2020-02-09 13:29:09 +01:00
return theDb;
}
void _onCreate(Database db, int version) async {
await db
.execute("""CREATE TABLE PodcastLocal(id TEXT PRIMARY KEY,title TEXT,
2020-03-31 18:36:20 +02:00
imageUrl TEXT,rssUrl TEXT UNIQUE, primaryColor TEXT, author TEXT,
2020-03-01 13:17:06 +01:00
description TEXT, add_date INTEGER, imagePath TEXT, provider TEXT, link TEXT,
2020-07-13 08:56:05 +02:00
background_image TEXT DEFAULT '', hosts TEXT DEFAULT '',update_count INTEGER DEFAULT 0,
2020-06-07 20:42:27 +02:00
episode_count INTEGER DEFAULT 0, skip_seconds INTEGER DEFAULT 0, auto_download INTEGER DEFAULT 0)""");
2020-02-09 13:29:09 +01:00
await db
.execute("""CREATE TABLE Episodes(id INTEGER PRIMARY KEY,title TEXT,
enclosure_url TEXT UNIQUE, enclosure_length INTEGER, pubDate TEXT,
description TEXT, feed_id TEXT, feed_link TEXT, milliseconds INTEGER,
2020-02-09 13:29:09 +01:00
duration INTEGER DEFAULT 0, explicit INTEGER DEFAULT 0, liked INTEGER DEFAULT 0,
2020-04-11 19:23:12 +02:00
liked_date INTEGER DEFAULT 0, downloaded TEXT DEFAULT 'ND', download_date INTEGER DEFAULT 0, media_id TEXT,
is_new INTEGER DEFAULT 0)""");
await db.execute(
2020-04-11 19:23:12 +02:00
"""CREATE TABLE PlayHistory(id INTEGER PRIMARY KEY, title TEXT, enclosure_url TEXT,
seconds REAL, seek_value REAL, add_date INTEGER, listen_time INTEGER DEFAULT 0)""");
await db.execute(
"""CREATE TABLE SubscribeHistory(id TEXT PRIMARY KEY, title TEXT, rss_url TEXT UNIQUE,
add_date INTEGER, remove_date INTEGER DEFAULT 0, status INTEGER DEFAULT 0)""");
2020-02-09 13:29:09 +01:00
}
2020-04-11 19:23:12 +02:00
void _onUpgrade(Database db, int oldVersion, int newVersion) async {
if (oldVersion == 1) {
await db.execute(
2020-06-07 20:42:27 +02:00
"ALTER TABLE PodcastLocal ADD skip_seconds INTEGER DEFAULT 0 ");
await db.execute(
"ALTER TABLE PodcastLocal ADD auto_download INTEGER DEFAULT 0");
} else if (oldVersion == 2) {
await db.execute(
"ALTER TABLE PodcastLocal ADD auto_download INTEGER DEFAULT 0");
2020-04-11 19:23:12 +02:00
}
}
Future<List<PodcastLocal>> getPodcastLocal(List<String> podcasts) async {
var dbClient = await database;
2020-07-26 12:20:42 +02:00
var podcastLocal = <PodcastLocal>[];
2020-07-13 09:41:59 +02:00
for (var s in podcasts) {
List<Map> list;
list = await dbClient.rawQuery(
2020-03-31 18:36:20 +02:00
"""SELECT id, title, imageUrl, rssUrl, primaryColor, author, imagePath , provider,
link ,update_count, episode_count FROM PodcastLocal WHERE id = ?""",
[s]);
2020-07-26 12:20:42 +02:00
if (list.length > 0) {
2020-04-26 07:41:25 +02:00
podcastLocal.add(PodcastLocal(
list.first['title'],
list.first['imageUrl'],
list.first['rssUrl'],
list.first['primaryColor'],
list.first['author'],
list.first['id'],
list.first['imagePath'],
list.first['provider'],
list.first['link'],
upateCount: list.first['update_count'],
episodeCount: list.first['episode_count']));
2020-07-26 12:20:42 +02:00
}
2020-07-13 09:41:59 +02:00
}
return podcastLocal;
}
Future<List<PodcastLocal>> getPodcastLocalAll() async {
2020-02-09 13:29:09 +01:00
var dbClient = await database;
List<Map> list = await dbClient.rawQuery(
2020-03-01 13:17:06 +01:00
'SELECT id, title, imageUrl, rssUrl, primaryColor, author, imagePath, provider, link FROM PodcastLocal ORDER BY add_date DESC');
2020-07-26 12:20:42 +02:00
var podcastLocal = <PodcastLocal>[];
2020-07-13 08:56:05 +02:00
for (var i in list) {
podcastLocal.add(PodcastLocal(
2020-07-13 08:56:05 +02:00
i['title'],
i['imageUrl'],
i['rssUrl'],
i['primaryColor'],
i['author'],
i['id'],
i['imagePath'],
list.first['provider'],
list.first['link']));
2020-02-09 13:29:09 +01:00
}
return podcastLocal;
}
Future<int> getPodcastCounts(String id) async {
2020-03-31 18:36:20 +02:00
var dbClient = await database;
2020-04-26 07:41:25 +02:00
List<Map> list = await dbClient
.rawQuery('SELECT episode_count FROM PodcastLocal WHERE id = ?', [id]);
return list.first['episode_count'];
}
Future<int> getPodcastUpdateCounts(String id) async {
var dbClient = await database;
List<Map> list = await dbClient.rawQuery(
'SELECt count(*) as count FROM Episodes WHERE feed_id = ? AND is_new = 1',
[id]);
return list.first['count'];
2020-03-31 18:36:20 +02:00
}
Future<int> getSkipSeconds(String id) async {
var dbClient = await database;
List<Map> list = await dbClient
.rawQuery('SELECT skip_seconds FROM PodcastLocal WHERE id = ?', [id]);
return list.first['skip_seconds'];
}
Future<int> saveSkipSeconds(String id, int seconds) async {
var dbClient = await database;
return await dbClient.rawUpdate(
"UPDATE PodcastLocal SET skip_seconds = ? WHERE id = ?", [seconds, id]);
}
2020-06-07 20:42:27 +02:00
Future<bool> getAutoDownload(String id) async {
var dbClient = await database;
List<Map> list = await dbClient
.rawQuery('SELECT auto_download FROM PodcastLocal WHERE id = ?', [id]);
return list.first['auto_download'] == 1;
}
2020-07-26 12:20:42 +02:00
Future<int> saveAutoDownload(String id, {bool boo}) async {
2020-06-07 20:42:27 +02:00
var dbClient = await database;
return await dbClient.rawUpdate(
"UPDATE PodcastLocal SET auto_download = ? WHERE id = ?",
[boo ? 1 : 0, id]);
}
Future<String> checkPodcast(String url) async {
var dbClient = await database;
List<Map> list = await dbClient
.rawQuery('SELECT id FROM PodcastLocal WHERE rssUrl = ?', [url]);
if (list.isEmpty) return '';
return list.first['id'];
}
2020-02-09 13:29:09 +01:00
Future savePodcastLocal(PodcastLocal podcastLocal) async {
2020-07-26 12:20:42 +02:00
var _milliseconds = DateTime.now().millisecondsSinceEpoch;
2020-02-09 13:29:09 +01:00
var dbClient = await database;
await dbClient.transaction((txn) async {
await txn.rawInsert(
"""INSERT OR IGNORE INTO PodcastLocal (id, title, imageUrl, rssUrl,
2020-03-01 13:17:06 +01:00
primaryColor, author, description, add_date, imagePath, provider, link) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)""",
2020-02-09 13:29:09 +01:00
[
podcastLocal.id,
2020-02-09 13:29:09 +01:00
podcastLocal.title,
podcastLocal.imageUrl,
podcastLocal.rssUrl,
podcastLocal.primaryColor,
podcastLocal.author,
podcastLocal.description,
_milliseconds,
podcastLocal.imagePath,
podcastLocal.provider,
podcastLocal.link
2020-02-09 13:29:09 +01:00
]);
await txn.rawInsert(
"""REPLACE INTO SubscribeHistory(id, title, rss_url, add_date) VALUES (?, ?, ?, ?)""",
[
podcastLocal.id,
podcastLocal.title,
podcastLocal.rssUrl,
_milliseconds
]);
});
2020-02-09 13:29:09 +01:00
}
2020-03-01 13:17:06 +01:00
Future<int> saveFiresideData(List<String> list) async {
var dbClient = await database;
2020-07-26 12:20:42 +02:00
var result = await dbClient.rawUpdate(
2020-03-01 13:17:06 +01:00
'UPDATE PodcastLocal SET background_image = ? , hosts = ? WHERE id = ?',
[list[1], list[2], list[0]]);
return result;
}
2020-03-01 13:17:06 +01:00
Future<List<String>> getFiresideData(String id) async {
var dbClient = await database;
List<Map> list = await dbClient.rawQuery(
2020-03-01 13:17:06 +01:00
'SELECT background_image, hosts FROM PodcastLocal WHERE id = ?', [id]);
if (list.length > 0) {
2020-07-26 12:20:42 +02:00
var data = <String>[list.first['background_image'], list.first['hosts']];
return data;
}
return ['', ''];
}
Future delPodcastLocal(String id) async {
2020-02-09 13:29:09 +01:00
var dbClient = await database;
await dbClient.rawDelete('DELETE FROM PodcastLocal WHERE id =?', [id]);
2020-02-09 13:29:09 +01:00
List<Map> list = await dbClient.rawQuery(
"""SELECT downloaded FROM Episodes WHERE downloaded != 'ND' AND feed_id = ?""",
[id]);
2020-07-13 08:56:05 +02:00
for (var i in list) {
2020-07-26 12:20:42 +02:00
if (i != null) {
await FlutterDownloader.remove(
2020-07-13 08:56:05 +02:00
taskId: i['downloaded'], shouldDeleteContent: true);
2020-07-26 12:20:42 +02:00
}
2020-02-09 13:29:09 +01:00
}
await dbClient.rawDelete('DELETE FROM Episodes WHERE feed_id=?', [id]);
2020-07-26 12:20:42 +02:00
var _milliseconds = DateTime.now().millisecondsSinceEpoch;
await dbClient.rawUpdate(
"""UPDATE SubscribeHistory SET remove_date = ? , status = ? WHERE id = ?""",
[_milliseconds, 1, id]);
2020-02-09 13:29:09 +01:00
}
Future<int> saveHistory(PlayHistory history) async {
var dbClient = await database;
2020-07-26 12:20:42 +02:00
var _milliseconds = DateTime.now().millisecondsSinceEpoch;
var recent = await getPlayHistory(1);
2020-04-11 19:23:12 +02:00
if (recent.length == 1) {
if (recent.first.url == history.url) {
await dbClient.rawDelete("DELETE FROM PlayHistory WHERE add_date = ?",
[recent.first.playdate.millisecondsSinceEpoch]);
}
}
2020-07-26 12:20:42 +02:00
var result = await dbClient.transaction((txn) async {
return await txn.rawInsert(
2020-04-11 19:23:12 +02:00
"""REPLACE INTO PlayHistory (title, enclosure_url, seconds, seek_value, add_date, listen_time)
VALUES (?, ?, ?, ?, ?, ?) """,
[
history.title,
history.url,
history.seconds,
history.seekValue,
2020-04-11 19:23:12 +02:00
_milliseconds,
history.seekValue > 0.95 ? 1 : 0
]);
});
return result;
}
Future<List<PlayHistory>> getPlayHistory(int top) async {
2020-03-01 13:17:06 +01:00
var dbClient = await database;
List<Map> list = await dbClient.rawQuery(
"""SELECT title, enclosure_url, seconds, seek_value, add_date FROM PlayHistory
ORDER BY add_date DESC LIMIT ?
2020-04-11 19:23:12 +02:00
""", [top]);
2020-07-26 12:20:42 +02:00
var playHistory = <PlayHistory>[];
2020-07-13 09:41:59 +02:00
for (var record in list) {
playHistory.add(PlayHistory(record['title'], record['enclosure_url'],
2020-07-28 06:35:57 +02:00
(record['seconds']).toInt(), record['seek_value'],
playdate: DateTime.fromMillisecondsSinceEpoch(record['add_date'])));
2020-07-13 09:41:59 +02:00
}
2020-03-01 13:17:06 +01:00
return playHistory;
}
2020-08-09 21:07:03 +02:00
/// History list in playlist page, not include marked episdoes.
Future<List<PlayHistory>> getPlayRecords(int top) async {
var dbClient = await database;
List<Map> list = await dbClient.rawQuery(
"""SELECT title, enclosure_url, seconds, seek_value, add_date FROM PlayHistory
WHERE seconds != 0 ORDER BY add_date DESC LIMIT ?
""", [top]);
var playHistory = <PlayHistory>[];
for (var record in list) {
playHistory.add(PlayHistory(record['title'], record['enclosure_url'],
(record['seconds']).toInt(), record['seek_value'],
playdate: DateTime.fromMillisecondsSinceEpoch(record['add_date'])));
}
return playHistory;
}
2020-03-01 13:17:06 +01:00
2020-04-11 19:23:12 +02:00
Future<int> isListened(String url) async {
var dbClient = await database;
2020-07-26 12:20:42 +02:00
var i = 0;
2020-06-11 17:13:10 +02:00
List<Map> list = await dbClient.rawQuery(
"SELECT listen_time FROM PlayHistory WHERE enclosure_url = ?", [url]);
2020-07-26 12:20:42 +02:00
if (list.length == 0) {
2020-04-11 19:23:12 +02:00
return 0;
2020-07-26 12:20:42 +02:00
} else {
2020-07-13 09:41:59 +02:00
for (var element in list) {
2020-04-11 19:23:12 +02:00
i += element['listen_time'];
2020-07-13 09:41:59 +02:00
}
2020-04-11 19:23:12 +02:00
return i;
}
}
2020-08-10 15:10:12 +02:00
Future<int> markNotListened(String url) async {
var dbClient = await database;
int count;
await dbClient.transaction((txn) async {
count = await txn.rawUpdate(
"UPDATE OR IGNORE PlayHistory SET listen_time = 0 WHERE enclosure_url = ?",
[url]);
});
await dbClient.rawDelete(
'DELETE FROM PlayHistory WHERE enclosure_url=? '
'AND listen_time = 0 AND seconds = 0',
[url]);
return count;
}
Future<List<SubHistory>> getSubHistory() async {
var dbClient = await database;
List<Map> list = await dbClient.rawQuery(
"""SELECT title, rss_url, add_date, remove_date, status FROM SubscribeHistory
ORDER BY add_date DESC""");
return list
.map((record) => SubHistory(
record['status'] == 0 ? true : false,
DateTime.fromMillisecondsSinceEpoch(record['remove_date']),
DateTime.fromMillisecondsSinceEpoch(record['add_date']),
record['rss_url'],
record['title']))
.toList();
}
Future<double> listenMins(int day) async {
var dbClient = await database;
var now = DateTime.now();
var start = DateTime(now.year, now.month, now.day)
.subtract(Duration(days: day))
.millisecondsSinceEpoch;
var end = DateTime(now.year, now.month, now.day)
.subtract(Duration(days: (day - 1)))
.millisecondsSinceEpoch;
List<Map> list = await dbClient.rawQuery(
"SELECT seconds FROM PlayHistory WHERE add_date > ? AND add_date < ?",
[start, end]);
2020-07-29 14:12:07 +02:00
var sum = 0.0;
2020-07-13 09:41:59 +02:00
if (list.isEmpty) {
2020-07-29 14:12:07 +02:00
sum = 0.0;
} else {
2020-07-26 12:20:42 +02:00
for (var record in list) {
sum += record['seconds'];
}
}
return (sum ~/ 60).toDouble();
}
2020-03-31 18:36:20 +02:00
Future<PlayHistory> getPosition(EpisodeBrief episodeBrief) async {
2020-03-01 13:17:06 +01:00
var dbClient = await database;
List<Map> list = await dbClient.rawQuery(
2020-04-11 19:23:12 +02:00
"""SELECT title, enclosure_url, seconds, seek_value, add_date FROM PlayHistory
WHERE enclosure_url = ? ORDER BY add_date DESC LIMIT 1""",
2020-03-01 13:17:06 +01:00
[episodeBrief.enclosureUrl]);
return list.isNotEmpty
2020-03-31 18:36:20 +02:00
? PlayHistory(list.first['title'], list.first['enclosure_url'],
2020-07-28 06:35:57 +02:00
(list.first['seconds']).toInt(), list.first['seek_value'],
2020-03-31 18:36:20 +02:00
playdate:
DateTime.fromMillisecondsSinceEpoch(list.first['add_date']))
: PlayHistory(episodeBrief.title, episodeBrief.enclosureUrl, 0, 0);
2020-03-01 13:17:06 +01:00
}
2020-07-13 08:56:05 +02:00
/// Check if episode was marked listend.
Future<bool> checkMarked(EpisodeBrief episodeBrief) async {
var dbClient = await database;
List<Map> list = await dbClient.rawQuery(
"""SELECT title, enclosure_url, seconds, seek_value, add_date FROM PlayHistory
WHERE enclosure_url = ? AND seek_value = 1 ORDER BY add_date DESC LIMIT 1""",
[episodeBrief.enclosureUrl]);
return list.isNotEmpty;
}
2020-02-11 14:01:57 +01:00
DateTime _parsePubDate(String pubDate) {
if (pubDate == null) return DateTime.now();
2020-02-11 14:01:57 +01:00
DateTime date;
2020-07-26 12:20:42 +02:00
var yyyy = RegExp(r'[1-2][0-9]{3}');
var hhmm = RegExp(r'[0-2][0-9]\:[0-5][0-9]');
var ddmmm = RegExp(r'[0-3][0-9]\s[A-Z][a-z]{2}');
var mmDd = RegExp(r'([1-2][0-9]{3}\-[0-1]|\s)[0-9]\-[0-3][0-9]');
// RegExp timezone
2020-07-26 12:20:42 +02:00
var z = RegExp(r'(\+|\-)[0-1][0-9]00');
var timezone = z.stringMatch(pubDate);
var timezoneInt = 0;
if (timezone != null) {
if (timezone.substring(0, 1) == '-') {
timezoneInt = int.parse(timezone.substring(1, 2));
} else {
timezoneInt = -int.parse(timezone.substring(1, 2));
}
}
2020-02-09 13:29:09 +01:00
try {
date = DateFormat('EEE, dd MMM yyyy HH:mm:ss Z', 'en_US').parse(pubDate);
2020-02-09 13:29:09 +01:00
} catch (e) {
try {
date = DateFormat('dd MMM yyyy HH:mm:ss Z', 'en_US').parse(pubDate);
} catch (e) {
try {
date = DateFormat('EEE, dd MMM yyyy HH:mm Z', 'en_US').parse(pubDate);
} catch (e) {
//parse date using regex, still have issue in parse maonth/day
2020-07-26 12:20:42 +02:00
var year = yyyy.stringMatch(pubDate);
var time = hhmm.stringMatch(pubDate);
var month = ddmmm.stringMatch(pubDate);
if (year != null && time != null && month != null) {
date = DateFormat('dd MMM yyyy HH:mm', 'en_US')
.parse(month + year + time);
} else if (year != null && time != null && month == null) {
2020-07-26 12:20:42 +02:00
var month = mmDd.stringMatch(pubDate);
date =
DateFormat('yyyy-MM-dd HH:mm', 'en_US').parse('$month $time');
} else {
date = DateTime.now();
}
}
}
2020-02-09 13:29:09 +01:00
}
date.add(Duration(hours: timezoneInt)).add(DateTime.now().timeZoneOffset);
2020-08-10 20:41:22 +02:00
developer.log(date.toString());
return date;
2020-02-09 13:29:09 +01:00
}
int _getExplicit(bool b) {
2020-02-09 13:29:09 +01:00
int result;
if (b == true) {
result = 1;
return result;
} else {
result = 0;
return result;
}
}
bool _isXimalaya(String input) {
2020-07-26 12:20:42 +02:00
var ximalaya = RegExp(r"ximalaya.com");
2020-02-09 13:29:09 +01:00
return ximalaya.hasMatch(input);
}
String _getDescription(String content, String description, String summary) {
if (content.length >= description.length) {
if (content.length >= summary.length) {
2020-08-10 15:10:12 +02:00
return content;
} else {
2020-08-10 15:10:12 +02:00
return summary;
}
} else if (description.length >= summary.length) {
2020-08-10 15:10:12 +02:00
return description;
} else {
2020-08-10 15:10:12 +02:00
return summary;
}
}
Future<int> savePodcastRss(RssFeed feed, String id) async {
feed.items.removeWhere((item) => item == null);
2020-07-26 12:20:42 +02:00
var result = feed.items.length;
var dbClient = await database;
String description, url;
2020-07-26 12:20:42 +02:00
for (var i = 0; i < result; i++) {
2020-08-10 20:41:22 +02:00
developer.log(feed.items[i].title);
2020-07-26 15:48:30 +02:00
description = _getDescription(feed.items[i]?.content?.value ?? '',
feed.items[i].description ?? '', feed.items[i].itunes.summary ?? '');
if (feed.items[i].enclosure != null) {
_isXimalaya(feed.items[i].enclosure.url)
? url = feed.items[i].enclosure.url.split('=').last
: url = feed.items[i].enclosure.url;
}
final title = feed.items[i].itunes.title ?? feed.items[i].title;
final length = feed.items[i]?.enclosure?.length;
final pubDate = feed.items[i].pubDate;
final date = _parsePubDate(pubDate);
final milliseconds = date.millisecondsSinceEpoch;
2020-04-11 19:23:12 +02:00
final duration = feed.items[i].itunes.duration?.inSeconds ?? 0;
final explicit = _getExplicit(feed.items[i].itunes.explicit);
if (url != null) {
await dbClient.transaction((txn) {
return txn.rawInsert(
"""INSERT OR REPLACE INTO Episodes(title, enclosure_url, enclosure_length, pubDate,
description, feed_id, milliseconds, duration, explicit, media_id) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?)""",
[
title,
url,
length,
pubDate,
description,
id,
milliseconds,
duration,
explicit,
url
]);
});
}
}
2020-07-26 12:20:42 +02:00
var countUpdate = Sqflite.firstIntValue(await dbClient
2020-03-31 18:36:20 +02:00
.rawQuery('SELECT COUNT(*) FROM Episodes WHERE feed_id = ?', [id]));
await dbClient.rawUpdate(
"""UPDATE PodcastLocal SET episode_count = ? WHERE id = ?""",
[countUpdate, id]);
return result;
}
Future<int> updatePodcastRss(PodcastLocal podcastLocal,
{int removeMark = 0}) async {
2020-07-26 12:20:42 +02:00
var options = BaseOptions(
connectTimeout: 20000,
receiveTimeout: 20000,
);
try {
2020-07-26 12:20:42 +02:00
var response = await Dio(options).get(podcastLocal.rssUrl);
if (response.statusCode == 200) {
var feed = RssFeed.parse(response.data);
String url, description;
feed.items.removeWhere((item) => item == null);
var dbClient = await database;
2020-07-26 12:20:42 +02:00
var count = Sqflite.firstIntValue(await dbClient.rawQuery(
'SELECT COUNT(*) FROM Episodes WHERE feed_id = ?',
[podcastLocal.id]));
2020-07-26 12:20:42 +02:00
if (removeMark == 0) {
await dbClient.rawUpdate(
"UPDATE Episodes SET is_new = 0 WHERE feed_id = ?",
[podcastLocal.id]);
2020-07-26 12:20:42 +02:00
}
2020-07-13 08:56:05 +02:00
for (var item in feed.items) {
2020-08-10 20:41:22 +02:00
developer.log(item.title);
2020-07-13 08:56:05 +02:00
description = _getDescription(item.content.value ?? '',
item.description ?? '', item.itunes.summary ?? '');
if (item.enclosure?.url != null) {
_isXimalaya(item.enclosure.url)
? url = item.enclosure.url.split('=').last
: url = item.enclosure.url;
}
2020-07-13 08:56:05 +02:00
final title = item.itunes.title ?? item.title;
final length = item?.enclosure?.length ?? 0;
final pubDate = item.pubDate;
final date = _parsePubDate(pubDate);
final milliseconds = date.millisecondsSinceEpoch;
2020-07-13 08:56:05 +02:00
final duration = item.itunes.duration?.inSeconds ?? 0;
final explicit = _getExplicit(item.itunes.explicit);
2020-03-31 18:36:20 +02:00
if (url != null) {
await dbClient.transaction((txn) async {
await txn.rawInsert(
"""INSERT OR IGNORE INTO Episodes(title, enclosure_url, enclosure_length, pubDate,
2020-03-31 18:36:20 +02:00
description, feed_id, milliseconds, duration, explicit, media_id, is_new) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 1)""",
[
title,
url,
length,
pubDate,
description,
podcastLocal.id,
milliseconds,
duration,
explicit,
url,
]);
});
}
}
2020-07-26 12:20:42 +02:00
var countUpdate = Sqflite.firstIntValue(await dbClient.rawQuery(
'SELECT COUNT(*) FROM Episodes WHERE feed_id = ?',
[podcastLocal.id]));
await dbClient.rawUpdate(
"""UPDATE PodcastLocal SET update_count = ?, episode_count = ? WHERE id = ?""",
[countUpdate - count, countUpdate, podcastLocal.id]);
return countUpdate - count;
2020-02-09 13:29:09 +01:00
}
return 0;
} catch (e) {
2020-08-11 19:06:56 +02:00
developer.log(e.toString(), name: 'Update podcast error');
return -1;
2020-02-09 13:29:09 +01:00
}
}
2020-07-26 12:20:42 +02:00
Future<List<EpisodeBrief>> getRssItem(String id, int count,
{bool reverse,
Filter filter = Filter.all,
2020-07-24 16:10:08 +02:00
String query = '',
bool hideListened = false}) async {
2020-02-09 13:29:09 +01:00
var dbClient = await database;
2020-07-26 12:20:42 +02:00
var episodes = <EpisodeBrief>[];
var list = <Map>[];
2020-07-23 20:42:25 +02:00
if (count == -1) {
switch (filter) {
case Filter.all:
list = await dbClient
.rawQuery("""SELECT E.title, E.enclosure_url, E.enclosure_length,
E.milliseconds, P.imagePath, P.title as feedTitle, E.duration, E.explicit,
P.primaryColor FROM Episodes E INNER JOIN PodcastLocal P ON E.feed_id = P.id
WHERE P.id = ? ORDER BY E.milliseconds ASC""", [id]);
2020-07-23 20:42:25 +02:00
break;
case Filter.liked:
list = await dbClient
.rawQuery("""SELECT E.title, E.enclosure_url, E.enclosure_length,
E.milliseconds, P.imagePath, P.title as feedTitle, E.duration, E.explicit,
P.primaryColor FROM Episodes E INNER JOIN PodcastLocal P ON E.feed_id = P.id
WHERE P.id = ? AND E.liked = 1 ORDER BY E.milliseconds ASC""", [id]);
break;
case Filter.downloaded:
list = await dbClient.rawQuery(
"""SELECT E.title, E.enclosure_url, E.enclosure_length,
E.milliseconds, P.imagePath, P.title as feedTitle, E.duration, E.explicit,
P.primaryColor FROM Episodes E INNER JOIN PodcastLocal P ON E.feed_id = P.id
WHERE P.id = ? AND E.media_id != E.enclosure_url ORDER BY E.milliseconds ASC""",
[id]);
break;
case Filter.search:
list = await dbClient.rawQuery(
"""SELECT E.title, E.enclosure_url, E.enclosure_length,
E.milliseconds, P.imagePath, P.title as feedTitle, E.duration, E.explicit,
P.primaryColor FROM Episodes E INNER JOIN PodcastLocal P ON E.feed_id = P.id
WHERE P.id = ? AND E.title LIKE ? ORDER BY E.milliseconds ASC""",
[id, '%$query%']);
break;
default:
}
} else if (reverse) {
2020-07-23 20:42:25 +02:00
switch (filter) {
case Filter.all:
list = await dbClient
.rawQuery("""SELECT E.title, E.enclosure_url, E.enclosure_length,
E.milliseconds, P.imagePath, P.title as feedTitle, E.duration, E.explicit,
P.primaryColor FROM Episodes E INNER JOIN PodcastLocal P ON E.feed_id = P.id
WHERE P.id = ? ORDER BY E.milliseconds ASC LIMIT ?""", [id, count]);
break;
case Filter.liked:
list = await dbClient.rawQuery(
"""SELECT E.title, E.enclosure_url, E.enclosure_length,
E.milliseconds, P.imagePath, P.title as feedTitle, E.duration, E.explicit,
P.primaryColor FROM Episodes E INNER JOIN PodcastLocal P ON E.feed_id = P.id
WHERE P.id = ? AND E.liked = 1 ORDER BY E.milliseconds ASC LIMIT ?""",
[id, count]);
break;
case Filter.downloaded:
list = await dbClient.rawQuery(
"""SELECT E.title, E.enclosure_url, E.enclosure_length,
E.milliseconds, P.imagePath, P.title as feedTitle, E.duration, E.explicit,
P.primaryColor FROM Episodes E INNER JOIN PodcastLocal P ON E.feed_id = P.id
WHERE P.id = ? AND E.enclosure_url != E.media_id ORDER BY E.milliseconds ASC LIMIT ?""",
[id, count]);
break;
case Filter.search:
list = await dbClient.rawQuery(
"""SELECT E.title, E.enclosure_url, E.enclosure_length,
E.milliseconds, P.imagePath, P.title as feedTitle, E.duration, E.explicit,
P.primaryColor FROM Episodes E INNER JOIN PodcastLocal P ON E.feed_id = P.id
WHERE P.id = ? AND E.title LIKE ? ORDER BY E.milliseconds ASC LIMIT ?""",
[id, '%$query%', count]);
break;
default:
2020-04-11 19:23:12 +02:00
}
} else {
2020-07-23 20:42:25 +02:00
switch (filter) {
case Filter.all:
2020-07-24 16:10:08 +02:00
list = await dbClient.rawQuery(
"""SELECT E.title, E.enclosure_url, E.enclosure_length, E.is_new,
2020-07-23 20:42:25 +02:00
E.milliseconds, P.imagePath, P.title as feedTitle, E.duration, E.explicit,
P.primaryColor FROM Episodes E INNER JOIN PodcastLocal P ON E.feed_id = P.id
WHERE P.id = ? ORDER BY E.milliseconds DESC LIMIT ?""", [id, count]);
break;
case Filter.liked:
list = await dbClient.rawQuery(
2020-07-24 16:10:08 +02:00
"""SELECT E.title, E.enclosure_url, E.enclosure_length, E.is_new,
2020-07-23 20:42:25 +02:00
E.milliseconds, P.imagePath, P.title as feedTitle, E.duration, E.explicit,
P.primaryColor FROM Episodes E INNER JOIN PodcastLocal P ON E.feed_id = P.id
WHERE P.id = ? AND E.liked = 1 ORDER BY E.milliseconds DESC LIMIT ?""",
[id, count]);
break;
case Filter.downloaded:
list = await dbClient.rawQuery(
2020-07-24 16:10:08 +02:00
"""SELECT E.title, E.enclosure_url, E.enclosure_length, E.is_new,
2020-07-23 20:42:25 +02:00
E.milliseconds, P.imagePath, P.title as feedTitle, E.duration, E.explicit,
P.primaryColor FROM Episodes E INNER JOIN PodcastLocal P ON E.feed_id = P.id
WHERE P.id = ? AND E.enclosure_url != E.media_id ORDER BY E.milliseconds DESC LIMIT ?""",
[id, count]);
break;
case Filter.search:
list = await dbClient.rawQuery(
2020-07-24 16:10:08 +02:00
"""SELECT E.title, E.enclosure_url, E.enclosure_length, E.is_new,
2020-07-23 20:42:25 +02:00
E.milliseconds, P.imagePath, P.title as feedTitle, E.duration, E.explicit,
P.primaryColor FROM Episodes E INNER JOIN PodcastLocal P ON E.feed_id = P.id
WHERE P.id = ? AND E.title LIKE ? ORDER BY E.milliseconds DESC LIMIT ?""",
[id, '%$query%', count]);
break;
default:
}
}
2020-07-24 16:10:08 +02:00
if (list.isNotEmpty) {
2020-07-26 12:20:42 +02:00
if (!hideListened) {
2020-07-24 16:10:08 +02:00
for (var i in list) {
episodes.add(EpisodeBrief(
i['title'],
i['enclosure_url'],
i['enclosure_length'],
i['milliseconds'],
i['feedTitle'],
i['primaryColor'],
i['duration'],
i['explicit'],
i['imagePath'],
i['is_new']));
}
2020-07-26 12:20:42 +02:00
} else {
2020-07-24 16:10:08 +02:00
for (var i in list) {
2020-07-26 12:20:42 +02:00
var listened = await isListened(i['enclosure_url']);
if (listened == 0) {
2020-07-24 16:10:08 +02:00
episodes.add(EpisodeBrief(
i['title'],
i['enclosure_url'],
i['enclosure_length'],
i['milliseconds'],
i['feedTitle'],
i['primaryColor'],
i['duration'],
i['explicit'],
i['imagePath'],
i['is_new']));
2020-07-26 12:20:42 +02:00
}
2020-07-24 16:10:08 +02:00
}
2020-07-26 12:20:42 +02:00
}
2020-07-24 16:10:08 +02:00
}
2020-07-23 20:42:25 +02:00
return episodes;
2020-02-09 13:29:09 +01:00
}
Future<List<EpisodeBrief>> getNewEpisodes(String id) async {
var dbClient = await database;
2020-07-26 12:20:42 +02:00
var episodes = <EpisodeBrief>[];
List<Map> list;
2020-07-26 12:20:42 +02:00
if (id == 'all') {
list = await dbClient.rawQuery(
2020-07-24 16:10:08 +02:00
"""SELECT E.title, E.enclosure_url, E.enclosure_length, E.is_new,
2020-07-23 20:42:25 +02:00
E.milliseconds, P.imagePath, P.title as feed_title, E.duration, E.explicit,
P.primaryColor FROM Episodes E INNER JOIN PodcastLocal P ON E.feed_id = P.id
2020-06-11 17:13:10 +02:00
WHERE E.is_new = 1 AND E.downloaded = 'ND' AND P.auto_download = 1 ORDER BY E.milliseconds ASC""",
);
2020-07-26 12:20:42 +02:00
} else {
list = await dbClient.rawQuery(
2020-07-24 16:10:08 +02:00
"""SELECT E.title, E.enclosure_url, E.enclosure_length, E.is_new,
2020-07-23 20:42:25 +02:00
E.milliseconds, P.imagePath, P.title as feed_title, E.duration, E.explicit,
P.primaryColor FROM Episodes E INNER JOIN PodcastLocal P ON E.feed_id = P.id
2020-06-11 17:13:10 +02:00
WHERE E.is_new = 1 AND E.downloaded = 'ND' AND E.feed_id = ? ORDER BY E.milliseconds ASC""",
[id]);
2020-07-26 12:20:42 +02:00
}
if (list.isNotEmpty) {
2020-07-13 08:56:05 +02:00
for (var i in list) {
episodes.add(EpisodeBrief(
2020-07-24 16:10:08 +02:00
i['title'],
i['enclosure_url'],
i['enclosure_length'],
i['milliseconds'],
i['feed_title'],
i['primaryColor'],
i['duration'],
i['explicit'],
i['imagePath'],
i['is_new']));
}
2020-07-26 12:20:42 +02:00
}
return episodes;
}
Future<List<EpisodeBrief>> getRssItemTop(String id) async {
2020-02-09 13:29:09 +01:00
var dbClient = await database;
2020-07-26 12:20:42 +02:00
var episodes = <EpisodeBrief>[];
2020-07-24 16:10:08 +02:00
List<Map> list = await dbClient.rawQuery(
"""SELECT E.title, E.enclosure_url, E.enclosure_length, E.is_new,
2020-07-23 20:42:25 +02:00
E.milliseconds, P.imagePath, P.title as feed_title, E.duration, E.explicit,
P.primaryColor FROM Episodes E INNER JOIN PodcastLocal P ON E.feed_id = P.id
where E.feed_id = ? ORDER BY E.milliseconds DESC LIMIT 2""", [id]);
2020-07-13 08:56:05 +02:00
for (var i in list) {
2020-02-09 13:29:09 +01:00
episodes.add(EpisodeBrief(
2020-07-24 16:10:08 +02:00
i['title'],
i['enclosure_url'],
i['enclosure_length'],
i['milliseconds'],
i['feed_title'],
i['primaryColor'],
i['duration'],
i['explicit'],
i['imagePath'],
i['is_new']));
2020-02-09 13:29:09 +01:00
}
return episodes;
}
Future<List<EpisodeBrief>> getRecentRssItem(int top) async {
2020-02-09 13:29:09 +01:00
var dbClient = await database;
2020-07-26 12:20:42 +02:00
var episodes = <EpisodeBrief>[];
2020-07-24 16:10:08 +02:00
List<Map> list = await dbClient.rawQuery(
"""SELECT E.title, E.enclosure_url, E.enclosure_length, E.is_new,
2020-07-23 20:42:25 +02:00
E.milliseconds, P.title as feed_title, E.duration, E.explicit,
P.imagePath, P.primaryColor FROM Episodes E INNER JOIN PodcastLocal P ON E.feed_id = P.id
ORDER BY E.milliseconds DESC LIMIT ? """, [top]);
2020-07-13 08:56:05 +02:00
for (var i in list) {
2020-02-09 13:29:09 +01:00
episodes.add(EpisodeBrief(
2020-07-24 16:10:08 +02:00
i['title'],
i['enclosure_url'],
i['enclosure_length'],
i['milliseconds'],
i['feed_title'],
i['primaryColor'],
i['duration'],
i['explicit'],
i['imagePath'],
i['is_new']));
2020-02-09 13:29:09 +01:00
}
return episodes;
}
2020-04-11 19:23:12 +02:00
Future<List<EpisodeBrief>> getGroupRssItem(
int top, List<String> group) async {
var dbClient = await database;
2020-07-26 12:20:42 +02:00
var episodes = <EpisodeBrief>[];
2020-04-11 19:23:12 +02:00
if (group.length > 0) {
2020-07-26 12:20:42 +02:00
var s = group.map<String>((e) => "'$e'").toList();
2020-07-24 16:10:08 +02:00
List<Map> list = await dbClient.rawQuery(
"""SELECT E.title, E.enclosure_url, E.enclosure_length, E.is_new,
2020-07-23 20:42:25 +02:00
E.milliseconds, P.title as feed_title, E.duration, E.explicit,
P.imagePath, P.primaryColor FROM Episodes E INNER JOIN PodcastLocal P ON E.feed_id = P.id
2020-04-11 19:23:12 +02:00
WHERE P.id in (${s.join(',')})
ORDER BY E.milliseconds DESC LIMIT ? """, [top]);
2020-07-13 08:56:05 +02:00
for (var i in list) {
2020-04-11 19:23:12 +02:00
episodes.add(EpisodeBrief(
2020-07-24 16:10:08 +02:00
i['title'],
i['enclosure_url'],
i['enclosure_length'],
i['milliseconds'],
i['feed_title'],
i['primaryColor'],
i['duration'],
i['explicit'],
i['imagePath'],
i['is_new']));
}
}
return episodes;
}
Future<List<EpisodeBrief>> getRecentNewRssItem() async {
var dbClient = await database;
2020-07-26 12:20:42 +02:00
var episodes = <EpisodeBrief>[];
List<Map> list = await dbClient.rawQuery(
2020-07-25 12:32:05 +02:00
"""SELECT E.title, E.enclosure_url, E.enclosure_length, E.is_new, E.media_id,
2020-07-23 20:42:25 +02:00
E.milliseconds, P.title as feed_title, E.duration, E.explicit,
P.imagePath, P.primaryColor FROM Episodes E INNER JOIN PodcastLocal P ON E.feed_id = P.id
WHERE is_new = 1 ORDER BY E.milliseconds DESC """,
);
2020-07-13 08:56:05 +02:00
for (var i in list) {
episodes.add(EpisodeBrief(
2020-07-24 16:10:08 +02:00
i['title'],
i['enclosure_url'],
i['enclosure_length'],
i['milliseconds'],
i['feed_title'],
i['primaryColor'],
i['duration'],
i['explicit'],
i['imagePath'],
2020-07-25 12:32:05 +02:00
i['is_new'],
mediaId: i['media_id']));
}
return episodes;
}
Future<List<EpisodeBrief>> getOutdatedEpisode(int deadline) async {
var dbClient = await database;
2020-07-26 12:20:42 +02:00
var episodes = <EpisodeBrief>[];
2020-07-24 16:10:08 +02:00
List<Map> list = await dbClient.rawQuery(
"""SELECT E.title, E.enclosure_url, E.enclosure_length, E.is_new,
2020-07-23 20:42:25 +02:00
E.milliseconds, P.title as feed_title, E.duration, E.explicit,
P.imagePath, P.primaryColor FROM Episodes E INNER JOIN PodcastLocal P ON E.feed_id = P.id
WHERE E.download_date < ? AND E.enclosure_url != E.media_id
ORDER BY E.milliseconds DESC""", [deadline]);
2020-07-13 08:56:05 +02:00
for (var i in list) {
episodes.add(EpisodeBrief(
2020-07-24 16:10:08 +02:00
i['title'],
i['enclosure_url'],
i['enclosure_length'],
i['milliseconds'],
i['feed_title'],
i['primaryColor'],
i['duration'],
i['explicit'],
i['imagePath'],
i['is_new']));
}
return episodes;
}
Future<List<EpisodeBrief>> getDownloadedEpisode(int mode) async {
var dbClient = await database;
2020-07-26 12:20:42 +02:00
var episodes = <EpisodeBrief>[];
List<Map> list;
//Ordered by date
2020-07-26 12:20:42 +02:00
if (mode == 0) {
list = await dbClient.rawQuery(
2020-07-24 16:10:08 +02:00
"""SELECT E.title, E.enclosure_url, E.enclosure_length, E.download_date, E.is_new,
2020-07-23 20:42:25 +02:00
E.milliseconds, P.title as feed_title, E.duration, E.explicit,
P.imagePath, P.primaryColor FROM Episodes E INNER JOIN PodcastLocal P ON E.feed_id = P.id
WHERE E.enclosure_url != E.media_id
ORDER BY E.download_date DESC""",
);
2020-07-26 12:20:42 +02:00
} else if (mode == 1) {
list = await dbClient.rawQuery(
2020-07-24 16:10:08 +02:00
"""SELECT E.title, E.enclosure_url, E.enclosure_length, E.download_date,E.is_new,
2020-07-23 20:42:25 +02:00
E.milliseconds, P.title as feed_title, E.duration, E.explicit,
P.imagePath, P.primaryColor FROM Episodes E INNER JOIN PodcastLocal P ON E.feed_id = P.id
WHERE E.enclosure_url != E.media_id
ORDER BY E.download_date ASC""",
);
2020-07-26 12:20:42 +02:00
} else if (mode == 2) {
list = await dbClient.rawQuery(
2020-07-24 16:10:08 +02:00
"""SELECT E.title, E.enclosure_url, E.enclosure_length, E.download_date,E.is_new,
2020-07-23 20:42:25 +02:00
E.milliseconds, P.title as feed_title, E.duration, E.explicit,
P.imagePath, P.primaryColor FROM Episodes E INNER JOIN PodcastLocal P ON E.feed_id = P.id
WHERE E.enclosure_url != E.media_id
ORDER BY E.enclosure_length DESC""",
);
2020-07-26 12:20:42 +02:00
}
2020-07-13 08:56:05 +02:00
for (var i in list) {
episodes.add(
EpisodeBrief(
2020-07-13 08:56:05 +02:00
i['title'],
i['enclosure_url'],
i['enclosure_length'],
i['milliseconds'],
i['feed_title'],
i['primaryColor'],
i['duration'],
i['explicit'],
i['imagePath'],
2020-07-24 16:10:08 +02:00
i['is_new'],
2020-07-13 08:56:05 +02:00
downloadDate: i['download_date']),
);
}
return episodes;
}
2020-06-07 20:42:27 +02:00
2020-06-11 17:13:10 +02:00
removeAllNewMark() async {
var dbClient = await database;
2020-06-11 17:13:10 +02:00
await dbClient.transaction((txn) async {
await txn.rawUpdate("UPDATE Episodes SET is_new = 0 ");
});
}
Future<List<EpisodeBrief>> getGroupNewRssItem(List<String> group) async {
var dbClient = await database;
2020-07-26 12:20:42 +02:00
var episodes = <EpisodeBrief>[];
if (group.length > 0) {
2020-07-26 12:20:42 +02:00
var s = group.map<String>((e) => "'$e'").toList();
List<Map> list = await dbClient.rawQuery(
2020-07-25 12:32:05 +02:00
"""SELECT E.title, E.enclosure_url, E.enclosure_length, E.is_new, E.media_id,
2020-07-23 20:42:25 +02:00
E.milliseconds, P.title as feed_title, E.duration, E.explicit,
P.imagePath, P.primaryColor FROM Episodes E INNER JOIN PodcastLocal P ON E.feed_id = P.id
WHERE P.id in (${s.join(',')}) AND is_new = 1
ORDER BY E.milliseconds DESC""",
);
2020-07-13 08:56:05 +02:00
for (var i in list) {
episodes.add(EpisodeBrief(
2020-07-24 16:10:08 +02:00
i['title'],
i['enclosure_url'],
i['enclosure_length'],
i['milliseconds'],
i['feed_title'],
i['primaryColor'],
i['duration'],
i['explicit'],
i['imagePath'],
2020-07-25 12:32:05 +02:00
i['is_new'],
mediaId: i['media_id']));
2020-04-11 19:23:12 +02:00
}
}
return episodes;
}
2020-06-11 17:13:10 +02:00
removeGroupNewMark(List<String> group) async {
var dbClient = await database;
if (group.length > 0) {
2020-07-26 12:20:42 +02:00
var s = group.map<String>((e) => "'$e'").toList();
2020-06-11 17:13:10 +02:00
await dbClient.transaction((txn) async {
await txn.rawUpdate(
"UPDATE Episodes SET is_new = 0 WHERE feed_id in (${s.join(',')})");
});
}
}
2020-06-11 17:13:10 +02:00
removeEpisodeNewMark(String url) async {
var dbClient = await database;
2020-06-11 17:13:10 +02:00
await dbClient.transaction((txn) async {
await txn.rawUpdate(
"UPDATE Episodes SET is_new = 0 WHERE enclosure_url = ?", [url]);
});
2020-08-10 20:41:22 +02:00
developer.log('remove new episode $url');
}
2020-04-11 19:23:12 +02:00
Future<List<EpisodeBrief>> getLikedRssItem(int i, int sortBy) async {
2020-02-09 13:29:09 +01:00
var dbClient = await database;
2020-07-26 12:20:42 +02:00
var episodes = <EpisodeBrief>[];
2020-04-11 19:23:12 +02:00
if (sortBy == 0) {
List<Map> list = await dbClient.rawQuery(
"""SELECT E.title, E.enclosure_url, E.enclosure_length, E.milliseconds, P.imagePath,
2020-07-24 16:10:08 +02:00
P.title as feed_title, E.duration, E.explicit, P.primaryColor, E.is_new
2020-07-23 20:42:25 +02:00
FROM Episodes E INNER JOIN PodcastLocal P ON E.feed_id = P.id
2020-03-31 18:36:20 +02:00
WHERE E.liked = 1 ORDER BY E.milliseconds DESC LIMIT ?""", [i]);
2020-07-13 08:56:05 +02:00
for (var i in list) {
2020-04-11 19:23:12 +02:00
episodes.add(EpisodeBrief(
2020-07-24 16:10:08 +02:00
i['title'],
i['enclosure_url'],
i['enclosure_length'],
i['milliseconds'],
i['feed_title'],
i['primaryColor'],
i['duration'],
i['explicit'],
i['imagePath'],
i['is_new']));
2020-04-11 19:23:12 +02:00
}
} else {
List<Map> list = await dbClient.rawQuery(
"""SELECT E.title, E.enclosure_url, E.enclosure_length, E.milliseconds, P.imagePath,
2020-07-24 16:10:08 +02:00
P.title as feed_title, E.duration, E.explicit, P.primaryColor, E.is_new
2020-07-23 20:42:25 +02:00
FROM Episodes E INNER JOIN PodcastLocal P ON E.feed_id = P.id
2020-04-11 19:23:12 +02:00
WHERE E.liked = 1 ORDER BY E.liked_date DESC LIMIT ?""", [i]);
2020-07-13 08:56:05 +02:00
for (var i in list) {
2020-04-11 19:23:12 +02:00
episodes.add(EpisodeBrief(
2020-07-24 16:10:08 +02:00
i['title'],
i['enclosure_url'],
i['enclosure_length'],
i['milliseconds'],
i['feed_title'],
i['primaryColor'],
i['duration'],
i['explicit'],
i['imagePath'],
i['is_new']));
2020-04-11 19:23:12 +02:00
}
2020-02-09 13:29:09 +01:00
}
return episodes;
}
2020-06-11 17:13:10 +02:00
setLiked(String url) async {
2020-02-09 13:29:09 +01:00
var dbClient = await database;
2020-07-26 12:20:42 +02:00
var milliseconds = DateTime.now().millisecondsSinceEpoch;
2020-06-11 17:13:10 +02:00
await dbClient.transaction((txn) async {
await txn.rawUpdate(
"UPDATE Episodes SET liked = 1, liked_date = ? WHERE enclosure_url= ?",
[milliseconds, url]);
});
2020-02-09 13:29:09 +01:00
}
2020-06-11 17:13:10 +02:00
setUniked(String url) async {
2020-02-09 13:29:09 +01:00
var dbClient = await database;
2020-06-11 17:13:10 +02:00
await dbClient.transaction((txn) async {
await txn.rawUpdate(
"UPDATE Episodes SET liked = 0 WHERE enclosure_url = ?", [url]);
});
2020-02-09 13:29:09 +01:00
}
2020-04-11 19:23:12 +02:00
Future<bool> isLiked(String url) async {
var dbClient = await database;
var list = <Map>[];
list = await dbClient
2020-04-11 19:23:12 +02:00
.rawQuery("SELECT liked FROM Episodes WHERE enclosure_url = ?", [url]);
if (list.isNotEmpty) {
return list.first['liked'] == 0 ? false : true;
}
return false;
2020-04-11 19:23:12 +02:00
}
Future<bool> isDownloaded(String url) async {
var dbClient = await database;
List<Map> list = await dbClient.rawQuery(
2020-08-01 17:11:12 +02:00
"SELECT id FROM Episodes WHERE enclosure_url = ? AND enclosure_url != media_id",
[url]);
return list.isNotEmpty;
}
2020-02-09 13:29:09 +01:00
Future<int> saveDownloaded(String url, String id) async {
var dbClient = await database;
2020-07-26 12:20:42 +02:00
var milliseconds = DateTime.now().millisecondsSinceEpoch;
int count;
await dbClient.transaction((txn) async {
count = await txn.rawUpdate(
"UPDATE Episodes SET downloaded = ?, download_date = ? WHERE enclosure_url = ?",
[id, milliseconds, url]);
});
2020-02-09 13:29:09 +01:00
return count;
}
Future<int> saveMediaId(String url, String path, String id, int size) async {
var dbClient = await database;
2020-07-26 12:20:42 +02:00
var milliseconds = DateTime.now().millisecondsSinceEpoch;
int count;
await dbClient.transaction((txn) async {
count = await txn.rawUpdate(
"UPDATE Episodes SET enclosure_length = ?, media_id = ?, download_date = ?, downloaded = ? WHERE enclosure_url = ?",
[size, path, milliseconds, id, url]);
});
return count;
}
2020-02-09 13:29:09 +01:00
Future<int> delDownloaded(String url) async {
var dbClient = await database;
int count;
await dbClient.transaction((txn) async {
count = await txn.rawUpdate(
"UPDATE Episodes SET downloaded = 'ND', media_id = ? WHERE enclosure_url = ?",
[url, url]);
});
2020-08-10 20:41:22 +02:00
developer.log('Deleted $url');
2020-02-09 13:29:09 +01:00
return count;
}
Future<String> getDescription(String url) async {
2020-02-09 13:29:09 +01:00
var dbClient = await database;
List<Map> list = await dbClient.rawQuery(
'SELECT description FROM Episodes WHERE enclosure_url = ?', [url]);
2020-02-09 13:29:09 +01:00
String description = list[0]['description'];
return description;
}
Future<String> getFeedDescription(String id) async {
2020-02-09 13:29:09 +01:00
var dbClient = await database;
List<Map> list = await dbClient
.rawQuery('SELECT description FROM PodcastLocal WHERE id = ?', [id]);
2020-02-09 13:29:09 +01:00
String description = list[0]['description'];
return description;
}
Future<EpisodeBrief> getRssItemWithUrl(String url) async {
var dbClient = await database;
EpisodeBrief episode;
List<Map> list = await dbClient.rawQuery(
"""SELECT E.title, E.enclosure_url, E.enclosure_length, E.milliseconds, P.imagePath,
2020-07-24 16:10:08 +02:00
P.title as feed_title, E.duration, E.explicit, P.skip_seconds,E.is_new,
2020-07-23 20:42:25 +02:00
P.primaryColor, E.media_id FROM Episodes E INNER JOIN PodcastLocal P ON E.feed_id = P.id
WHERE E.enclosure_url = ?""", [url]);
2020-07-13 08:56:05 +02:00
if (list.isEmpty) {
return null;
} else {
episode = EpisodeBrief(
list.first['title'],
list.first['enclosure_url'],
list.first['enclosure_length'],
list.first['milliseconds'],
list.first['feed_title'],
list.first['primaryColor'],
list.first['duration'],
list.first['explicit'],
list.first['imagePath'],
2020-07-24 16:10:08 +02:00
list.first['is_new'],
2020-07-23 20:42:25 +02:00
mediaId: list.first['media_id'],
skipSeconds: list.first['skip_seconds']);
2020-08-15 19:43:45 +02:00
if (episode.enclosureUrl != episode.mediaId &&
!File(episode.mediaId.substring(7)).existsSync()) {
final url = episode.enclosureUrl;
await delDownloaded(url);
episode = episode.copyWith(mediaId: url);
}
return episode;
}
}
Future<EpisodeBrief> getRssItemWithMediaId(String id) async {
var dbClient = await database;
EpisodeBrief episode;
List<Map> list = await dbClient.rawQuery(
"""SELECT E.title, E.enclosure_url, E.enclosure_length, E.milliseconds, P.imagePath,
2020-07-24 16:10:08 +02:00
P.title as feed_title, E.duration, E.explicit, P.skip_seconds,E.is_new,
2020-07-23 20:42:25 +02:00
P.primaryColor, E.media_id FROM Episodes E INNER JOIN PodcastLocal P ON E.feed_id = P.id
WHERE E.media_id = ?""", [id]);
2020-07-26 12:20:42 +02:00
if (list.isEmpty) {
return null;
2020-07-26 12:20:42 +02:00
} else {
episode = EpisodeBrief(
list.first['title'],
list.first['enclosure_url'],
list.first['enclosure_length'],
list.first['milliseconds'],
list.first['feed_title'],
list.first['primaryColor'],
list.first['duration'],
list.first['explicit'],
list.first['imagePath'],
2020-07-24 16:10:08 +02:00
list.first['is_new'],
2020-07-23 20:42:25 +02:00
mediaId: list.first['media_id'],
skipSeconds: list.first['skip_seconds']);
2020-08-15 19:43:45 +02:00
if (episode.enclosureUrl != episode.mediaId &&
!File(episode.mediaId.substring(7)).existsSync()) {
final url = episode.enclosureUrl;
await delDownloaded(url);
episode = episode.copyWith(mediaId: url);
}
return episode;
}
}
2020-05-18 19:03:45 +02:00
Future<String> getImageUrl(String url) async {
2020-05-18 19:03:45 +02:00
var dbClient = await database;
List<Map> list = await dbClient.rawQuery(
"""SELECT P.imageUrl FROM Episodes E INNER JOIN PodcastLocal P ON E.feed_id = P.id
WHERE E.enclosure_url = ?""", [url]);
2020-07-13 08:56:05 +02:00
if (list.isEmpty) return null;
2020-05-18 19:03:45 +02:00
return list.first["imageUrl"];
}
2020-02-09 13:29:09 +01:00
}