Exemplo n.º 1
0
        public void CountImageFiles(SetItem setItem)
        {
            setItem.ImageCount = 0;
            setItem.CardCount  = 0;

            foreach (var card in setItem.set.Cards)
            {
                foreach (var alt in card.Properties)
                {
                    if (!alt.Key.Contains("split"))
                    {
                        setItem.CardCount += 1;
                        if (FindLocalCardImages(setItem.set, card, alt.Key).Count() > 0)
                        {
                            setItem.ImageCount += 1;
                        }
                    }
                }
            }
        }
Exemplo n.º 2
0
        public MainWindow()
        {
            this.InitializeComponent();
            this.DataContext = this;
            if (game == null)
            {
                game = DbContext.Get().GameById(Guid.Parse("A6C8D2E8-7CD8-11DD-8F94-E62B56D89593")) ?? throw new Exception("MTG is not installed!");
            }

            JArray scryfallSetData;
            JArray OctgnSetData;

            using (var webclient = new WebClient()
            {
                Encoding = Encoding.UTF8
            })
            {
                OctgnSetData    = (JArray)JsonConvert.DeserializeObject(webclient.DownloadString("http://www.octgngames.com/forum/json.php"));
                scryfallSetData = (JsonConvert.DeserializeObject(webclient.DownloadString("https://api.scryfall.com/sets")) as JObject)["data"] as JArray;
            }

            sets = new List <SetInfo>();

            foreach (var jsonset in scryfallSetData)
            {
                var setInfo = new SetInfo();

                setInfo.Code       = jsonset.Value <string>("code");
                setInfo.ParentCode = jsonset.Value <string>("parent_set_code");
                setInfo.BlockCode  = jsonset.Value <string>("block_code");

                setInfo.Type      = jsonset.Value <string>("set_type");
                setInfo.SearchUri = jsonset.Value <string>("search_uri");

                sets.Add(setInfo);
            }

            setList             = new List <SetItem>();
            SetList.ItemsSource = setList;

            foreach (var set in game.Sets())
            {
                if (!set.Hidden && set.Cards.Count() > 0)
                {
                    if (set.Id.ToString() == "a584b75b-266f-4378-bed5-9ffa96cd3961")
                    {
                        var setItem = new SetItem();
                        setItem.set = set;

                        setItem.releaseDate = new DateTime(3000, 1, 1);
                        setItem.extraSets   = new List <SetInfo>();
                        CountImageFiles(setItem);
                        setList.Add(setItem);
                    }
                    else
                    {
                        var octgndbset = OctgnSetData.FirstOrDefault(x => x.Value <string>("guid") == set.Id.ToString());
                        if (octgndbset == null)
                        {
                            continue;
                        }

                        var setItem = new SetItem();
                        setItem.set = set;

                        var setCode = octgndbset.Value <string>("octgn_code").ToLower();

                        setItem.setData = sets.First(x => x.Code == setCode);

                        setItem.extraSets = new List <SetInfo>();

                        setItem.extraSets.AddRange(sets.Where(x => x.ParentCode == setItem.setData.Code));
                        if (setItem.setData.BlockCode != setItem.setData.Code)
                        {
                            setItem.extraSets.AddRange(sets.Where(x => x.ParentCode == setItem.setData.BlockCode));
                        }

                        setItem.releaseDate = Convert.ToDateTime(octgndbset.Value <string>("date"));
                        CountImageFiles(setItem);
                        setList.Add(setItem);
                    }
                }
            }

            this.Closing += CancelWorkers;
        }
