IMDb plugin + downloader: parse IMDb plots
[cinaest] / src / imdb / imdb-line-parser.vala
index 1218cbf..7963fda 100644 (file)
@@ -226,3 +226,89 @@ class AkaLineParser : LineParser {
                }
        }
 }
+
+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;
+                       }
+               }
+       }
+}