IMDb plaintext downloader: move line parsers into their own source file
authorPhilipp Zabel <philipp.zabel@gmail.com>
Mon, 23 Nov 2009 13:12:11 +0000 (14:12 +0100)
committerPhilipp Zabel <philipp.zabel@gmail.com>
Tue, 24 Nov 2009 09:52:09 +0000 (10:52 +0100)
Makefile.am
src/imdb/imdb-line-parser.vala [new file with mode: 0644]
src/imdb/imdb-plaintext-downloader.vala

index cae5825..0845f33 100644 (file)
@@ -167,12 +167,14 @@ src/plugins/imdb-plugin.c: ${libimdb_plugin_la_VALASOURCES}
 imdb_plaintext_downloader_SOURCES = \
         src/imdb/imdb-plaintext-downloader.c \
         src/imdb/gzip-input-stream.c \
+       src/imdb/imdb-line-parser.c \
         src/imdb/imdb-sqlite.c \
         src/imdb/plaintext-downloader-interface.c
 
 imdb_plaintext_downloader_VALASOURCES = \
         src/imdb/imdb-plaintext-downloader.vala \
         src/imdb/gzip-input-stream.vala \
+       src/imdb/imdb-line-parser.vala \
         src/imdb/imdb-sqlite.vala \
         src/imdb/plaintext-downloader-interface.vala
 
diff --git a/src/imdb/imdb-line-parser.vala b/src/imdb/imdb-line-parser.vala
new file mode 100644 (file)
index 0000000..fd3875a
--- /dev/null
@@ -0,0 +1,135 @@
+abstract class LineParser {
+        internal unowned IMDbSqlite sqlite;
+
+       public LineParser (IMDbSqlite _sqlite) {
+               sqlite = _sqlite;
+       }
+
+       public abstract void parse_line (string line);
+
+       internal bool skip_title (string title) {
+               if (title.has_suffix ("(TV)")) {
+                       return true;
+               }
+               if (title.has_suffix ("(V)")) {
+                       return true;
+               }
+               if (title.has_suffix ("(VG)")) {
+                       return true;
+               }
+               return false;
+       }
+}
+
+class MovieLineParser : LineParser {
+       Regex re_movie;
+
+       public MovieLineParser (IMDbSqlite _sqlite) {
+               base (_sqlite);
+               try {
+                       re_movie = new Regex ("^([^\t]+)\t+([0-9]+)$");
+               } catch (RegexError e) {
+                       critical ("Failed to initialize regex: %s\n", e.message);
+               }
+       }
+
+       public override void parse_line (string line) {
+               MatchInfo matchinfo;
+
+               // Skip series episodes
+               if (line[0] == '"')
+                       return;
+
+               if (!re_movie.match(line, 0, out matchinfo))
+                       return;
+
+               string title;
+               string year = matchinfo.fetch (2);
+               try {
+                       title = convert(matchinfo.fetch (1), -1, "utf-8", "latin1");
+               } catch (ConvertError e) {
+                       return;
+               }
+
+               if (skip_title (title))
+                       return;
+
+               sqlite.add_movie (title, year.to_int ());
+       }
+}
+
+class GenreLineParser : LineParser {
+       Regex re_genre;
+
+       public GenreLineParser (IMDbSqlite _sqlite) {
+               base (_sqlite);
+               try {
+                       re_genre = new Regex ("^([^\t]+)\t+([A-Za-z-]+)$");
+               } catch (RegexError e) {
+                       critical ("Failed to initialize regex: %s\n", e.message);
+               }
+       }
+
+       public override void parse_line (string line) {
+               MatchInfo matchinfo;
+
+               // Skip series episodes
+               if (line[0] == '"')
+                       return;
+
+               if (!re_genre.match(line, 0, out matchinfo))
+                       return;
+
+               string title;
+               string genre = matchinfo.fetch (2);
+               try {
+                       title = convert(matchinfo.fetch (1), -1, "utf-8", "latin1");
+               } catch (ConvertError e) {
+                       return;
+               }
+
+               sqlite.movie_add_genre (title, genre);
+       }
+}
+
+class RatingLineParser : LineParser {
+       Regex re_rating;
+
+       public RatingLineParser (IMDbSqlite _sqlite) {
+               base (_sqlite);
+               try {
+                       re_rating = new Regex ("^      .+ +([0-9]+) +([0-9.]+) +(.+)$");
+               } catch (RegexError e) {
+                       critical ("Failed to initialize regex: %s\n", e.message);
+               }
+       }
+
+       public override void parse_line (string line) {
+               MatchInfo matchinfo;
+
+               // Skip series episodes
+               if (line[0] == '"')
+                       return;
+
+               if (!re_rating.match(line, 0, out matchinfo))
+                       return;
+
+               string title;
+               string votes = matchinfo.fetch (1);
+               string rating = matchinfo.fetch (2);
+               try {
+                       title = convert(matchinfo.fetch (3), -1, "utf-8", "latin1");
+               } catch (ConvertError e) {
+                       return;
+               }
+
+               // Skip series episodes
+               if (title[0] == '"')
+                       return;
+
+               if (skip_title (title))
+                       return;
+
+               sqlite.movie_set_rating (title, (int) (rating.to_double () * 10), votes.to_int ());
+       }
+}
index bddfff0..74efd39 100644 (file)
@@ -225,139 +225,3 @@ class IMDbDownloadServer : Object, IMDbDownloader {
                }
        }
 }