Exemplo n.º 3
0
        private void DownloadSet(SetItem setItem, IProgress <WorkerItem> progress)
        {
            var i       = 0;
            var set     = setItem.set; //TODO: remove this and use setItem
            var setSize = set.Cards.Count();

            foreach (var c in set.Cards)
            {
                i++;
                foreach (var alt in c.Properties)
                {
                    if (_cts.IsCancellationRequested)
                    {
                        break;
                    }

                    var cardInfo = GetCardInfo(setItem, c, alt.Key);

                    var workerItem = new WorkerItem();
                    workerItem.set      = setItem;
                    workerItem.alt      = alt.Key;
                    workerItem.progress = (double)i / setSize;
                    workerItem.card     = c;

                    // get local image info

                    var files = FindLocalCardImages(set, c, workerItem.alt);

                    if (cardInfo == null)
                    {
                        if (set.Id.ToString() != "a584b75b-266f-4378-bed5-9ffa96cd3961")
                        {
                            MessageBox.Show(String.Format("Cannot find scryfall data for card {0}.", c.Name));
                        }
                        progress.Report(workerItem);
                        continue;
                    }
                    workerItem.local = files.Length > 0 ? UriToStream(files.First()) : null;

                    var imageDownloadUrl = "";
                    var flipCard         = false;

                    switch (cardInfo.Layout)
                    {
                    case "transform":
                    {
                        if (workerItem.alt == "transform")
                        {
                            imageDownloadUrl = xl ? cardInfo.LargeBackUrl : cardInfo.NormalBackUrl;
                        }
                        else
                        {
                            imageDownloadUrl = xl ? cardInfo.LargeUrl : cardInfo.NormalUrl;
                        }
                        break;
                    }

                    case "split":
                    {
                        if (workerItem.alt == "")
                        {
                            imageDownloadUrl = xl ? cardInfo.LargeUrl : cardInfo.NormalUrl;
                        }
                        break;
                    }

                    case "flip":
                    {
                        if (workerItem.alt == "flip")
                        {
                            flipCard = true;
                        }
                        imageDownloadUrl = xl ? cardInfo.LargeUrl : cardInfo.NormalUrl;
                        break;
                    }

                    default:
                    {
                        imageDownloadUrl = xl ? cardInfo.LargeUrl : cardInfo.NormalUrl;
                        break;
                    }
                    }

                    // if the card has no web image
                    if (string.IsNullOrEmpty(imageDownloadUrl))
                    {
                        progress.Report(workerItem);
                        continue;
                    }

                    // check if the web image has a newer timestamp
                    var webTimestamp = Convert.ToInt32(imageDownloadUrl.Split('?')[1]);

                    if (workerItem.local != null)
                    {
                        int localTimestamp;
                        using (var image = Image.FromStream(workerItem.local))
                        {
                            if (image.PropertyIdList.FirstOrDefault(x => x == 40092) == 0)
                            {
                                localTimestamp = 0;
                            }
                            else
                            {
                                localTimestamp = Convert.ToInt32(Encoding.Unicode.GetString(image.GetPropertyItem(40092).Value));
                            }
                            if (localTimestamp > 0 && ((image.Width > 600 && xl) || (image.Width < 500 && !xl)))
                            {
                                if (webTimestamp <= localTimestamp)
                                {
                                    progress.Report(workerItem);
                                    continue;
                                }
                            }
                        }
                    }

                    // download image

                    var garbage = Config.Instance.Paths.GraveyardPath;
                    if (!Directory.Exists(garbage))
                    {
                        Directory.CreateDirectory(garbage);
                    }

                    foreach (var f in files.Select(x => new FileInfo(x)))
                    {
                        f.MoveTo(Path.Combine(garbage, f.Name));
                    }

                    var imageUri = String.IsNullOrWhiteSpace(workerItem.alt) ? c.ImageUri : c.ImageUri + "." + workerItem.alt;

                    var newPath = Path.Combine(set.ImagePackUri, imageUri + ".jpg");


                    workerItem.web = UriToStream(imageDownloadUrl);

                    using (var newimg = Image.FromStream(workerItem.web))
                    {
                        if (flipCard)
                        {
                            newimg.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);
                        }
                        else if (cardInfo.Layout == "Planar")
                        {
                            newimg.RotateFlip(System.Drawing.RotateFlipType.Rotate90FlipNone);
                        }

                        var commentMetadata = (PropertyItem)FormatterServices.GetUninitializedObject(typeof(PropertyItem));

                        commentMetadata.Id    = 40092; // this is the comments field
                        commentMetadata.Value = Encoding.Unicode.GetBytes(webTimestamp.ToString());
                        commentMetadata.Len   = commentMetadata.Value.Length;
                        commentMetadata.Type  = 1;

                        newimg.SetPropertyItem(commentMetadata);
                        newimg.Save(newPath, ImageFormat.Jpeg);
                    }

                    progress.Report(workerItem);
                }
            }
        }