2020-02-09 13:29:09 +01:00
|
|
|
import 'package:sqflite/sqflite.dart';
|
|
|
|
import 'dart:async';
|
|
|
|
import 'package:path/path.dart';
|
|
|
|
import 'package:intl/intl.dart';
|
|
|
|
import 'package:flutter_downloader/flutter_downloader.dart';
|
2020-02-20 16:44:42 +01:00
|
|
|
import 'package:dio/dio.dart';
|
2020-02-20 10:09:21 +01:00
|
|
|
import 'package:tsacdop/class/podcastlocal.dart';
|
2020-02-25 10:57:12 +01:00
|
|
|
import 'package:tsacdop/class/audiostate.dart';
|
2020-02-20 10:09:21 +01:00
|
|
|
import 'package:tsacdop/class/episodebrief.dart';
|
2020-02-12 04:08:28 +01:00
|
|
|
import 'package:tsacdop/webfeed/webfeed.dart';
|
2020-03-14 04:14:24 +01:00
|
|
|
import 'package:tsacdop/class/sub_history.dart';
|
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 {
|
2020-03-03 17:04:23 +01:00
|
|
|
var documentsDirectory = await getDatabasesPath();
|
|
|
|
String path = join(documentsDirectory, "podcasts.db");
|
2020-02-09 13:29:09 +01:00
|
|
|
Database theDb = await openDatabase(path, version: 1, onCreate: _onCreate);
|
|
|
|
return theDb;
|
|
|
|
}
|
|
|
|
|
|
|
|
void _onCreate(Database db, int version) async {
|
2020-02-20 10:09:21 +01:00
|
|
|
await db
|
|
|
|
.execute("""CREATE TABLE PodcastLocal(id TEXT PRIMARY KEY,title TEXT,
|
2020-02-16 09:25:53 +01: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-02-26 10:54:59 +01:00
|
|
|
background_image TEXT DEFAULT '',hosts TEXT DEFAULT '')""");
|
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,
|
2020-02-20 16:44:42 +01:00
|
|
|
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-03-14 04:14:24 +01:00
|
|
|
downloaded TEXT DEFAULT 'ND', download_date INTEGER DEFAULT 0, media_id TEXT)""");
|
2020-02-25 10:57:12 +01:00
|
|
|
await db.execute(
|
|
|
|
"""CREATE TABLE PlayHistory(id INTEGER PRIMARY KEY, title TEXT, enclosure_url TEXT UNIQUE,
|
2020-03-01 13:17:06 +01:00
|
|
|
seconds REAL, seek_value REAL, add_date INTEGER)""");
|
2020-03-14 04:14:24 +01:00
|
|
|
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-02-20 16:44:42 +01:00
|
|
|
Future<List<PodcastLocal>> getPodcastLocal(List<String> podcasts) async {
|
2020-02-20 10:09:21 +01:00
|
|
|
var dbClient = await database;
|
|
|
|
List<PodcastLocal> podcastLocal = List();
|
|
|
|
await Future.forEach(podcasts, (s) async {
|
|
|
|
List<Map> list;
|
2020-02-20 16:44:42 +01:00
|
|
|
list = await dbClient.rawQuery(
|
2020-03-01 13:17:06 +01:00
|
|
|
'SELECT id, title, imageUrl, rssUrl, primaryColor, author, imagePath , provider, link FROM PodcastLocal WHERE id = ?',
|
2020-02-20 16:44:42 +01:00
|
|
|
[s]);
|
2020-02-20 10:09:21 +01:00
|
|
|
podcastLocal.add(PodcastLocal(
|
|
|
|
list.first['title'],
|
|
|
|
list.first['imageUrl'],
|
|
|
|
list.first['rssUrl'],
|
|
|
|
list.first['primaryColor'],
|
|
|
|
list.first['author'],
|
2020-02-20 16:44:42 +01:00
|
|
|
list.first['id'],
|
2020-02-23 14:20:07 +01:00
|
|
|
list.first['imagePath'],
|
|
|
|
list.first['provider'],
|
|
|
|
list.first['link']));
|
2020-02-20 10:09:21 +01:00
|
|
|
});
|
|
|
|
return podcastLocal;
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<List<PodcastLocal>> getPodcastLocalAll() async {
|
2020-02-09 13:29:09 +01:00
|
|
|
var dbClient = await database;
|
2020-02-20 10:09:21 +01:00
|
|
|
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-02-25 10:57:12 +01:00
|
|
|
|
2020-02-09 13:29:09 +01:00
|
|
|
List<PodcastLocal> podcastLocal = List();
|
2020-02-21 16:04:02 +01:00
|
|
|
|
2020-02-09 13:29:09 +01:00
|
|
|
for (int i = 0; i < list.length; i++) {
|
2020-02-20 16:44:42 +01:00
|
|
|
podcastLocal.add(PodcastLocal(
|
|
|
|
list[i]['title'],
|
|
|
|
list[i]['imageUrl'],
|
|
|
|
list[i]['rssUrl'],
|
|
|
|
list[i]['primaryColor'],
|
|
|
|
list[i]['author'],
|
|
|
|
list[i]['id'],
|
2020-02-23 14:20:07 +01:00
|
|
|
list[i]['imagePath'],
|
|
|
|
list.first['provider'],
|
|
|
|
list.first['link']));
|
2020-02-09 13:29:09 +01:00
|
|
|
}
|
|
|
|
return podcastLocal;
|
|
|
|
}
|
|
|
|
|
2020-02-21 16:04:02 +01:00
|
|
|
Future<bool> checkPodcast(String url) async {
|
|
|
|
var dbClient = await database;
|
|
|
|
List<Map> list = await dbClient
|
|
|
|
.rawQuery('SELECT id FROM PodcastLocal WHERE rssUrl = ?', [url]);
|
|
|
|
return list.length == 0;
|
|
|
|
}
|
|
|
|
|
2020-02-09 13:29:09 +01:00
|
|
|
Future savePodcastLocal(PodcastLocal podcastLocal) async {
|
2020-02-16 09:25:53 +01:00
|
|
|
print('podcast saved in sqllite');
|
2020-02-09 13:29:09 +01:00
|
|
|
int _milliseconds = DateTime.now().millisecondsSinceEpoch;
|
|
|
|
var dbClient = await database;
|
|
|
|
await dbClient.transaction((txn) async {
|
2020-03-14 04:14:24 +01:00
|
|
|
await txn.rawInsert(
|
2020-02-20 10:09:21 +01:00
|
|
|
"""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
|
|
|
[
|
2020-02-20 10:09:21 +01:00
|
|
|
podcastLocal.id,
|
2020-02-09 13:29:09 +01:00
|
|
|
podcastLocal.title,
|
|
|
|
podcastLocal.imageUrl,
|
|
|
|
podcastLocal.rssUrl,
|
|
|
|
podcastLocal.primaryColor,
|
|
|
|
podcastLocal.author,
|
|
|
|
podcastLocal.description,
|
2020-02-20 16:44:42 +01:00
|
|
|
_milliseconds,
|
2020-02-23 14:20:07 +01:00
|
|
|
podcastLocal.imagePath,
|
|
|
|
podcastLocal.provider,
|
|
|
|
podcastLocal.link
|
2020-02-09 13:29:09 +01:00
|
|
|
]);
|
|
|
|
});
|
2020-03-14 04:14:24 +01:00
|
|
|
await dbClient.transaction((txn) async {
|
|
|
|
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 {
|
2020-02-26 10:54:59 +01:00
|
|
|
var dbClient = await database;
|
|
|
|
int 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]]);
|
2020-02-26 10:54:59 +01:00
|
|
|
print('Fireside data save in sqllite');
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-03-01 13:17:06 +01:00
|
|
|
Future<List<String>> getFiresideData(String id) async {
|
2020-02-26 10:54:59 +01:00
|
|
|
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]);
|
|
|
|
List<String> data = [list.first['background_image'], list.first['hosts']];
|
2020-02-26 10:54:59 +01:00
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2020-02-20 16:44:42 +01:00
|
|
|
Future delPodcastLocal(String id) async {
|
2020-02-09 13:29:09 +01:00
|
|
|
var dbClient = await database;
|
2020-02-20 16:44:42 +01:00
|
|
|
await dbClient.rawDelete('DELETE FROM PodcastLocal WHERE id =?', [id]);
|
2020-02-09 13:29:09 +01:00
|
|
|
List<Map> list = await dbClient.rawQuery(
|
2020-02-20 16:44:42 +01:00
|
|
|
"""SELECT downloaded FROM Episodes WHERE downloaded != 'ND' AND feed_id = ?""",
|
|
|
|
[id]);
|
2020-02-11 14:01:57 +01:00
|
|
|
for (int i = 0; i < list.length; i++) {
|
|
|
|
if (list[i] != null)
|
|
|
|
FlutterDownloader.remove(
|
|
|
|
taskId: list[i]['downloaded'], shouldDeleteContent: true);
|
|
|
|
print('Removed all download tasks');
|
2020-02-09 13:29:09 +01:00
|
|
|
}
|
2020-02-20 16:44:42 +01:00
|
|
|
await dbClient.rawDelete('DELETE FROM Episodes WHERE feed_id=?', [id]);
|
2020-03-14 04:14:24 +01:00
|
|
|
int _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
|
|
|
}
|
|
|
|
|
2020-02-25 10:57:12 +01:00
|
|
|
Future<int> saveHistory(PlayHistory history) async {
|
|
|
|
var dbClient = await database;
|
|
|
|
int _milliseconds = DateTime.now().millisecondsSinceEpoch;
|
|
|
|
int result = await dbClient.transaction((txn) async {
|
|
|
|
return await txn.rawInsert(
|
|
|
|
"""REPLACE INTO PlayHistory (title, enclosure_url, seconds, seek_value, add_date)
|
|
|
|
VALUES (?, ?, ?, ?, ?) """,
|
|
|
|
[
|
|
|
|
history.title,
|
|
|
|
history.url,
|
|
|
|
history.seconds,
|
|
|
|
history.seekValue,
|
|
|
|
_milliseconds
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-03-01 13:17:06 +01:00
|
|
|
Future<List<PlayHistory>> getPlayHistory() async {
|
|
|
|
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
|
|
|
|
""");
|
|
|
|
List<PlayHistory> playHistory = [];
|
|
|
|
list.forEach((record) {
|
2020-03-14 04:14:24 +01:00
|
|
|
playHistory.add(PlayHistory(record['title'], record['enclosure_url'],
|
|
|
|
record['seconds'], record['seek_value'],
|
|
|
|
playdate: DateTime.fromMillisecondsSinceEpoch(record['add_date'])));
|
2020-03-01 13:17:06 +01:00
|
|
|
});
|
|
|
|
return playHistory;
|
|
|
|
}
|
|
|
|
|
2020-03-14 04:14:24 +01:00
|
|
|
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]);
|
|
|
|
double sum = 0;
|
|
|
|
if (list.length == 0) {
|
|
|
|
sum = 0;
|
|
|
|
} else {
|
|
|
|
list.forEach((record) => sum += record['seconds']);
|
|
|
|
}
|
|
|
|
return (sum ~/ 60).toDouble();
|
|
|
|
}
|
|
|
|
|
2020-03-01 13:17:06 +01:00
|
|
|
Future<int> getPosition(EpisodeBrief episodeBrief) async {
|
|
|
|
var dbClient = await database;
|
|
|
|
List<Map> list = await dbClient.rawQuery(
|
|
|
|
"SELECT seconds FROM PlayHistory Where enclosure_url = ?",
|
|
|
|
[episodeBrief.enclosureUrl]);
|
|
|
|
return list.length > 0 ? list.first['seconds'] : 0;
|
|
|
|
}
|
|
|
|
|
2020-02-11 14:01:57 +01:00
|
|
|
DateTime _parsePubDate(String pubDate) {
|
2020-02-21 16:04:02 +01:00
|
|
|
if (pubDate == null) return DateTime.now();
|
|
|
|
print(pubDate);
|
2020-02-11 14:01:57 +01:00
|
|
|
DateTime date;
|
2020-02-21 16:04:02 +01:00
|
|
|
RegExp yyyy = RegExp(r'[1-2][0-9]{3}');
|
|
|
|
RegExp hhmm = RegExp(r'[0-2][0-9]\:[0-5][0-9]');
|
|
|
|
RegExp ddmmm = RegExp(r'[0-3][0-9]\s[A-Z][a-z]{2}');
|
|
|
|
RegExp mmDd = RegExp(r'([0-1]|\s)[0-9]\-[0-3][0-9]');
|
2020-03-14 04:14:24 +01:00
|
|
|
// RegExp timezone
|
|
|
|
RegExp z = RegExp(r'(\+|\-)[0-1][0-9]00');
|
|
|
|
String timezone = z.stringMatch(pubDate);
|
|
|
|
int 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 {
|
2020-02-21 16:04:02 +01:00
|
|
|
date = DateFormat('EEE, dd MMM yyyy HH:mm:ss Z', 'en_US').parse(pubDate);
|
2020-02-09 13:29:09 +01:00
|
|
|
} catch (e) {
|
2020-02-21 16:04:02 +01:00
|
|
|
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) {
|
2020-03-14 04:14:24 +01:00
|
|
|
//parse date using regex, still have issue in parse maonth/day
|
2020-02-21 16:04:02 +01:00
|
|
|
String year = yyyy.stringMatch(pubDate);
|
|
|
|
String time = hhmm.stringMatch(pubDate);
|
|
|
|
String month = ddmmm.stringMatch(pubDate);
|
|
|
|
if (year != null && time != null && month != null) {
|
|
|
|
date = DateFormat('dd MMM yyyy HH:mm', 'en_US')
|
|
|
|
.parse(month + year + time);
|
2020-02-25 10:57:12 +01:00
|
|
|
} else if (year != null && time != null && month == null) {
|
2020-02-21 16:04:02 +01:00
|
|
|
String month = mmDd.stringMatch(pubDate);
|
|
|
|
date = DateFormat('mm-dd yyyy HH:mm', 'en_US')
|
2020-02-25 10:57:12 +01:00
|
|
|
.parse(month + ' ' + year + ' ' + time);
|
|
|
|
} else {
|
2020-03-14 04:14:24 +01:00
|
|
|
date = DateTime.now().toUtc();
|
2020-02-25 10:57:12 +01:00
|
|
|
}
|
2020-02-21 16:04:02 +01:00
|
|
|
}
|
|
|
|
}
|
2020-02-09 13:29:09 +01:00
|
|
|
}
|
2020-03-14 04:14:24 +01:00
|
|
|
return date.add(Duration(hours: timezoneInt));
|
2020-02-09 13:29:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int getExplicit(bool b) {
|
|
|
|
int result;
|
|
|
|
if (b == true) {
|
|
|
|
result = 1;
|
|
|
|
return result;
|
|
|
|
} else {
|
|
|
|
result = 0;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isXimalaya(String input) {
|
|
|
|
RegExp ximalaya = RegExp(r"ximalaya.com");
|
|
|
|
return ximalaya.hasMatch(input);
|
|
|
|
}
|
|
|
|
|
2020-02-20 16:44:42 +01:00
|
|
|
Future<int> savePodcastRss(RssFeed _p, String id) async {
|
|
|
|
int _result = _p.items.length;
|
|
|
|
var dbClient = await database;
|
2020-02-21 16:04:02 +01:00
|
|
|
String _description, _url;
|
2020-02-20 16:44:42 +01:00
|
|
|
for (int i = 0; i < _result; i++) {
|
|
|
|
print(_p.items[i].title);
|
2020-02-21 16:04:02 +01:00
|
|
|
if (_p.items[i].itunes.summary != null) {
|
|
|
|
_p.items[i].itunes.summary.contains('<')
|
|
|
|
? _description = _p.items[i].itunes.summary
|
|
|
|
: _description = _p.items[i].description;
|
|
|
|
} else {
|
|
|
|
_description = _p.items[i].description;
|
|
|
|
}
|
2020-02-20 16:44:42 +01:00
|
|
|
|
|
|
|
isXimalaya(_p.items[i].enclosure.url)
|
|
|
|
? _url = _p.items[i].enclosure.url.split('=').last
|
|
|
|
: _url = _p.items[i].enclosure.url;
|
|
|
|
|
2020-02-21 16:04:02 +01:00
|
|
|
final _title = _p.items[i].itunes.title ?? _p.items[i].title;
|
2020-02-20 16:44:42 +01:00
|
|
|
final _length = _p.items[i].enclosure.length;
|
|
|
|
final _pubDate = _p.items[i].pubDate;
|
2020-02-21 16:04:02 +01:00
|
|
|
print(_pubDate);
|
2020-02-20 16:44:42 +01:00
|
|
|
final _date = _parsePubDate(_pubDate);
|
|
|
|
final _milliseconds = _date.millisecondsSinceEpoch;
|
2020-02-21 16:04:02 +01:00
|
|
|
final _duration = _p.items[i].itunes.duration?.inMinutes ?? 0;
|
2020-02-20 16:44:42 +01:00
|
|
|
final _explicit = getExplicit(_p.items[i].itunes.explicit);
|
2020-02-21 16:04:02 +01:00
|
|
|
|
|
|
|
if (_url != null) {
|
2020-02-20 16:44:42 +01:00
|
|
|
await dbClient.transaction((txn) {
|
|
|
|
return txn.rawInsert(
|
|
|
|
"""INSERT OR IGNORE INTO Episodes(title, enclosure_url, enclosure_length, pubDate,
|
2020-03-14 04:14:24 +01:00
|
|
|
description, feed_id, milliseconds, duration, explicit, media_id) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?)""",
|
2020-02-20 16:44:42 +01:00
|
|
|
[
|
|
|
|
_title,
|
|
|
|
_url,
|
|
|
|
_length,
|
|
|
|
_pubDate,
|
|
|
|
_description,
|
|
|
|
id,
|
|
|
|
_milliseconds,
|
|
|
|
_duration,
|
|
|
|
_explicit,
|
2020-03-14 04:14:24 +01:00
|
|
|
_url
|
2020-02-20 16:44:42 +01:00
|
|
|
]);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return _result;
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<int> updatePodcastRss(PodcastLocal podcastLocal) async {
|
|
|
|
Response response = await Dio().get(podcastLocal.rssUrl);
|
|
|
|
var _p = RssFeed.parse(response.data);
|
2020-02-21 16:04:02 +01:00
|
|
|
String _url, _description;
|
2020-02-09 13:29:09 +01:00
|
|
|
int _result = _p.items.length;
|
|
|
|
var dbClient = await database;
|
|
|
|
int _count = Sqflite.firstIntValue(await dbClient.rawQuery(
|
2020-02-20 16:44:42 +01:00
|
|
|
'SELECT COUNT(*) FROM Episodes WHERE feed_id = ?', [podcastLocal.id]));
|
2020-02-09 13:29:09 +01:00
|
|
|
print(_count);
|
|
|
|
if (_count == _result) {
|
|
|
|
_result = 0;
|
|
|
|
return _result;
|
|
|
|
} else {
|
|
|
|
for (int i = 0; i < (_result - _count); i++) {
|
|
|
|
print(_p.items[i].title);
|
2020-02-21 16:04:02 +01:00
|
|
|
if (_p.items[i].itunes.summary != null) {
|
|
|
|
_p.items[i].itunes.summary.contains('<')
|
|
|
|
? _description = _p.items[i].itunes.summary
|
|
|
|
: _description = _p.items[i].description;
|
|
|
|
} else {
|
|
|
|
_description = _p.items[i].description;
|
|
|
|
}
|
2020-02-20 10:09:21 +01:00
|
|
|
|
2020-02-09 13:29:09 +01:00
|
|
|
isXimalaya(_p.items[i].enclosure.url)
|
|
|
|
? _url = _p.items[i].enclosure.url.split('=').last
|
|
|
|
: _url = _p.items[i].enclosure.url;
|
2020-02-20 10:09:21 +01:00
|
|
|
|
2020-02-21 16:04:02 +01:00
|
|
|
final _title = _p.items[i].itunes.title ?? _p.items[i].title;
|
2020-02-09 13:29:09 +01:00
|
|
|
final _length = _p.items[i].enclosure.length;
|
|
|
|
final _pubDate = _p.items[i].pubDate;
|
2020-02-11 14:01:57 +01:00
|
|
|
final _date = _parsePubDate(_pubDate);
|
2020-02-09 13:29:09 +01:00
|
|
|
final _milliseconds = _date.millisecondsSinceEpoch;
|
2020-02-21 16:04:02 +01:00
|
|
|
final _duration = _p.items[i].itunes.duration?.inMinutes ?? 0;
|
2020-02-09 13:29:09 +01:00
|
|
|
final _explicit = getExplicit(_p.items[i].itunes.explicit);
|
2020-02-21 16:04:02 +01:00
|
|
|
|
|
|
|
if (_url != null) {
|
2020-02-09 13:29:09 +01:00
|
|
|
await dbClient.transaction((txn) {
|
|
|
|
return txn.rawInsert(
|
|
|
|
"""INSERT OR IGNORE INTO Episodes(title, enclosure_url, enclosure_length, pubDate,
|
2020-03-14 04:14:24 +01:00
|
|
|
description, feed_id, milliseconds, duration, explicit, media_id) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?)""",
|
2020-02-09 13:29:09 +01:00
|
|
|
[
|
|
|
|
_title,
|
|
|
|
_url,
|
|
|
|
_length,
|
|
|
|
_pubDate,
|
|
|
|
_description,
|
2020-02-20 16:44:42 +01:00
|
|
|
podcastLocal.id,
|
2020-02-09 13:29:09 +01:00
|
|
|
_milliseconds,
|
|
|
|
_duration,
|
|
|
|
_explicit,
|
2020-03-14 04:14:24 +01:00
|
|
|
_url
|
2020-02-09 13:29:09 +01:00
|
|
|
]);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2020-02-20 16:44:42 +01:00
|
|
|
return _result - _count;
|
2020-02-09 13:29:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-20 16:44:42 +01:00
|
|
|
Future<List<EpisodeBrief>> getRssItem(String id) async {
|
2020-02-09 13:29:09 +01:00
|
|
|
var dbClient = await database;
|
2020-02-21 16:04:02 +01:00
|
|
|
List<EpisodeBrief> episodes = [];
|
2020-02-09 13:29:09 +01:00
|
|
|
List<Map> list = await dbClient
|
|
|
|
.rawQuery("""SELECT E.title, E.enclosure_url, E.enclosure_length,
|
2020-02-21 16:04:02 +01:00
|
|
|
E.milliseconds, P.imagePath, P.title as feedTitle, E.duration, E.explicit, E.liked,
|
2020-03-14 04:14:24 +01:00
|
|
|
E.downloaded, P.primaryColor , E.media_id
|
2020-02-20 16:44:42 +01:00
|
|
|
FROM Episodes E INNER JOIN PodcastLocal P ON E.feed_id = P.id
|
2020-02-21 16:04:02 +01:00
|
|
|
WHERE P.id = ? ORDER BY E.milliseconds DESC""", [id]);
|
2020-02-09 13:29:09 +01:00
|
|
|
for (int x = 0; x < list.length; x++) {
|
|
|
|
episodes.add(EpisodeBrief(
|
|
|
|
list[x]['title'],
|
|
|
|
list[x]['enclosure_url'],
|
|
|
|
list[x]['enclosure_length'],
|
2020-02-21 16:04:02 +01:00
|
|
|
list[x]['milliseconds'],
|
|
|
|
list[x]['feedTitle'],
|
2020-02-09 13:29:09 +01:00
|
|
|
list[x]['primaryColor'],
|
|
|
|
list[x]['liked'],
|
|
|
|
list[x]['downloaded'],
|
|
|
|
list[x]['duration'],
|
2020-02-20 16:44:42 +01:00
|
|
|
list[x]['explicit'],
|
2020-03-14 04:14:24 +01:00
|
|
|
list[x]['imagePath'],
|
|
|
|
list[x]['media_id']));
|
2020-02-09 13:29:09 +01:00
|
|
|
}
|
|
|
|
return episodes;
|
|
|
|
}
|
|
|
|
|
2020-02-20 16:44:42 +01:00
|
|
|
Future<List<EpisodeBrief>> getRssItemTop(String id) async {
|
2020-02-09 13:29:09 +01:00
|
|
|
var dbClient = await database;
|
|
|
|
List<EpisodeBrief> episodes = List();
|
2020-02-20 16:44:42 +01:00
|
|
|
List<Map> list = await dbClient
|
|
|
|
.rawQuery("""SELECT E.title, E.enclosure_url, E.enclosure_length,
|
2020-02-21 16:04:02 +01:00
|
|
|
E.milliseconds, P.imagePath, P.title as feed_title, E.duration, E.explicit, E.liked,
|
2020-03-14 04:14:24 +01:00
|
|
|
E.downloaded, P.primaryColor, E.media_id
|
2020-02-20 16:44:42 +01:00
|
|
|
FROM Episodes E INNER JOIN PodcastLocal P ON E.feed_id = P.id
|
|
|
|
where E.feed_id = ? ORDER BY E.milliseconds DESC LIMIT 3""", [id]);
|
2020-02-09 13:29:09 +01:00
|
|
|
for (int x = 0; x < list.length; x++) {
|
|
|
|
episodes.add(EpisodeBrief(
|
|
|
|
list[x]['title'],
|
|
|
|
list[x]['enclosure_url'],
|
|
|
|
list[x]['enclosure_length'],
|
2020-02-21 16:04:02 +01:00
|
|
|
list[x]['milliseconds'],
|
2020-02-09 13:29:09 +01:00
|
|
|
list[x]['feed_title'],
|
|
|
|
list[x]['primaryColor'],
|
|
|
|
list[x]['liked'],
|
|
|
|
list[x]['downloaded'],
|
|
|
|
list[x]['duration'],
|
2020-02-20 16:44:42 +01:00
|
|
|
list[x]['explicit'],
|
2020-03-14 04:14:24 +01:00
|
|
|
list[x]['imagePath'],
|
|
|
|
list[x]['media_id']));
|
2020-02-09 13:29:09 +01:00
|
|
|
}
|
|
|
|
return episodes;
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<EpisodeBrief> getRssItemDownload(String url) async {
|
|
|
|
var dbClient = await database;
|
|
|
|
EpisodeBrief episode;
|
|
|
|
List<Map> list = await dbClient.rawQuery(
|
|
|
|
"""SELECT E.title, E.enclosure_url, E.enclosure_length,
|
2020-02-21 16:04:02 +01:00
|
|
|
E.milliseconds, P.imagePath, P.title as feed_title, E.duration, E.explicit, E.liked,
|
2020-03-14 04:14:24 +01:00
|
|
|
E.downloaded, P.primaryColor, E.media_id
|
2020-02-20 16:44:42 +01:00
|
|
|
FROM Episodes E INNER JOIN PodcastLocal P ON E.feed_id = P.id
|
2020-02-09 13:29:09 +01:00
|
|
|
where E.enclosure_url = ? ORDER BY E.milliseconds DESC LIMIT 3""",
|
|
|
|
[url]);
|
|
|
|
|
|
|
|
if (list != null)
|
|
|
|
episode = EpisodeBrief(
|
|
|
|
list.first['title'],
|
|
|
|
list.first['enclosure_url'],
|
|
|
|
list.first['enclosure_length'],
|
2020-02-21 16:04:02 +01:00
|
|
|
list.first['milliseconds'],
|
2020-02-09 13:29:09 +01:00
|
|
|
list.first['feed_title'],
|
|
|
|
list.first['primaryColor'],
|
|
|
|
list.first['liked'],
|
|
|
|
list.first['downloaded'],
|
|
|
|
list.first['duration'],
|
2020-02-20 16:44:42 +01:00
|
|
|
list.first['explicit'],
|
2020-03-14 04:14:24 +01:00
|
|
|
list.first['imagePath'],
|
|
|
|
list.first['media_id']);
|
2020-02-09 13:29:09 +01:00
|
|
|
return episode;
|
|
|
|
}
|
|
|
|
|
2020-03-14 04:14:24 +01:00
|
|
|
Future<List<EpisodeBrief>> getRecentRssItem(int top) async {
|
2020-02-09 13:29:09 +01:00
|
|
|
var dbClient = await database;
|
|
|
|
List<EpisodeBrief> episodes = List();
|
|
|
|
List<Map> list = await dbClient
|
|
|
|
.rawQuery("""SELECT E.title, E.enclosure_url, E.enclosure_length,
|
2020-02-21 16:04:02 +01:00
|
|
|
E.milliseconds, P.title as feed_title, E.duration, E.explicit, E.liked,
|
2020-03-14 04:14:24 +01:00
|
|
|
E.downloaded, P.imagePath, P.primaryColor, E.media_id
|
2020-02-20 16:44:42 +01:00
|
|
|
FROM Episodes E INNER JOIN PodcastLocal P ON E.feed_id = P.id
|
2020-03-14 04:14:24 +01:00
|
|
|
ORDER BY E.milliseconds DESC LIMIT ? """, [top]);
|
2020-02-09 13:29:09 +01:00
|
|
|
for (int x = 0; x < list.length; x++) {
|
|
|
|
episodes.add(EpisodeBrief(
|
|
|
|
list[x]['title'],
|
|
|
|
list[x]['enclosure_url'],
|
|
|
|
list[x]['enclosure_length'],
|
2020-02-21 16:04:02 +01:00
|
|
|
list[x]['milliseconds'],
|
2020-02-09 13:29:09 +01:00
|
|
|
list[x]['feed_title'],
|
|
|
|
list[x]['primaryColor'],
|
|
|
|
list[x]['liked'],
|
|
|
|
list[x]['doanloaded'],
|
|
|
|
list[x]['duration'],
|
2020-02-20 16:44:42 +01:00
|
|
|
list[x]['explicit'],
|
2020-03-14 04:14:24 +01:00
|
|
|
list[x]['imagePath'],
|
|
|
|
list[x]['media_id']));
|
2020-02-09 13:29:09 +01:00
|
|
|
}
|
|
|
|
return episodes;
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<List<EpisodeBrief>> getLikedRssItem() async {
|
|
|
|
var dbClient = await database;
|
|
|
|
List<EpisodeBrief> episodes = List();
|
|
|
|
List<Map> list = await dbClient.rawQuery(
|
2020-02-21 16:04:02 +01:00
|
|
|
"""SELECT E.title, E.enclosure_url, E.enclosure_length, E.milliseconds, P.imagePath,
|
2020-02-20 16:44:42 +01:00
|
|
|
P.title as feed_title, E.duration, E.explicit, E.liked, E.downloaded,
|
2020-03-14 04:14:24 +01:00
|
|
|
P.primaryColor, E.media_id FROM Episodes E INNER JOIN PodcastLocal P ON E.feed_id = P.id
|
2020-02-09 13:29:09 +01:00
|
|
|
WHERE E.liked = 1 ORDER BY E.milliseconds DESC LIMIT 99""");
|
|
|
|
for (int x = 0; x < list.length; x++) {
|
|
|
|
episodes.add(EpisodeBrief(
|
|
|
|
list[x]['title'],
|
|
|
|
list[x]['enclosure_url'],
|
|
|
|
list[x]['enclosure_length'],
|
2020-02-21 16:04:02 +01:00
|
|
|
list[x]['milliseconds'],
|
2020-02-09 13:29:09 +01:00
|
|
|
list[x]['feed_title'],
|
|
|
|
list[x]['primaryColor'],
|
|
|
|
list[x]['liked'],
|
|
|
|
list[x]['downloaded'],
|
|
|
|
list[x]['duration'],
|
2020-02-20 16:44:42 +01:00
|
|
|
list[x]['explicit'],
|
2020-03-14 04:14:24 +01:00
|
|
|
list[x]['imagePath'],
|
|
|
|
list[x]['media_id']));
|
2020-02-09 13:29:09 +01:00
|
|
|
}
|
|
|
|
return episodes;
|
|
|
|
}
|
|
|
|
|
2020-02-20 16:44:42 +01:00
|
|
|
Future<int> setLiked(String url) async {
|
2020-02-09 13:29:09 +01:00
|
|
|
var dbClient = await database;
|
2020-02-20 16:44:42 +01:00
|
|
|
int count = await dbClient.rawUpdate(
|
|
|
|
"UPDATE Episodes SET liked = 1 WHERE enclosure_url= ?", [url]);
|
2020-02-09 13:29:09 +01:00
|
|
|
print('liked');
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2020-02-20 16:44:42 +01:00
|
|
|
Future<int> setUniked(String url) async {
|
2020-02-09 13:29:09 +01:00
|
|
|
var dbClient = await database;
|
2020-02-20 16:44:42 +01:00
|
|
|
int count = await dbClient.rawUpdate(
|
|
|
|
"UPDATE Episodes SET liked = 0 WHERE enclosure_url = ?", [url]);
|
2020-02-09 13:29:09 +01:00
|
|
|
print('unliked');
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<int> saveDownloaded(String url, String id) async {
|
|
|
|
var dbClient = await database;
|
|
|
|
int _milliseconds = DateTime.now().millisecondsSinceEpoch;
|
|
|
|
int count = await dbClient.rawUpdate(
|
|
|
|
"UPDATE Episodes SET downloaded = ?, download_date = ? WHERE enclosure_url = ?",
|
|
|
|
[id, _milliseconds, url]);
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2020-03-14 04:14:24 +01:00
|
|
|
|
|
|
|
Future<int> saveMediaId(String url, String path) async {
|
|
|
|
var dbClient = await database;
|
|
|
|
int _milliseconds = DateTime.now().millisecondsSinceEpoch;
|
|
|
|
int count = await dbClient.rawUpdate(
|
|
|
|
"UPDATE Episodes SET media_id = ?, download_date = ? WHERE enclosure_url = ?",
|
|
|
|
[path, _milliseconds, 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.rawUpdate(
|
2020-03-14 04:14:24 +01:00
|
|
|
"UPDATE Episodes SET downloaded = 'ND', media_id = ? WHERE enclosure_url = ?", [url, url]);
|
2020-02-09 13:29:09 +01:00
|
|
|
print('Deleted ' + url);
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<List<EpisodeBrief>> getDownloadedRssItem() async {
|
|
|
|
var dbClient = await database;
|
|
|
|
List<EpisodeBrief> episodes = List();
|
|
|
|
List<Map> list = await dbClient.rawQuery(
|
2020-02-21 16:04:02 +01:00
|
|
|
"""SELECT E.title, E.enclosure_url, E.enclosure_length, E.milliseconds, P.imagePath,
|
2020-02-20 16:44:42 +01:00
|
|
|
P.title as feed_title, E.duration, E.explicit, E.liked, E.downloaded,
|
2020-03-14 04:14:24 +01:00
|
|
|
P.primaryColor, E.media_id FROM Episodes E INNER JOIN PodcastLocal P ON E.feed_id = P.id
|
2020-02-09 13:29:09 +01:00
|
|
|
WHERE E.downloaded != 'ND' ORDER BY E.download_date DESC LIMIT 99""");
|
|
|
|
for (int x = 0; x < list.length; x++) {
|
|
|
|
episodes.add(EpisodeBrief(
|
|
|
|
list[x]['title'],
|
|
|
|
list[x]['enclosure_url'],
|
|
|
|
list[x]['enclosure_length'],
|
2020-02-21 16:04:02 +01:00
|
|
|
list[x]['milliseconds'],
|
2020-02-09 13:29:09 +01:00
|
|
|
list[x]['feed_title'],
|
|
|
|
list[x]['primaryColor'],
|
|
|
|
list[x]['liked'],
|
|
|
|
list[x]['downloaded'],
|
|
|
|
list[x]['duration'],
|
2020-02-20 16:44:42 +01:00
|
|
|
list[x]['explicit'],
|
2020-03-14 04:14:24 +01:00
|
|
|
list[x]['imagePath'],
|
|
|
|
list[x]['media_id']));
|
2020-02-09 13:29:09 +01:00
|
|
|
}
|
|
|
|
return episodes;
|
|
|
|
}
|
|
|
|
|
2020-02-20 16:44:42 +01:00
|
|
|
Future<String> getDescription(String url) async {
|
2020-02-09 13:29:09 +01:00
|
|
|
var dbClient = await database;
|
2020-02-20 16:44:42 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-02-21 16:04:02 +01:00
|
|
|
Future<String> getFeedDescription(String id) async {
|
2020-02-09 13:29:09 +01:00
|
|
|
var dbClient = await database;
|
2020-02-25 10:57:12 +01:00
|
|
|
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;
|
|
|
|
}
|
2020-02-14 14:22:35 +01:00
|
|
|
|
2020-02-16 09:25:53 +01:00
|
|
|
Future<EpisodeBrief> getRssItemWithUrl(String url) async {
|
2020-02-14 14:22:35 +01:00
|
|
|
var dbClient = await database;
|
|
|
|
EpisodeBrief episode;
|
|
|
|
List<Map> list = await dbClient.rawQuery(
|
2020-02-21 16:04:02 +01:00
|
|
|
"""SELECT E.title, E.enclosure_url, E.enclosure_length, E.milliseconds, P.imagePath,
|
2020-02-20 16:44:42 +01:00
|
|
|
P.title as feed_title, E.duration, E.explicit, E.liked, E.downloaded,
|
2020-03-14 04:14:24 +01:00
|
|
|
P.primaryColor, E.media_id FROM Episodes E INNER JOIN PodcastLocal P ON E.feed_id = P.id
|
2020-02-16 09:25:53 +01:00
|
|
|
WHERE E.enclosure_url = ?""", [url]);
|
|
|
|
episode = EpisodeBrief(
|
|
|
|
list.first['title'],
|
|
|
|
list.first['enclosure_url'],
|
|
|
|
list.first['enclosure_length'],
|
2020-02-21 16:04:02 +01:00
|
|
|
list.first['milliseconds'],
|
2020-02-16 09:25:53 +01:00
|
|
|
list.first['feed_title'],
|
|
|
|
list.first['primaryColor'],
|
|
|
|
list.first['liked'],
|
|
|
|
list.first['downloaded'],
|
|
|
|
list.first['duration'],
|
2020-02-20 16:44:42 +01:00
|
|
|
list.first['explicit'],
|
2020-03-14 04:14:24 +01:00
|
|
|
list.first['imagePath'],
|
|
|
|
list.first['media_id']);
|
|
|
|
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,
|
|
|
|
P.title as feed_title, E.duration, E.explicit, E.liked, E.downloaded,
|
|
|
|
P.primaryColor, E.media_id FROM Episodes E INNER JOIN PodcastLocal P ON E.feed_id = P.id
|
|
|
|
WHERE E.media_id = ?""", [id]);
|
|
|
|
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['liked'],
|
|
|
|
list.first['downloaded'],
|
|
|
|
list.first['duration'],
|
|
|
|
list.first['explicit'],
|
|
|
|
list.first['imagePath'],
|
|
|
|
list.first['media_id']);
|
2020-02-14 14:22:35 +01:00
|
|
|
return episode;
|
|
|
|
}
|
2020-03-14 04:14:24 +01:00
|
|
|
|
2020-02-09 13:29:09 +01:00
|
|
|
}
|