Exemplo n.º 1
0
        //                        0                    1                    2                     3                      4                       5                        6                              7                         8
        public void DoUpdate(string sConnectionString)
        {
            // get the set of books that we want to update
            TCore.Sql sql;

            SqlSelect sqls        = new SqlSelect(s_sBaseQuery, s_mpAliases);
            SqlWhere  swInnerJoin = new SqlWhere();

            swInnerJoin.Add("$$upc_dvd$$.ScanCode = $$upc_codes$$.ScanCode", SqlWhere.Op.And);
            sqls.AddInnerJoin(new SqlInnerJoin("$$#upc_codes$$", swInnerJoin));

            UpdateStatus flagsToUpdate = UpdateStatus.MediaType | UpdateStatus.Title | UpdateStatus.Categories | UpdateStatus.CoverSrc | UpdateStatus.Summary;

            if (m_config.ForceUpdateSummary)
            {
                flagsToUpdate |= UpdateStatus.Summary;
            }

            // we want to match any entry that hasn't tried to update ALL of our fields. (if they have tried some,
            // but not others, then try again. after all, we might know more fields now that we want to update.
            sqls.Where.Add($"$$upc_dvd$$.UpdateStatus & {(int)flagsToUpdate} <> {(int)flagsToUpdate}", SqlWhere.Op.And);
            sqls.Where.StartGroup(SqlWhere.Op.And);
            sqls.Where.Add(String.Format("$$upc_dvd$$.LastUpdate < '{0}'", DateTime.Now.ToString("d")), SqlWhere.Op.And);
            sqls.Where.Add("$$upc_dvd$$.LastUpdate IS NULL", SqlWhere.Op.Or);
            sqls.Where.EndGroup();

            // and lets update the latest scanned items first
            sqls.AddOrderBy("$$upc_codes$$.LastScanDate DESC");

            SR sr = Sql.OpenConnection(out sql, sConnectionString);

            if (!sr.Succeeded)
            {
                throw new Exception(sr.Reason);
            }

            // from this point on, we have to release the sql connection!!
            int cFailed = 0;

            try
            {
                sr = Sql.ExecuteQuery(sql, sqls.ToString(), this, null);

                if (!sr.Succeeded)
                {
                    throw new Exception(sr.Reason);
                }

                foreach (DvdElementEx dvdex in m_plDvd)
                {
                    string sError;

                    Console.WriteLine($"Trying to scrape dvd {dvdex.ScanCode}: {dvdex.Title}...");
                    DVD.ScrapeSet scrapedSet = 0;

                    if (m_config.ForceUpdateSummary)
                    {
                        dvdex.Summary = null;
                    }

                    if (DVD.FScrapeDvd(dvdex, out scrapedSet, out sError))
                    {
                        // we might not have scraped everything, but we scraped something...

                        if (scrapedSet.HasFlag(DVD.ScrapeSet.CoverSrc) && !DownloadCoverForDvd(dvdex, m_config.LocalCoverRoot, "covers", out sError))
                        {
                            Console.WriteLine($"FAILED: to download cover art: {sError}");
                            LogDvdUpdateError(dvdex, scrapedSet, sError);
                        }
                        else
                        {
                            Console.WriteLine(
                                $"SUCCEEDED: MediaType: {dvdex.MediaType}, Classification: {dvdex.Classification}");
                            AddDVDToUpdateQueue(dvdex, scrapedSet);
                        }
                    }
                    else
                    {
                        Console.WriteLine($"FAILED: {sError}");
                        LogDvdUpdateError(dvdex, scrapedSet, sError);
                        cFailed++;
                        if (cFailed > 150)
                        {
                            throw new Exception("giving up");
                        }
                    }
                }
            }
            finally
            {
                sql.Close();
            }
        }