-
-abstract class LineParser {
-        internal unowned IMDbSqlite sqlite;
-
-       public LineParser (IMDbSqlite _sqlite) {
-               sqlite = _sqlite;
-       }
-
-       public abstract void parse_line (string line);
-
-       internal bool skip_title (string title) {
-               if (title.has_suffix ("(TV)")) {
-                       return true;
-               }
-               if (title.has_suffix ("(V)")) {
-                       return true;
-               }
-               if (title.has_suffix ("(VG)")) {
-                       return true;
-               }
-               return false;
-       }
-}
-
-class MovieLineParser : LineParser {
-       Regex re_movie;
-
-       public MovieLineParser (IMDbSqlite _sqlite) {
-               base (_sqlite);
-               try {
-                       re_movie = new Regex ("^([^\t]+)\t+([0-9]+)$");
-               } catch (RegexError e) {
-                       critical ("Failed to initialize regex: %s\n", e.message);
-               }
-       }
-
-       public override void parse_line (string line) {
-               MatchInfo matchinfo;
-
-               // Skip series episodes
-               if (line[0] == '"')
-                       return;
-
-               if (!re_movie.match(line, 0, out matchinfo))
-                       return;
-
-               string title;
-               string year = matchinfo.fetch (2);
-               try {
-                       title = convert(matchinfo.fetch (1), -1, "utf-8", "latin1");
-               } catch (ConvertError e) {
-                       return;
-               }
-
-               if (skip_title (title))
-                       return;
-
-               sqlite.add_movie (title, year.to_int ());
-       }
-}
-
-class GenreLineParser : LineParser {
-       Regex re_genre;
-
-       public GenreLineParser (IMDbSqlite _sqlite) {
-               base (_sqlite);
-               try {
-                       re_genre = new Regex ("^([^\t]+)\t+([A-Za-z-]+)$");
-               } catch (RegexError e) {
-                       critical ("Failed to initialize regex: %s\n", e.message);
-               }
-       }
-
-       public override void parse_line (string line) {
-               MatchInfo matchinfo;
-
-               // Skip series episodes
-               if (line[0] == '"')
-                       return;
-
-               if (!re_genre.match(line, 0, out matchinfo))
-                       return;
-
-               string title;
-               string genre = matchinfo.fetch (2);
-               try {
-                       title = convert(matchinfo.fetch (1), -1, "utf-8", "latin1");
-               } catch (ConvertError e) {
-                       return;
-               }
-
-               sqlite.movie_add_genre (title, genre);
-       }
-}
-
-class RatingLineParser : LineParser {
-       Regex re_rating;
-
-       public RatingLineParser (IMDbSqlite _sqlite) {
-               base (_sqlite);
-               try {
-                       re_rating = new Regex ("^      .+ +([0-9]+) +([0-9.]+) +(.+)$");
-               } catch (RegexError e) {
-                       critical ("Failed to initialize regex: %s\n", e.message);
-               }
-       }
-
-       public override void parse_line (string line) {
-               MatchInfo matchinfo;
-
-               // Skip series episodes
-               if (line[0] == '"')
-                       return;
-
-               if (!re_rating.match(line, 0, out matchinfo))
-                       return;
-
-               string title;
-               string votes = matchinfo.fetch (1);
-               string rating = matchinfo.fetch (2);
-               try {
-                       title = convert(matchinfo.fetch (3), -1, "utf-8", "latin1");
-               } catch (ConvertError e) {
-                       return;
-               }
-
-               // Skip series episodes
-               if (title[0] == '"')
-                       return;
-
-               if (skip_title (title))
-                       return;
-
-               sqlite.movie_set_rating (title, (int) (rating.to_double () * 10), votes.to_int ());
-       }
-}