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
43 aspects_filtered
+= [aspect
]
47 return (mane
, aspects_filtered
)
50 def description(self
):
51 mane
, aspects
= self
.aspects
52 if not mane
or len(aspects
) == 0:
55 return '%s %s %s' % (mane
[0], mane
[1], '/'.join(aspects
))
58 def ponyhead_link(self
):
59 cards
= defaultdict(int)
60 for card
in self
.cards
:
61 cards
[card
['id'].replace('f', 'F')] += 1
63 cards
= ['%sx%d' % (id, amount
) for id, amount
in cards
.iteritems()]
65 return 'http://ponyhead.com/deckbuilder?v1code=%s' % '-'.join(cards
)
67 def parse_ponyhead_xml(self
, path
):
68 logging
.debug('loading decklist from xml')
71 root
= ET
.parse(path
).getroot()
72 card_names
= [element
.text
.strip() for element
in root
.findall('.//card/name')]
73 for card_name
in card_names
:
74 self
.cards
+= [mlpccg
.CardDb
.CARDDB
.by_name(card_name
)['id']]
76 logging
.exception('failed')
78 def parse_ponyhead_link(self
, link
):
79 logging
.debug('loading decklist by parsing url')
82 id_list
= [id.split('x') for id in link
.split('=')[-1].split('-')]
83 for id, amount
in id_list
:
84 self
.cards
+= [mlpccg
.CardDb
.CARDDB
.by_id(id)] * int(amount
)
86 logging
.exception('failed: %s', link
)
88 def parse_ponyhead_txt(self
):