magefree / mage

Magic Another Game Engine
http://xmage.today
MIT License
1.85k stars 762 forks source link

Images: tokens source is broken (tokens.mtg.onl, mythicspoiler.com and others) #5767

Closed JayDi85 closed 3 months ago

JayDi85 commented 5 years ago
Malktuth commented 5 years ago

I wrote a code in R (sorry, no python) to download the token pics. In case anyone is interested... The whole code might be a mess (I ain't no programmer)

#Load packages
if (!require(pacman)){install.packages('pacman')}
p_load(RCurl,rvest,XML,httr,dplyr,progress)

#Creates the function to download the pics
getpic = function(ed,root.path = 'E:/Stuff/MTG/TOK'){ #Change root.path to whatever
  if (!dir.exists(paste(root.path,ed,sep = '/'))) {dir.create(paste(root.path,ed,sep = '/'),recursive = T)}
  setwd(file.path(root.path,ed))
  url <- paste0("https://www.mtg.onl/tokens/",toupper(ed))

  doc = GET(url)
  doc = htmlParse(doc,encoding = 'UTF-8')
  cardnames = xpathSApply(doc, '//*[@class="tokensGroup-module--link--2XG4o"]/@href') %>% 
    gsub('^\\/token-list\\/(.*)\\/$','\\1',.) %>% 
    gsub('\\_[UWBRG]*\\_*\\d\\_\\d$','',.) %>% 
    gsub('\\_',' ',.)
  if (any(duplicated(cardnames))) 
  {
    dupes = cardnames[duplicated(cardnames)]
    for (dupe in dupes)
    {
      cardnames[cardnames %in% dupe] = paste(dupe,seq(1,sum(cardnames %in% dupe)))
    }
  }

  links = xpathSApply(doc, "//img/@srcset") %>% 
    grep(paste0(ed,'.*\\.jpg'),.,ignore.case = T,value = T) %>% 
    sapply(., function(x) {strsplit(x,',') %>% 
        unlist() %>% tail(1) %>% 
        gsub('\\n','',.) %>% 
        gsub(' \\d{3}w$','',.)})

  pb = winProgressBar('Downloading',,0,100,0)
  for (i in 1:length(links))
  {
    download.file(links[i], destfile = basename(sprintf('%s.full.jpg',cardnames[i])),mode = 'wb',quiet = T)
    info <- sprintf("%s%%", round(100*i/length(links),2))
    setWinProgressBar(pb, round(100*i/length(links),2), sprintf("Downloading (%s)", info), info)
  }
  close(pb)
}

# Get the code for each expansion
setnamesAll = NULL
for (i in c("",2:11)){
  url <- sprintf("https://www.mtg.onl/tokens/%s",i)
  doc = GET(url)
  doc = htmlParse(doc,encoding = 'UTF-8')
  setnames = xpathSApply(doc, '//*[@class="tokensGroup-module--header--JeYYP"]/h3/i/@class') %>% 
    gsub('^ss\\-(\\w+)\\sss.*','\\1',.) %>% toupper()
  setnamesAll = c(setnamesAll,setnames)  
}
setnamesAll = gsub('\\_','',setnamesAll)
dwlded = c(dir('.'),'CON') # Removes already downloaded directories a also for some reason you can create a 'CON' folder, so CONflux is out

pb <- progress_bar$new(format = "  downloading :what [:bar] :percent eta: :eta", total = length(setdiff(setnamesAll,dwlded)))
for (ed in grep(sprintf('(%s)',paste0(dwlded,collapse = '|')),setnamesAll,invert = T,value = T))
{
  getpic(ed)
  pb$tick(tokens = list(what = ed))
}

#Go up and zip all
setwd('..')
zip('TOK.zip',dir('.'))
timhae commented 4 years ago

I would like to implement that feature but I am a little confused regarding the current implementation. Are there two files implementing this?

I would rewrite those into one file and make it similar to the scryfall one.

Also, should I update this file https://github.com/magefree/mage/blob/master/Mage.Client/src/main/resources/tokens-mtg-onl-list.csv programmatically? Last update was 4 years ago..

JayDi85 commented 4 years ago
  1. That's all tokens download are broken now:
    • MtgOnlTokensImageSource -- never worked, can be deleted;
    • TokensMtgImageSource and AltMtgOnlTokensImageSource are broken after site updated. I think it can be deleted at all too (after scryfall or another token source completed).
  2. tokens-mtg-onl-list.csv that's internal file downloaded from onl-source, so no needs in it anymore and it can be deleted too.
  3. Yes, it will be good to implement tokens download from scryfall or another source. You can add tokens download as new images source from scratch or to existed file. If you would add scryfall support and change existed source then try to make changes like that PR (#6190 -- search files with scryfall: 1, 2 -- that PR will be merged soon);

BUT what's your idea to implement it? It's not so easy. XMage uses custom tokens list and you can't download it by names -- tokens with same name can be different (e.g. different PT, abilities, etc). So you must manually link xmage's tokens with images source.

How's xmage tokens works:

How's to add new tokens and downloads (as example, for new set):

timhae commented 4 years ago

Thanks a lot, that's very useful input! I will try and brew smething up in a pr or get back to you if I ever get stuck. I guess I will either wait until #6190 is merged or just create a branch with that fork as upstream..

Jieiku commented 1 year ago

How can I test which Tokens I am missing? (is there a way to generate a list of missing token images?)

In the image downloader I can see I am missing 32 card images and 238 token images....

I got most of the missing card images here using the clipboard option: https://github.com/magefree/mage/issues/9249#issuecomment-1457490644

but for tokens option 8 Copy and Paste Image URLs does not seem to apply to tokens...

2023-03-06_20-34-13

I figured this out mostly: https://github.com/magefree/mage/issues/6955#issuecomment-1458924711

JayDi85 commented 1 year ago

Web site reworked and tokens download doesn't work anymore. Also it's outdated -- miss tokens from last years releases.

But it can be used to download a proxy tokens in the future (tokens that was miss by wizards), see https://www.mtg.onl/mtg-missing-tokens/