33import java .io .IOException ;
44import java .util .ArrayList ;
55import java .util .Collections ;
6+ import java .util .EnumSet ;
67import java .util .List ;
78import java .util .Optional ;
89import java .util .Set ;
@@ -73,20 +74,24 @@ private static class MagicCardBuilder{
7374 private Rarity rarity ;
7475 private String text ;
7576 private String flavorText ;
76- private Optional < Integer > power ;
77- private Optional < Integer > toughness ;
77+ private Integer power ;
78+ private Integer toughness ;
7879 private String imageUrl ;
7980 private String priceUrl ;
8081
8182 public static void main (String ... args ) throws IOException {
8283 MagicCardBuilder temp = new MagicCardBuilder (Jsoup .connect (
83- "http://magiccards.info/shm /en/224 .html" ).get ());
84+ "http://magiccards.info/ne /en/103 .html" ).get ());
8485 temp .extractName ();
8586 temp .extractCost ();
8687 temp .extractColors ();
8788 temp .extractTypes ();
8889 temp .extractRarity ();
89- System .out .println (temp .colors );
90+ temp .extractText ();
91+ temp .extractFlavorText ();
92+ temp .extractPowerAndToughness ();
93+ temp .extractImageUrl ();
94+ System .out .println (temp .imageUrl );
9095 }
9196
9297 public MagicCardBuilder (Document doc ){
@@ -96,7 +101,6 @@ public MagicCardBuilder(Document doc){
96101 private void extractName () {
97102 name = doc .select ("body > table:nth-child(7) > tbody > tr > td:nth-child(2) > span > a" )
98103 .get (0 ).text ();
99- return ;
100104 }
101105
102106 private void extractCost (){
@@ -112,12 +116,15 @@ private void extractCost(){
112116 while (costMatcher .find ()){
113117 manaCost .add (costMatcher .group ());
114118 }
115- return ;
116119 }
117120
118121 private void extractColors (){
119- colors = manaCost .stream ().map (Color ::parseColor ).flatMap (Set ::stream ).filter (x -> x != null ).collect (Collectors .toSet ());
122+ colors = manaCost .stream ().map (Color ::parseColor )
123+ .flatMap (Set ::stream ).filter (x -> x != null )
124+ .collect (Collectors .toSet ());
120125 if (colors .isEmpty ()){
126+ colors = EnumSet .of (Color .COLORLESS );
127+ }
121128 }
122129
123130 private void extractTypes (){
@@ -127,7 +134,9 @@ private void extractTypes(){
127134 types = Collections .emptySet ();
128135 return ;
129136 }
130- types = Stream .of (matcher .group (1 ).split ("\\ s" )).map (Type ::parseType ).filter (x -> x != null ).collect (Collectors .toSet ());
137+ types = Stream .of (matcher .group (1 ).split ("\\ s" ))
138+ .map (Type ::parseType ).filter (x -> x != null )
139+ .collect (Collectors .toSet ());
131140 }
132141
133142 private void extractRarity (){
@@ -137,6 +146,51 @@ private void extractRarity(){
137146 this .rarity = Rarity .parseRarity (rarity );
138147 }
139148
149+ private void extractText (){
150+ text = doc .select ("body > table:nth-child(7) > tbody > tr > td:nth-child(2) > p.ctext > b" )
151+ .get (0 ).html ().replace ("<br>" , "\n " );
152+ }
153+
154+ private void extractFlavorText (){
155+ flavorText = doc .select ("body > table:nth-child(7) > tbody > tr > td:nth-child(2) > p:nth-child(4) > i" )
156+ .get (0 ).html ().replace ("<br>" , "\n " );
157+ }
158+
159+ private void extractPowerAndToughness (){
160+ String textBox = doc .select ("body > table:nth-child(7) > tbody > tr > td:nth-child(2) > p:nth-child(2)" ).get (0 ).text ();
161+ Matcher matcher = costTypePattern .matcher (textBox );
162+ if (!matcher .matches ()){
163+ power = null ;
164+ toughness = null ;
165+ return ;
166+ }
167+ String [] typeAndPower = matcher .group (1 ).split (" " );
168+ String [] powerToughness = typeAndPower [typeAndPower .length - 1 ].split ("/" );
169+ if (powerToughness .length < 2 ){
170+ power = null ;
171+ toughness = null ;
172+ return ;
173+ }
174+ try {
175+ power = Integer .parseInt (powerToughness [0 ]);
176+ }catch (NumberFormatException e ){
177+ power = null ;
178+ }
179+ try {
180+ toughness = Integer .parseInt (powerToughness [1 ]);
181+ }catch (NumberFormatException e ){
182+ toughness = null ;
183+ }
184+ }
185+
186+ private void extractImageUrl (){
187+ imageUrl = doc .select ("body > table:nth-child(7) > tbody > tr > td:nth-child(1) > img" ).get (0 ).attr ("src" );
188+ }
189+
190+ private void extractPriceUrl (){
191+
192+ }
193+
140194
141195 }
142196
0 commit comments