private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) { var sName = ""; var worker = sender as BackgroundWorker; //Step1 Create temp directory if doesnt exists if(!Directory.Exists("temp")) { Directory.CreateDirectory("temp"); } if (!Directory.Exists("res")) { Directory.CreateDirectory("res"); } //Step2 Download series info zip file worker.ReportProgress(15); var client = new WebClient(); client.DownloadFile("http://thetvdb.com/api/" + Constants.ApiKey + "/series/" + _selectedItem.GetSeriesId() + "/all/" + _selectedItem.GetLang() + ".zip", @"temp/tmp.zip"); //Step3 Extract xmls worker.ReportProgress(30); using (var zip = ZipFile.Read("temp/tmp.zip")) { zip.ExtractAll("temp/"); } //Step4 Add series information to database worker.ReportProgress(45); var doc = XDocument.Load("temp/en.xml"); var names = from ele in doc.Descendants("Series") select new { seriesName = (string)ele.Element("SeriesName"), overview = (string)ele.Element("Overview"), lang = (string)ele.Element("Language"), id = (int)ele.Element("id"), firstAired = (string)ele.Element("FirstAired"), imdbId = (string)ele.Element("IMDB_ID"), network = (string)ele.Element("Network"), rating = (double)ele.Element("Rating"), runtime = (int)ele.Element("Runtime"), status = (string)ele.Element("Status"), banner = (string)ele.Element("banner"), fanart = (string)ele.Element("fanart"), poster = (string)ele.Element("poster") }; foreach (var n in names) { client.DownloadFile("http://thetvdb.com/banners/"+n.banner, @"res/"+n.seriesName+"_banner.jpg"); client.DownloadFile("http://thetvdb.com/banners/" + n.fanart, @"res/" + n.seriesName + "_fanart.jpg"); client.DownloadFile("http://thetvdb.com/banners/" + n.poster, @"res/" + n.seriesName + "_poster.jpg"); var sdb = new SeriesDatabaseEntry(n.seriesName, n.firstAired, n.imdbId, n.overview, n.rating, n.id, n.lang, "res/" + n.seriesName + "_banner.jpg", "http://thetvdb.com/banners/" + n.banner, "res/" + n.seriesName + "_poster.jpg", "http://thetvdb.com/banners/" + n.poster, "res/" + n.seriesName + "_fanart.jpg", "http://thetvdb.com/banners/" + n.fanart, n.network, n.runtime, n.status, false, false, DateTime.Now.ToString()); sName = n.id.ToString(); CreateArtsTable(sName); var adb = new ArtsDatabaseEntry( "res/" + n.seriesName + "_banner.jpg"); var adb2 = new ArtsDatabaseEntry("res/" + n.seriesName + "_poster.jpg"); var adb3 = new ArtsDatabaseEntry( "res/" + n.seriesName + "_fanart.jpg"); try { _db.InsertSeries(sdb); } catch (Exception crap) { MessageBox.Show(crap.Message); } try { _db.InsertArts(adb, "arts_"+sName); _db.InsertArts(adb2, "arts_" + sName); _db.InsertArts(adb3, "arts_" + sName); } catch (Exception crap) { MessageBox.Show(crap.Message); } } //Step5 Create table for episodes worker.ReportProgress(60); CreateEpisodesTable(sName); //Step6 Add episodes info to database worker.ReportProgress(75); // var names = doc.Descendants("Series"); var names1 = from ele in doc.Descendants("Episode") select new { episodeName = (string)ele.Element("EpisodeName") ?? string.Empty, episode = (int)ele.Element("EpisodeNumber") , season = (int)ele.Element("SeasonNumber"), firstAired = (string)ele.Element("FirstAired") ?? string.Empty, imdbId = (string)ele.Element("IMDB_ID") ?? string.Empty, overview = (string)ele.Element("Overview") ?? string.Empty, rating = getRating(ele), episodeId = (int)ele.Element("id") }; foreach (var edb in names1.Select(n => new EpisodeDatabaseEntry(n.episodeName, n.episode, n.season, n.rating, n.firstAired, n.imdbId, n.overview, false, n.episodeId))) { try { _db.InsertEpisode("episodes_"+sName, edb); } catch (Exception crap) { MessageBox.Show(crap.Message); } } //Step7 dELETE temp folder worker.ReportProgress(90); Directory.Delete(@"temp", true); worker.ReportProgress(100); }
/**/ public int InsertSeries(SeriesDatabaseEntry series) { var cnn = new SQLiteConnection(_dbConnection); cnn.Open(); var mycommand = new SQLiteCommand(cnn) { CommandText = "INSERT INTO series (series_name, first_aired, imdb_id, overview, rating, series_id, language, banner_local, banner_url, poster_local, poster_url, fanart_local, fanart_url, network, runtime, status, ignore_agenda, hide_from_list, updated) VALUES(@series_name, @first_aired, @imdb_id, @overview, @rating, @series_id, @language, @banner_local, @banner_url, @poster_local, @poster_url, @fanart_local, @fanart_url, @network, @runtime, @status, @ignore_agenda, @hide, @updated);", CommandType = CommandType.Text }; mycommand.Parameters.Add(new SQLiteParameter("@series_name", series.SeriesName)); mycommand.Parameters.Add(new SQLiteParameter("@first_aired", series.FirstAired)); mycommand.Parameters.Add(new SQLiteParameter("@imdb_id", series.ImdbId)); mycommand.Parameters.Add(new SQLiteParameter("@overview", series.Overview)); mycommand.Parameters.Add(new SQLiteParameter("@rating", series.Rating)); mycommand.Parameters.Add(new SQLiteParameter("@series_id", series.SeriesId)); mycommand.Parameters.Add(new SQLiteParameter("@language", series.Language)); mycommand.Parameters.Add(new SQLiteParameter("@banner_local", series.BannerLocal)); mycommand.Parameters.Add(new SQLiteParameter("@banner_url", series.BannerUrl)); mycommand.Parameters.Add(new SQLiteParameter("@poster_local", series.PosterLocal)); mycommand.Parameters.Add(new SQLiteParameter("@poster_url", series.PosterUrl)); mycommand.Parameters.Add(new SQLiteParameter("@fanart_local", series.FanartLocal)); mycommand.Parameters.Add(new SQLiteParameter("@fanart_url", series.FanartUrl)); mycommand.Parameters.Add(new SQLiteParameter("@status", series.Status)); mycommand.Parameters.Add(new SQLiteParameter("@network", series.Network)); mycommand.Parameters.Add(new SQLiteParameter("@runtime", series.Runtime)); mycommand.Parameters.Add(new SQLiteParameter("@ignore_agenda", series.Ignore)); mycommand.Parameters.Add(new SQLiteParameter("@hide", series.Hide)); mycommand.Parameters.Add(new SQLiteParameter("@updated", series.Updated)); var rowsUpdated = mycommand.ExecuteNonQuery(); cnn.Close(); return rowsUpdated; }