Log in

LHpi

LHpi is LordHelmchen's price import library and sitescripts for Magic Album.

LHpi is a collection of lua scripts to be used by Magic Album, but is not part of Magic Album and thus updated and supported seperately.

Discuss, ask for help and leave feedback at the corresponding Forum Thread.

Contents

Sitescripts

The sitescripts are the actual price import scripts, written in lua. They use the Magic Album Lua API and the LHpi library to query and parse a website and import the price information into Magic Album. Most of the program logic is contained in the library, so the sitescript needs to hold only site-specific information and a few functions that are not easily generalizable.

In theory, you should not be dependent on a new version once a new set is available: just add new entries to site.sets table in the sitescript. Being easy to update yourself was one of the design goals.

Sitescripts will need to provide

  • a table of supported sets with language and url infix fields
  • a table of supported languages with url infix field
  • a function to build urls
  • a function to parse the html raw data into data fields via regular expressions (and the regex itself)
  • optionally, they can also provide
    • functions for site specific data manipulation, in case the site has some strange formatting the library does not understand
    • a one-to-one name map for names that differ between the site and Magic Album's database
    • tables to map card names to picture versions, in case the one-to-one map and LHpi's defaults are not sufficient
    • a table with expected card count numbers for LHpi's sanity checks.

magicuniverse.de

LHpi.magicuniverseDE.lua.zip
Sitescript version 2.9.2.10
used LHpi version 2.9
used LHpi.Data version 2
updated last on 2014-03-06
size 12.62 KiB

Magic Universe is a German site with German and English cards, priced in €.

This site started it all, since I wanted prices in local currency, and my FLG apparently uses them as price reference.

mtgmintcard.com

LHpi.mtgmintcard.lua.zip
Sitescript version 2.10.2.10
used LHpi version 2.10
used LHpi.Data version 2
updated last on 2014-03-07
size 10.79 KiB

Functionally the same as "MTG Mint Card.lua" by Goblin Hero and Stromglad1.

When I wanted to generalize my script for easy addaption to other sites, this provided me with a good baseline, since I could search MA for cards that my script missed but should have set. Also, the site is easy to querry and parse. They do have a currency chooser, so I started by trying to make a mtgmintcard-Euro script. I failed, but salvaged the wreck for default $ prices.

magic.tcgplayer.com PriceGuide

LHpi.tcgplayerPriceGuide.lua.zip
Sitescript version 2.9.2.9
used LHpi version 2.9
used LHpi.Data version 2
updated last on 2014-03-06
size 14.17 KiB

Tcgplayer Price Guide claims to accumulate prices from 75 online vendors. They are priced in $. Their tables give a high, low and average price; the sitescript contains a variable to select which one of those it should use.
To change the column to import from, open the script in a text editor, find himelo = 3 and change it to himelo = 1 for high or himelo = 2 for medium prices. You can have multiple copies of the same script
As requested by Golob here.
Also, most Promo sets have not been tested yet and are commented out.
Note that cards with multiple versions are averaged in some sets and set to "variant 1"'s price in others, as the source list does not clearly identify the version. Even when it does, it is unclear if the entry without version suffix is meant to be all unspecified or version 1 only. In most cases, "CARD" price is set for all versions and averaged with "CARD (version)" price for given versions now.

trader-online.de

LHpi.trader-onlineDE-v2.8.1.1.lua.zip
Sitescript version 2.9.2.9
used LHpi version 2.9
used LHpi.Data version 2
updated last on 2014-03-06
size 13.08 KiB

Trader Online is a German site with German and English cards, priced in €.

I found this site while looking for Token images, found it to be parseable by sitescript, and decided to use it as a test on how well the library was adapted to support additional sites.

The LHpi Library

LHpi-v2.9.lua.zip
Tested with Ma version 1.6.1.325
updated last on 2014-03-06
size 16.64 KiB
LHpi-v2.10.lua.zip
Tested with Ma version 1.6.1.325
updated last on 2014-03-07
size 16.69 KiB

