~michael-sheldon/podbird/snap

« back to all changes in this revision

Viewing changes to app/podcasts.js

  • Committer: Michael Sheldon
  • Date: 2015-01-04 10:20:41 UTC
  • Revision ID: michael.sheldon@canonical.com-20150104102041-7ywa80x963eqppeb
Initial commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright 2015 Michael Sheldon <mike@mikeasoft.com>
 
3
 *
 
4
 * This file is part of Podbird.
 
5
 *
 
6
 * Podbird is free software; you can redistribute it and/or modify
 
7
 * it under the terms of the GNU General Public License as published by
 
8
 * the Free Software Foundation; version 3.
 
9
 *
 
10
 * Podbird is distributed in the hope that it will be useful,
 
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
 * GNU General Public License for more details.
 
14
 *
 
15
 * You should have received a copy of the GNU General Public License
 
16
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
17
 */
 
18
 
 
19
function init() {
 
20
    var db = LocalStorage.openDatabaseSync("Podbird", "1.0", "Database of subscribed podcasts and their episodes", 1000000);
 
21
    db.transaction(function(tx) {
 
22
        tx.executeSql('CREATE TABLE IF NOT EXISTS Podcast(artist TEXT, name TEXT, description TEXT, feed TEXT, image TEXT, lastupdate TIMESTAMP)');
 
23
        tx.executeSql('CREATE TABLE IF NOT EXISTS Episode(guid TEXT, podcast INTEGER, name TEXT, subtitle TEXT, description TEXT, duration INTEGER, audiourl TEXT, downloadedfile TEXT, published TIMESTAMP, listened BOOLEAN, position INTEGER, FOREIGN KEY(podcast) REFERENCES Podcast(rowid))');
 
24
    });
 
25
    return db;
 
26
}
 
27
 
 
28
function subscribe(artist, name, feed, img) {
 
29
    var db = init();
 
30
    db.transaction(function(tx) {
 
31
        var rs = tx.executeSql("SELECT rowid FROM Podcast WHERE feed = ?", feed);
 
32
        if (rs.rows.length === 0) {
 
33
            tx.executeSql("INSERT INTO Podcast(artist, name, feed, image) VALUES(?, ?, ?, ?)", [artist, name, feed, img]);
 
34
        }
 
35
    });
 
36
}
 
37
 
 
38
function formatTime(seconds) {
 
39
    var rem = seconds % 3600;
 
40
    return Math.floor(seconds / 3600) + ":" + zeroFill(Math.floor(rem / 60), 2);
 
41
}
 
42
 
 
43
function zeroFill(n, width) {
 
44
    width -= n.toString().length;
 
45
    if (width > 0) {
 
46
        return new Array(width + (/\./.test(n) ? 2 : 1)).join('0') + n;
 
47
    }
 
48
    return n + "";
 
49
}