3 import xml
.etree
.ElementTree
as ET
4 from collections
import defaultdict
9 def __init__(self
, name
=None, xml
=None, url
=None, txt
=None):
14 self
.parse_ponyhead_xml(xml
)
16 self
.parse_ponyhead_link(url
)
24 aspects
= defaultdict(int)
25 for card
in self
.cards
:
26 if card
['type'] == 'Mane':
27 mane
= (card
['title'], mlpccg
.CardDb
.CardDb
.set_ids
[card
['set']].upper())
29 elif card
['type'] == 'Friend':
30 aspects
[card
['color']] += 1
32 elif card
['type'] != 'Problem':
33 aspects
[card
['type']] += 1
35 aspects_sorted
= sorted(aspects
.iteritems(), reverse
=True, key
=operator
.itemgetter(1))
36 aspects_sum
= float(sum([aspect
[1] for aspect
in aspects_sorted
]))
40 for aspect
, value
in aspects_sorted
:
41 amount
+= value
/ aspects_sum
42 aspects_filtered
+= [aspect
]
46 return (mane
, aspects_filtered
)
49 def description(self
):
50 mane
, aspects
= self
.aspects
51 if not mane
or len(aspects
) == 0:
54 return '%s %s %s' % (mane
[0], mane
[1], '/'.join(aspects
))
57 def ponyhead_link(self
):
58 cards
= defaultdict(int)
59 for card
in self
.cards
:
60 cards
[card
['id'].replace('f', 'F')] += 1
62 cards
= ['%sx%d' % (id, amount
) for id, amount
in cards
.iteritems()]
64 return 'http://ponyhead.com/deckbuilder?v1code=%s' % '-'.join(cards
)
66 def parse_ponyhead_xml(self
, path
):
67 logging
.debug('loading decklist from xml')
70 root
= ET
.parse(path
).getroot()
71 card_names
= [element
.text
.strip() for element
in root
.findall('.//card/name')]
72 for card_name
in card_names
:
73 self
.cards
+= [mlpccg
.CardDb
.CARDDB
.by_name(card_name
)['id']]
75 logging
.exception('failed')
77 def parse_ponyhead_link(self
, link
):
78 logging
.debug('loading decklist by parsing url')
81 id_list
= [id.split('x') for id in link
.split('=')[-1].split('-')]
82 for id, amount
in id_list
:
83 self
.cards
+= [mlpccg
.CardDb
.CARDDB
.by_id(id)] * int(amount
)
85 logging
.exception('failed: %s', link
)
87 def parse_ponyhead_txt(self
):