summaryrefslogtreecommitdiff
path: root/generatewebpages.py
blob: 60d313377615fe9c78751cbbfb819fdc84938ac2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#!/usr/bin/env python3

"""
generatewebpages.py is part of animedb.
The purpose of this program is to generate all our html pages.
Copyright (C) 2018 Daniel Jones daniel@danieljon.es 

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 2
of the License, or (at your option) 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, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
"""

import argparse;
import sqlite3;
import os;
import re;
from os import fdopen, remove;
from shutil import copyfile;
from tempfile import mkstemp;
from shutil import move;

class Anime:
    """
    every anime from the database will get an instance of this class
    """
    colors = {  
            "Plan to Watch": "gray", 
            "On-Hold": "orange", 
            "Watching": "blue", 
            "Completed": "green",
            };

    def __init__(self, animeobj):
        self.dbpos = animeobj[0];
        self.animeid = animeobj[1];
        self.title = animeobj[2].replace("\"", """);
        self.episodes = animeobj[3];
        self.type = animeobj[4];
        self.watched = animeobj[5];
        self.score = animeobj[6];
        self.status = animeobj[7];
        self.notes = str(animeobj[8]).replace("\"", """);
        self.output = ""; # our final html string

    def setstatuscolor(self, status):
        try:
            self.statuscolor = Anime.colors[status];
        except KeyError:
            self.statuscolor = "black";

def dbconnect(dbname):
    """
    connect to our database and return the object
    """
    try:
        dbcon = sqlite3.connect(dbname);
    except:
        e = sys.exc_info()[0];
        exit(e);
    return dbcon; 

def dbclose(db):
    """
    close database
    """
    db.close();

def dbcollectanimedata(db):
    """
    collect anime data from the database
    """
    animelist = [];
    c = db.cursor();
    for anime in c.execute("SELECT * FROM anime;"):
        animelist.append(anime);
    return animelist;

def replacestringinfile(file_path, string, substr):
    """
    replaces contents in a file
    """
    fh, abs_path = mkstemp();
    with fdopen(fh, "w") as new_file:
        with open(file_path) as old_file:
            for line in old_file:
                new_file.write(line.replace(string, substr));
    remove(file_path);
    move(abs_path, file_path);

def replacestring(string, substr, replacement):
    return string.replace(substr, replacement);

def insertanimedata(anime):
    tmp = animetemplate;
    tmp = replacestring(tmp, "{ANIME_UNDERLINED_TITLE}", re.sub(r'[^\x00-\x7f]',r'', anime.title.replace(" ", "_")));
    tmp = replacestring(tmp, "{ANIME_ID}", str(anime.animeid));
    tmp = replacestring(tmp, "{ANIME_TITLE}", re.sub(r'[^\x00-\x7f]',r'', anime.title));
    tmp = replacestring(tmp, "{TOTAL_EPISODES}", str(anime.episodes));
    tmp = replacestring(tmp, "{EPISODES_WATCHED}", str(anime.watched));
    tmp = replacestring(tmp, "{WATCH_STATUS_COLOR}", anime.statuscolor);
    tmp = replacestring(tmp, "{WATCH_STATUS}", anime.status);
    tmp = replacestring(tmp, "{RATING}", str(anime.score));
    tmp = replacestring(tmp, "{NOTES}", re.sub(r'[^\x00-\x7f]',r'', str(anime.notes)));
    anime.output = tmp;

# this template is used for every anime we display
animetemplate = (
        "<div class=\"box\">\n"
        "<div class=\"anime\">\n"
        "<div id=\"{ANIME_UNDERLINED_TITLE}\">\n"
        "<img width=96 height=137 src=\"covers/{ANIME_ID}.jpg\" alt=\"{ANIME_TITLE} cover\">\n"
        "<div class=\"viewing\">\n"
        "{ANIME_TITLE} <br>\n"
        "{TOTAL_EPISODES} episodes ({EPISODES_WATCHED} watched) <br>\n"
        "status: <font color=\"{WATCH_STATUS_COLOR}\">{WATCH_STATUS}</font> <br>\n"
        "rating: {RATING}/10 <br>\n"
        "notes: {NOTES}<br>\n"
        "</div>\n"
        "</div>\n"
        "</div>\n"
        "</div>\n");

if __name__ == "__main__":
    """
    parse arguments
    connect to database
    generate anime strings from template and database
    close database
    """

    parser = argparse.ArgumentParser();
    parser.add_argument("-d", "--database", type=str, action="store", dest="dbfile",
            default="userdb.db", required=False,
            help="sqlite3 database file containing anime information");
    parser.add_argument("-o", "--output", type=str, action="store", dest="outdir",
            default="output", required=False,
            help="directory to output generated pages");
    args = parser.parse_args();

    # if our database (file) doesn't exist, exit
    if not os.path.isfile(args.dbfile):
        exit("file doesn't exist {}".format(args.dbfile));

    db = dbconnect(args.dbfile);

    # collect anime from database
    animelist = dbcollectanimedata(db);

    # create an instance of Anime for each anime
    formattedanimelist = [];
    for anime in animelist:
        formattedanimelist.append(Anime(anime));

    # set the status color for each anime
    for anime in formattedanimelist:
        anime.setstatuscolor(anime.status);

    # insert anime data for each Anime instance
    for anime in formattedanimelist:
        insertanimedata(anime);
    
    # copy index.txt to output/index.html
    copyfile("index.txt", "output/index.html");

    # generate string containing all anime output strings
    output = [];
    for anime in formattedanimelist:
        output.append(anime.output);

    # place output into index.html
    replacestringinfile("output/index.html", "{CONTENT}", " ".join(output));

    dbclose(db);