It is not expected to be fast, but as reliable as possible:
It first compares the user requested sets and languages with what the sitescript has available and builds a table of urls to querry, seperated by set.
Then it loops though the sets that are both available and selected:
The raw html data is fetched, parsed and entered into an intermediate table of all prices for the current edition, merging regular and foil prices to minimize calls to ma.SetPrice.
Should the same card (defined by set,card,language,(picture-)version ) be encountered again, it compares the prices. If they are equal, the new price is discarded; if they differ (Yes, magicuniverse.de's database is quirky), it uses the average price. Cards that are suffxed with a condition description are currently disregarded.
Once that cardsetTable is completed (all urls belonging to the set are parsed), it sends the prices to MA. Finally, it compares the count of set prices with the number of cards in the set (or, if given, the expectation given in the sitescript). See How-To_sitescriptTemplate#procedure_of_function_calls for details. Logging to a seperate file is now disabled by default again (but still configurable by the variable SAVELOG in the script). The log (\Prices\LHpi.log) is overwritten on subsequent re-runs of a script.

LHpi.Data

LHpi.Data-v2.lua.zip
Library version >=2.7
updated last on 2014-03-06
size 21.55 KiB

Since library version 2.7, site-independent set data (number of cards,foilage, default variant and foiltweak tables) were moved into a seperate data file that gets loaded by the library on initialization. This will allow to distinguish between code updates (to the library) and set data updates (which are usually needed when a new set is released). Version numbering of the sitescripts will reflect both library and data versions used.

Installation and usage

  1. Download at least one sitescript and both the LHpi libray and data file in the version the sitescript uses.
    • note the change to version numbers and file names in 2.9.2 , see #version_history for details.
  2. Unzip into the "Prices" subdirectory of your Magic Album installation.
  3. (recommended, but not yet strictly necessary) Create a subfolder called "lib" under "Prices" and move both library and data file into the subfolder.
  4. (optional) If you want to use the SAVEHTML or SAVETABLE feature, you need to create an empty subdirectory, named like the sitescript without file extension (for example "Prices\LHpi.magicuniverseDE").
  5. (re)start Magic Album and use the sitescript (not the library nor the data file. Step 3 makes this warning obsolete.) from Magic Album's Price Manager dialog.

If something does not happen as expected

  • make sure the script is saved utf-8 encoded. It's customary for lua scripts to be saved ANSI encoded, but as MA expects utf input, this is what I considered the most wysiwyg approach to set cards with special characters in their names.
  • open the lua file in a text editor and change "--VERBOSE = true" to "VERBOSE = true", likewise enable "LOGDROPS","LOGNAMEREPLACE","LOGFOILTWEAK","STRICTCHECKEXPECTED" and "SAVELOG". This will make the script provide more feedback to help track down any problems (or just to see what it was doing) and also enable additional checks. The logging will then be sent to "[MA Folder]\Prices\[scriptname].log".

I'll try to answer any questions you might have. If the script misbehaves please follow the two steps above and attach a copy of the .log to your post.

Development

How-to write your sitescript file

Detailed instructions on the properties and functions to define in your sitescript file can be found at How-To sitescriptTemplate.

sitescriptTemplate

LHpi.sitescriptTemplate-v2.10.2.9.lua.zip
Sitescript version 9
uses LHpi library version 2.10
uses LHpi.Data version 2
updated last on 2014-03-07
size 9.14 KiB

This is just an empty, commented template to help write new sitescripts.

dummyMA.lua

dummyMA.zip
version 0.4
updated last on 2013-03-07
size 7.6 KiB

Magic Album API dummy to test and debug LHpi within an IDE (such as eclipse) and without needing Magic Album. Now fixed for backwards compatibility with Lua 5.1 and paths outside of MA's "Prices" directory.

Planned features

in no particular order

  • Promo and special sets for magicuniverseDE,mtgmintcards, tcgplayerPriceguide
    • add all special and promo sets (cardcount,variants,foiltweak) to LHpi.Data
  • tweak log : better differentiation of log levels (normal,verbose,debug); rewrite normal-level logentries to be more pleasing to read.
    I'd love to get users' feedback on this: how silent should normal level be; what should not be logged unless in debug mode?
  • (((a+b)/2)+c)/2 != (a+b+c)/3 = (((a+b)/2*2)+c)/3
    For mathematically correct averaging, need to attach a counter to averaged prices, then on next averaging, do if counter then newaverage=(oldaverage*(counter+1) + newprice) / (counter+2)

Possible features

in no particular order

  • passing importsets to BuildCardData could sort out the mess that is magicuniverse's promo page, at the cost of lots looping through sets. Should thus be done in site.BCDPlugins
  • ideally, most information in LHpi.Data.sets could be read from MA via API getter functions.
  • add hardcoded exchange rate for crude currency conversion between parsing and importing.
  • handle conflicting prices differently. Is averaging ok, or should the script discard one (which?) of the prices and/or throw an error ? Until further notice, I'm ok with how averaging works.averaging conflicts. How it's done needs some work.
  • import prices from condition-suffixed cards instead of dropping them.
    • patch to accept entries with a condition description if no other entry with better condition is in the table:
      • buildCardData will need to add condition to carddata
      • global conditions{} to define priorities
      • then fillCardsetTable needs a new check before overwriting existing data
  • add another sitescript (and adapt library if needed)

add feature requests here, please.

Problems

  • can't set the sitescript's name dynammically.
--[[FIXME the dynamic approach myname does not work, ma.GetFile returns nil for its own log :(
do
	--local _s,_e,myname = string.find( ma.GetFile("Magic Album.log"), "Starting Lua script .-([^\\]+%.lua)$" )
	if myname then
		scriptname = myname
	else -- use hardcoded scriptname as fallback
		scriptname = "LHpi.magicuniverseDE-v2.0.lua" -- should always be equal to the scripts filename !
	end
end
--]]

  • number of cards in set must be hardcoded into library. An [url=http://www.slightlymagic.net/forum/tracker.php?p=2&t=303 ma.GetCardsInSet( setid,langid,regular|tokens )] would help, but the numbers are easy to hardcode anyways.
  • can't use mtgmintcard to get € prices. see [1] for details.
  • cards with quotation marks in their name currently confuse the script. Affected cards are
    • “Ach! Hans, Run!” (UNH)
    • Kongming, “Sleeping Dragon” (PTK)
    • Pang Tong, “Young Phoenix” (PTK)
  • Can't distinguish between regular and oversized versions of the same card. probably affects all Commander sets.

Hints for developers

Some information that might be useful if you want to write/improve sitescripts. Please add questions related to developing here, so I know what needs to be documented.

moved to How-To_sitescriptTemplate#Hints_for_developing

version history

I decided to start all versions at 2.0, since it's the continuation of magicuniverseDEv1.6.lua, but with enough changes to warrant the next major version number. Do not expect revision numbers to stay synched. Starting with 2.2.1, sitescript minor version will conform to used library version, and the sitecripts will have their version suffixed.Starting with 2.7 sitescript versioning will be libversion.dataversion.sitescriptrevision. This should make it more clear which versions to keep and what can be deleted after updating. The sitescripts will know which library and data version they need and attempt to load the right one or exit gracefully.

Staring with 2.9, the versioning was changed yet again: libversion.dataversion.sitescriptrevision still stands, but the version number will no longer be suffixed to the sitescripts' filename. They still are kept inside the script. This means that the same .csv will be used by Magic album to store the imported prices through updates, and also makes drop-in updates of library and/or datafile simpler. Sitescript revision numbers have been raised to reflect the number of past releases.


In case you need an old version or just want to see how the scripts have evolved: The version numbers below are also download links.

pre-2.0

1.2 (Downloaded 22 times)
first public release
1.3 (Downloaded 20 times)
started externalizing site-specific data to seperate .config . exeptions for some sets still remain in the code.
1.4 (Downloaded 29 times)
sanity checks should be sane again :)
config externalization progressed far enough that adding new sets should be easy
1.5 (Downloaded 5 times)
added RtR to avsets
updated expected fail and drop counts
continued config externalization and script generalization efforts
moved gpl.txt from within the zip to seperate attachment
1.6 (Downloaded 1 time)
last release under the name magicuniverseDEv1.x.lua
added Gatecrash to avsets and expectedcounts (shop does not yet provide foil)
renamed .config to .lconf to ease syntax highlighting
got rid of utf-8 BOM
changed some comments to luadoc-like

LHpi lib

2.0
completely refactored, I'll call the externalization complete:
config became sitescript, old script (and work-in-progress mtgmintcardEURO script) became the library.
improved language handling
slightly improved conflict handling: same price is merged, different price is averaged.
2.1
added number of cards per set
if no expectation is defined in sitescript, expect all cards and 0 tokens to be set successfully
minor fix to debug loging
moved handling of undefined sitescript fields to LHpi.DoImport
adapted to new sitescripts and confirmed that nothing broke in the old ones
DOING format logging more readable
reorganized LHpi.sets cardcount and variants
for kicks and giggles, have SAVETABLE generate a csv usable by woogerboys importprices :-)
2.2
fixed handling of unset cardcounts
updated for MA 1.5.2.264b (Dragon's Maze cardcount added)
fixed savepath handling for new versioning scheme
2.3
updated for MA 1.5.2.267 (M14 and MMA added)
2.4
added Theros
2.5
changed site.BCDpluginName(name, setid) to site.BCDpluginPre(card, setid) and site.BCDpluginCard(card, setid) to :site.BCDpluginPost(card, setid)
fixes to Token handling
GetSourceData now actually preserves all data from site.ParseHtmlData
adjusted handling of multiple pages per set
2.6
added BNG and Commander2013
started adding missing special and promo sets
expanded version tables for old expansions
count foiltweak events
minor improvements and fixes
2.7pre1
fix handling of site.ParseHtmlData supplied sourcerow.* data
pass supImportfoil from MainImportCycle to BuildCardData
improved card dropping in BuildCardData
ListSources now drops foilonly urls if foil import is deselected
2.7
externalized static set data to LHpi.Data
look for LHpi and LHpi.Data in Prices\lib\
set default values for unset sitescript options
fixed luadoc comments
BuildCardData drops unwanted foil/nonfoil cards/prices to make sure user wishes are honoured
2.8
DoImport: added savepath writable check to SAVECSV=true
don't assume fruc[1] is foil and all other frucs are nonfoil
  • site.fruc: add booleans to have each fruc identify itself as foil and/or nonfoil
  • site.BuildUrl: infix changed from site.frucs[frucid] to site.frucs[frucid].url
  • ProcessUserParams: disable unwanted frucs in site.sets at runtime, similar to how unwanted langs are already handled
  • ListSources: removed check for url.foilonly, ProcessUserParams should have taken care of it.
  • BuildCardData: removed @param #boolean urlfoil
BuildCardData: make sure userParams are honoured, even when preset data is present, by setting unwanted prices nil before returning the card
BuildCardData: BCDplugins should know everything BCD knows, so we're passing them importfoil, importlangs
GetSourceData: now expects site.ParseHtmlData to return card(s) wrapped in a container table
new STRICTCHECKEXPECTED option
Toutf8: new optional parameter enc
string.format'ed all nontrivial Log(string)s
read static language fields from LHpi.Data.languages
shortened site.langs
hotfix: two bugs fixed (thanks to Bloodnut for reporting them)
2.9
(all changes are transparent to sitescripts)
fixed and improved MergeCardrows (was broken if importfoil=="N" or "O" - reported by Bloodnut)
got rid of most global variables
  • curhtmlnum
    • local in MainImportCycle
  • persetcount
    • local in MainImportCycle
    • BuildCardData also returns #boolean namereplaced, #boolean foiltweaked
    • SetPrice also returns #table psetcount, #table failcount
  • totalcount,setcountdiffers
    • local in DoImport
    • MainImportCycle returns #table totalcount, #table setcountdiffers
misc. small improvements to code and/or comments
fixed loading of data file from deprecated "Prices" location
fixed SAVETABLE folder writable check
fixed logging resultregex finds
LHpi.Log now only uses ma.Log when loglevel<0 is used
LHpi.SetPrice adds abs(expected-retval) to failcount
2.10
os.clock() loglevel 2 if os is available
fix default logfile "LHpi.Log" -> "LHpi.log"
more intuitive boolean option names
  • DEBUGSKIPFOUND -> DEBUGFOUND
  • STRICTCHECKEXPECTED -> STRICTEXPECTED
  • legacy conversion for old sitescripts
merge site.variants with Data.variants, same with foiltweak

LHpi.Data

1
initial release, split from LHpi library 2.6
added all Special Sets
2
(all changes transparent to sitescripts)
minor improvements to some special sets' variant
misc. small improvements to code and/or comments
added 636:Salvat 2011

magicuniverseDE

2.0
initial release
2.1
updated for MA v1.5.2.264
use LHpi-v2.1
let LHpi lib set savepath
2.2
German Nemesis added
2.2.1
Updated for MA 1.5.2.264b (includes Dragon's Maze)
use LHpi-v2.2
2.3.1
use LHpi-v2.3
preemptively added M14
2.5.1
preemptively added Theros
removed lots of Ae -> Æ replacements
changed BuildUrl to match lib 2.5
2.5.2
fixed Theros
2.6.1
use LHpi-v2.6 (BNG)
2.8.1.1
uses LHpi-v2.8 and LHpi-Data-v1
hotfix: set options to default values
2.9.2.10
synchronized with template
updated site.expected

mtgmintcard

2.0
initial release
2.1
updated for MA v1.5.2.264
use LHpi-v2.1
let LHpi lib set savepath
2.2.1
Updated for MA 1.5.2.264b (includes Dragon's Maze)
use LHpi-v2.2
2.2.2
updated expected dropcounts
2.3.1
use LHpi-v2.3
added MMA and M14
2.5.1
added Theros
changed BuildUrl (crudely) to multiple pages per set
lots of small fixes
2.6.1
use LHpi-v2.6
added BNG,FTV:20,DD:HvsM,DD:SvT,DD:VvK,DD:EvT,DD:DvD,Beatdown,Unhinged,Unglued,Portal,Chronicles
2.8.1.1
uses LHpi-v2.8 and LHpi-Data-v1
hotfix: set options to default values
2.9.2.9
synchronized with template
updated site.expected
2.10.2.10
lib 2.10
foiltweak[setid].override=true

tcgplayerPriceGuide

2.0
initial release
just adapted Golob's avsets to LHpi syntax and tested basic functionality
2.1
updated for MA v1.5.2.264
use LHpi-v2.1
let LHpi lib set savepath
tested all core and expansions up to gatecrash, also 3 portal sets and both "Un-sets"
added namereplace table and expected card counts
commented out remaining untested sets
2.2.1
Updated for MA 1.5.2.264b (includes Dragon's Maze)
use LHpi-v2.2
2.3.1
use LHpi-v2.3
added MMA and M14
2.4.1
added Theros
lots of Ae -> Æ replacements
2.5.1
changed BuildUrl to match lib 2.5
2.6.1
use LHpi-v2.6
added BNG,FTV:20,DD:HvsM,DD:SvT,DD:VvK,DD:EvT,DD:DvD,Beatdown
now with less expected fails and slightly better support for card variants
2.7.1.1
use LHpi-v2.7 and LHpi.Data-v1
load lib from \lib subdir
cleaned site.sets' frucs
added an option to just copy English card's prices to other languages
2.8.1.1
use LHpi-v2.8
shortened site.langs
simplified site.frucs,site.sets to single fruc
hotfix: set options to default values
2.9.2.10
synchronized with template
drop all "SOON" prices (higher maintenance for site.expected,but less "0"s in averaging)
updated site.expected (apparently, they don't like Boros: a sizable portion is Plains and Mountains!?)

trader-onlineDE

2.0
unreleased
2.1
updated for MA v1.5.2.264
rewrote BuildURL and ParseHTMLData, as site was changed in the meantime.
2.2
German Nemesis added
2.2.1
Updated for MA 1.5.2.264b (includes Dragon's Maze)
use LHpi-v2.2
2.3.1
use LHpi-v2.3
added MMA and preemptively M14
2.5.1
preemptively added Theros
lots of Ae -> Æ replacements
use new 2.5 BuildCardDataPlugins for improved token handling
changed BuildUrl to match lib 2.5
2.5.2
fixed THS
2.6.1
use LHpi-v2.6 (BNG)
added BNG and all available special sets
2.8.1.1
uses LHpi-v2.8 and LHpi-Data-v1
hotfix: set options to default values
2.9.2.10
synchronized with template
updated site.expected

sitescriptTemplate

2.0
initial release as part of LHpi-v2.0.zip
2.1
use LHpi-v2.1
let LHpi lib set savepath
moved to seperate file instead of including it in LHpi lib zip
2.2.1
use LHpi-v2.2
2.5.1
use LHpi-v2.5
2.6.1
use LHpi-v2.6
pre-populated site.sets with all sets
added LOGFOILTWEAK option
2.7.1.1
use LHpi-v2.7 and LHpi.Data-v1
load lib from \lib subdir
cleaned up luadoc and improved documentation
2.8.1.1
uses LHpi-v2.8 and LHpi-Data-v1
site.frucs: new format
sample BuildUrl adapted to new fruc format
site.BCDplugins: added importfoil, importlangs parameters
site.ParseHtmlData: now returns a container, to allow multiple cards
new option STRICTCHECKEXPECTED
2.9.2.8
too much simplification prevented possible functionality: reintroduced site.langs[langid].id field for reverse lookup
split script version from scriptname to ease possible future change to versioning scheme
misc. small improvements to example code and/or comments
2.10.2.9
lib 2.10

dummyMA

0
initial release as part of LHpi-v2.0.zip

0.1

moved to seperate file instead of including it in LHpi lib zip
0.2
Backwards compatible with Lua5.1
tested with seperated LHpi.Data
0.3
misc. small improvements to code and/or comments
patch paths, because dev src has been moved from MA\Prices
dummy.fakesitescript()
0.4
execution timer in main()
global variables changed to dummy.*
new dummy.performancetest(repeats,script,impF,impL,impS,timefile)
more comfort, less code duplication

License

The LHpi library and all its sitescripts are released as open source under the [GNU General Public License]. Please note that LHpi is thusly licensed seperately from Magic Album proper, which to date is closed source.

Other scripts

Just for reference: These unrelated pricing scripts exist

  • "MTG Mint Card.lua" by Goblin Hero and Stromglad1 is included with Magic Album as a working script example.
  • "Import Prices" by woogerboy21.