Use general_no_thumbnail instead of imageviewer_no_pic if the movie has no poster
[cinaest] / src / imdb / imdb-line-parser.vala
index fd3875a..7963fda 100644 (file)
@@ -93,10 +93,16 @@ class GenreLineParser : LineParser {
 }
 
 class RatingLineParser : LineParser {
+       enum RatingState {
+               HEADER,
+               NONE
+       }
+       RatingState state;
        Regex re_rating;
 
        public RatingLineParser (IMDbSqlite _sqlite) {
                base (_sqlite);
+               state = RatingState.HEADER;
                try {
                        re_rating = new Regex ("^      .+ +([0-9]+) +([0-9.]+) +(.+)$");
                } catch (RegexError e) {
@@ -105,6 +111,15 @@ class RatingLineParser : LineParser {
        }
 
        public override void parse_line (string line) {
+               if (state == RatingState.HEADER) {
+                       if (line == "MOVIE RATINGS REPORT")
+                               state = RatingState.NONE;
+                       return;
+               }
+
+               if (state != RatingState.NONE)
+                       return;
+
                MatchInfo matchinfo;
 
                // Skip series episodes
@@ -117,7 +132,7 @@ class RatingLineParser : LineParser {
                string title;
                string votes = matchinfo.fetch (1);
                string rating = matchinfo.fetch (2);
-               try {
+               try {
                        title = convert(matchinfo.fetch (3), -1, "utf-8", "latin1");
                } catch (ConvertError e) {
                        return;
@@ -133,3 +148,167 @@ class RatingLineParser : LineParser {
                sqlite.movie_set_rating (title, (int) (rating.to_double () * 10), votes.to_int ());
        }
 }
+
+class AkaLineParser : LineParser {
+       enum AkaState {
+               HEADER,
+               NONE,
+               TITLE
+       }
+       AkaState state;
+       string title;
+
+       public AkaLineParser (IMDbSqlite _sqlite) {
+               base (_sqlite);
+               state = AkaState.HEADER;
+               title = null;
+       }
+
+       public override void parse_line (string line) {
+               if (state == AkaState.HEADER) {
+                       if (line == "AKA TITLES LIST") title = line;
+                       if (line == "===============" && title != null)
+                               state = AkaState.NONE;
+                       return;
+               }
+
+               if (state == AkaState.NONE) {
+                       // Skip empty lines
+                       if (line == "")
+                               return;
+
+                       // Skip series episodes
+                       if (line[0] == '"')
+                               return;
+
+                       // Parse error
+                       if (line[0] == ' ')
+                               return;
+
+                       try {
+                               title = convert (line, -1, "utf-8", "latin1");
+                       } catch (ConvertError e) {
+                               title = null;
+                               return;
+                       }
+
+                       if (skip_title (title))
+                               return;
+
+                       state = AkaState.TITLE;
+               }
+
+               if (state == AkaState.TITLE) {
+                       // Empty lines mark end of title
+                       if (line == "") {
+                               state = AkaState.NONE;
+                               return;
+                       }
+
+                       if (line.has_prefix ("   (aka ")) {
+                               if (skip_title (title))
+                                       return;
+
+                               char* start = line.offset (8);
+                               char* end = ((string) start).str ("))");
+                               if (end != null)
+                                       end[1] = '\0';
+
+                               string aka;
+                               try {
+                                       aka = convert ((string) start, -1, "utf-8", "latin1");
+                               } catch (ConvertError e) {
+                                       return;
+                               }
+
+                               sqlite.add_aka (title, aka);
+                       }
+               }
+       }
+}
+
+class PlotLineParser : LineParser {
+       enum PlotState {
+               HEADER,
+               NONE,
+               TITLE
+       }
+       string title;
+       string plot;
+       PlotState state;
+
+       public PlotLineParser (IMDbSqlite _sqlite) {
+               base (_sqlite);
+               state = PlotState.HEADER;
+               title = null;
+       }
+
+       public override void parse_line (string line) {
+               if (state == PlotState.HEADER) {
+                       if (line == "PLOT SUMMARIES LIST") title = line;
+                       if (line == "===================" && title != null)
+                               state = PlotState.NONE;
+                       return;
+               }
+
+               // Skip empty lines
+               if (line == "")
+                       return;
+
+               if (state == PlotState.NONE) {
+                       if (line.has_prefix ("MV: ")) {
+                               // Skip series episodes
+                               if (line[4] == '"')
+                                       return;
+
+                               try {
+                                       title = convert (line.offset (4), -1, "utf-8", "latin1");
+                               } catch (ConvertError e) {
+                                       stderr.printf ("Error converting title to UTF-8\n");
+                                       title = null;
+                                       return;
+                               }
+
+                               if (skip_title (title))
+                                       return;
+
+                               state = PlotState.TITLE;
+                               plot = "";
+                       }
+                       return;
+               }
+
+               if (state == PlotState.TITLE) {
+                       if (line.has_prefix ("PL: ")) {
+                               if (skip_title (title))
+                                       return;
+
+                               try {
+                                       if (plot != "")
+                                               plot += " ";
+                                       plot += convert (line.offset (4), -1, "utf-8", "latin1");
+                               } catch (ConvertError e) {
+                                       stderr.printf ("Error converting plot for \"%s\" to UTF-8\n", title);
+                                       plot = "";
+                                       return;
+                               }
+                       }
+
+                       // BY: tag marks end of plot
+                       if (line.has_prefix ("BY: ")) {
+                               string author;
+                               try {
+                                       author = convert (line.offset (4), -1, "utf-8", "latin1");
+                               } catch (ConvertError e) {
+                                       stderr.printf ("Error converting plot author for \"%s\" to UTF-8\n", title);
+                                       author = null;
+                               }
+
+                               sqlite.add_plot (title, plot, author);
+
+                               state = PlotState.NONE;
+                               return;
+                       }
+               }
+       }
+}