Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            username = args[0];
            password = args[1];

            Bot.unsafeHttpHeaderParsingUsed = 0;
            Site site = new Site(wikiSiteUrl, username, password);
            while (true)
            {
                int totalPagesProcessed = 0;
                try
                {
                    PageList pageList = new PageList(site);
            #if true
                    if (catScanning != null)
                        pageList.FillFromCategory(catScanning);
                    else
                        pageList.FillFromSearchResults(searchScanning, int.MaxValue);
            #endif

                    string failureReason = null;
                    foreach (Page page in pageList)
                    {
                        if (resumeAfter != null)
                        {
                            if (page.title == resumeAfter) resumeAfter = null;
                            continue;
                        }

                        totalPagesProcessed++;
                        if (!page.title.StartsWith("File:"))
                        {
                            continue;
                        }
                        while (true)
                        {
                            try
                            {
                                page.Load();

                                if (tagCompletedRegex.Match(page.text).Success)
                                {
                                    break;
                                }

                                if (!tagRegex.Match(page.text).Success &&
                                    !page.text.ToLower().Contains("{{picasareviewunnecessary}}") &&
                                    !page.text.ToLower().Contains("user:picasa review bot/reviewed-error"))
                                {
                                    Regex licenseReviewRegex = new Regex("{{LicenseReview\\|[^|]*\\|([^|]*)\\|([^}]*)}}");
                                    Match m;
                                    if ((m = licenseReviewRegex.Match(page.text)).Success)
                                    {
                                        page.text = licenseReviewRegex.Replace(page.text, "{{picasareview|" + m.Groups[1].ToString() + "|" + m.Groups[2].ToString() + "}}");
                                        SavePage(page, "Converting old LicenseReview tag into picasareview tag");
                                        break;
                                    }
                                    else
                                    {
                                        page.text += "\n{{picasareview}}\n";
                                    }
                                }

                                bool success = false;
                                do
                                {
                                    File.Delete("temp_wiki_image");
                                    File.Delete("temp_picasa_image");
                                    File.Delete("temp_picasa_image_full");

                                    string licenseName, mediaUrl;

                                    bool reviewUnnecessary = false;
                                    if (CheckIfReviewUnnecessary(page))
                                    {
                                        // Keep running so we can upload the original version, break out later
                                        // (unless it has an OTRS tag, in which case we shouldn't raise the resolution,
                                        // or is Flickr reviewed, in which case only a lower-resolution version may
                                        // be released on Flickr)
                                        reviewUnnecessary = true;
                                        success = true;
                                        if (HasOtrsTag(page) || IsFlickrReviewed(page)) continue;
                                    }

                                    if (!page.title.ToLower().EndsWith(".jpg") && !page.title.ToLower().EndsWith(".png") && !page.title.ToLower().EndsWith(".tiff"))
                                    {
                                        failureReason = "can only review image files, not other file types";
                                        continue;
                                    }

                                    if (!FetchPicasaImageInfo(page, out licenseName, out mediaUrl))
                                    {
                                        failureReason = "could not retrieve image information from Picasa";
                                        continue;
                                    }

                                    if (!reviewUnnecessary && !CheckLicense(page, licenseName))
                                    {
                                        failureReason = "image license on Picasa is invalid";
                                        continue;
                                    }
                                    string licenseChangedFrom = null, licenseChangedTo = null;
                                    if (!reviewUnnecessary && !UpdateLicense(page, licenseName, out licenseChangedFrom, out licenseChangedTo))
                                    {
                                        failureReason = "could not recognize license on Commons page";
                                        continue;
                                    }

                                    string mediaUrlFull = new Regex("^(.*)/([^/]*)$").Replace(mediaUrl, "${1}/d/${2}");
                                    if (!WgetToFile(mediaUrlFull, "temp_picasa_image_full"))
                                    {
                                        failureReason = "license matches and is valid - but could not retrieve full size image from Picasa";
                                        continue;
                                    }

                                    page.DownloadImage("temp_wiki_image");
                                    if (!FilesAreIdentical("temp_picasa_image_full", "temp_wiki_image"))
                                    {
                                        // Upload original full res version
                                        if (!UploadOriginalVersion(out failureReason, page, mediaUrl, "temp_wiki_image", "temp_picasa_image_full", /*fetchThumbnailVersion*/true, /*allowWikiBigger*/false))
                                        {
                                            continue;
                                        }
                                    }

                                    if (!reviewUnnecessary)
                                    {
                                        // It matches! Good to go.
                                        UpdateReviewTagSuccess(page, licenseChangedFrom, licenseChangedTo);
                                        success = true;
                                    }
                                } while (false);

                                if (!success)
                                {
                                    UpdateReviewTagFailure(page, failureReason);
                                }
                            }
                            catch (Exception e)
                            {
                                Console.WriteLine("Encountered exception: " + e.Message);
                                Console.WriteLine("Retrying...");
                                continue;
                            }

                            break;
                        }
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine("Encountered exception: " + e.Message);
                }
                Console.WriteLine("Total pages processed: " + totalPagesProcessed);
                Console.WriteLine("Sleeping for 2 minutes...");
                Thread.Sleep(new TimeSpan(0, 2, 0));
            }
        }