-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.py
1431 lines (1219 loc) · 63.9 KB
/
helpers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import base64
import json
from pprint import pprint
from random import choice, sample
import random
import re
from PIL import Image
from io import BytesIO
import scrython
from bs4 import BeautifulSoup
import requests
import datetime as DT
from rq import Queue
from worker import conn
# from test import count_words_at_url
from rq import get_current_job
import time
import os
from pymongo import UpdateOne
# from flask import current_app as app
from app import mongo
# from db import mongo
# DB_URI = os.environ['DB_URI']
# print(DB_URI)
def is_there_new_data() -> dict:
"""
Checks if new modern tournament decklist data has been published from MTGO
:return:is_new_data :bool,latest_modern_tournament_url:str
"""
url_data = read_from_file('static/card_data_url.json')
urls_doc_from_db = mongo.db.urls.find_one({"_id": "decklists_urls"})
# pprint(urls_doc_from_db)
urls_from_db = urls_doc_from_db['urls']
today = DT.date.today()
week_ago = today - DT.timedelta(days=7)
# d = DT.datetime.strptime(today, '%Y-%m-%d')
today = DT.date.strftime(today, "%m/%d/%Y")
week_ago = DT.date.strftime(week_ago, "%m/%d/%Y")
# print(today, week_ago)
is_new_data = False
url = 'https://magic.wizards.com/en/section-articles-see-more-ajax?dateoff=&l=en&f=9041&search-result-theme=&limit=30&fromDate=' + week_ago + '&toDate=' + today + '&sort=ASC&word=modern&offset=0'
# url = 'https://magic.wizards.com/en/content/deck-lists-magic-online-products-game-info'
# pprint(url)
response = requests.get(url)
json_data = response.json()
# for url in response['data']
response_html = json_data['data']
response_html.reverse()
# pprint(response_html)
soup = BeautifulSoup('"""' + ''.join(response_html) + '"""', 'html.parser')
# soup = BeautifulSoup(response_html, 'html.parser')
# print(soup)
tournament_urls = []
latest_modern_tournament = soup.findAll("a")
# pprint(latest_modern_tournament)
for a in latest_modern_tournament:
# pprint(a.text)
# if "League" not in a.text:
if "League" not in a.text and 'Modern' in a.text:
tournament_urls.append('https://magic.wizards.com' + a['href'])
# text=re.compile('\bModern\s*(League|Challenge|Preliminary)', flags=re.IGNORECASE))
# print(latest_modern_tournament)
if latest_modern_tournament is None:
latest_modern_tournament_urls = urls_from_db
elif tournament_urls:
# latest_modern_tournament_parent = latest_modern_tournament.parent.parent.parent
latest_modern_tournament_urls = tournament_urls
#
# latest_modern_tournament_url = 'https://magic.wizards.com/en/articles/archive/mtgo-standings/modern-league-2020-07-21'
# latest_modern_tournament_url = 'https://magic.wizards.com' + latest_modern_tournament_parent['href']
#
# data = read_from_file('static/card_data_url.json')
set_data_url = set(urls_from_db)
set_latest_modern_tournament_urls = set(latest_modern_tournament_urls)
# pprint(set_data_url)
# pprint(set_latest_modern_tournament_urls)
if set_data_url != set_latest_modern_tournament_urls:
# if data['url'] != latest_modern_tournament_urls:
is_new_data = True
# return tournament_urls
else:
latest_modern_tournament_urls = urls_from_db
# return {"is_new_data": True, "latest_modern_tournament_urls": latest_modern_tournament_urls}
return {"is_new_data": is_new_data, "latest_modern_tournament_urls": latest_modern_tournament_urls}
def scrape_card_data() -> dict:
"""
Using Beautiful soup scrapes card names,types and decklist_ids and saves that information
in dict format to a local JSON file
:return:unique_cards:Dict format card data,modern_league_url:str,cards_from:str
"""
card_data = read_from_file('static/card_data_url.json')
# card_names_data = read_from_file('static/latest_card_names.json')
# latest_card_names = read_from_file('static/latest_card_names.json')
# existing_card_data = card_data['card_set']
existing_card_data = []
urls_to_add = []
card_names_to_add = []
cards = mongo.db.cards
urls = mongo.db.urls
modern_atomic = mongo.db.modern_atomic
list_card_names_db = mongo.db.list_card_names
card_types_sets = mongo.db.card_types_sets
card_ids = cards.find().distinct('_id')
card_types_dict = {}
# existing_urls = card_data['url']
is_there_new_data_data = is_there_new_data()
# is_new_data = True
is_new_data = is_there_new_data_data['is_new_data']
# latest_modern_tournament_urls = [
# 'https://magic.wizards.com/en/articles/archive/mtgo-standings/modern-preliminary-2020-08-08']
latest_modern_tournament_urls = is_there_new_data_data['latest_modern_tournament_urls']
if is_new_data:
print('update urls')
cards_from = 'cards from URL'
print(cards_from)
# get_new_url = True
# card_data_json = open('static/card_data_url.json', 'w')
#
# card_names_list = open('static/latest_card_names.json', 'w')
card_list = [] # list of dict items
for url in latest_modern_tournament_urls[::-1]:
# if url not in existing_urls:
modern_league_url_from_file = url
modern_league_url = modern_league_url_from_file
# modern_league_url = 'https://magic.wizards.com' + modern_league_url_from_file
# pprint(modern_league_url)
response_league = requests.get(modern_league_url)
modern_deck_lists = BeautifulSoup(response_league.text, 'html.parser')
sorted_by_type = modern_deck_lists.findAll('div',
attrs={'class': re.compile(
'^sorted-by-(Tribal|Planeswalker|Creature|Sorcery|Land|Enchantment|Artifact|Instant|Sideboard|Other)',
flags=re.IGNORECASE)})
# pprint(sorted_by_type)
new_card_name_list = set() # list of dict items
# card_name_in_list = bool
for sorted_by_type in sorted_by_type:
# print(sorted_by_type.find('h5').string)
cards_in_decks = sorted_by_type.findAll('a', attrs={'class': 'deck-list-link'})
for card_name_div in cards_in_decks:
dict_to_add = {}
card_deck_list_id = card_name_div.parent.parent.parent.parent.parent.parent.parent.get('id')
if not card_deck_list_id:
card_deck_list_id = card_name_div.parent.parent.parent.parent.parent.parent.get('id')
# print(card_deck_list_id)
card_type = card_name_div.parent.parent.parent.find('h5').string
card_type = card_type[:card_type.index(" (")]
card_name = card_name_div.string
# if "Snapcaster Mage" in card_name:
# print(card_name_div.string + ': ' + card_type)
# print(div.string + ': ' + str(is_this_a_basic(div.string)))
# if ((any(d['name'] == card_name_div.string for d in card_list)) and (
# not is_this_a_basic(card_name_div.string)) or not card_list):
card_name_in_list = any(card_name in d for d in card_list)
card_list.append(card_name)
# pprint(card_list)
print(card_name, card_name_in_list)
# card_name_in_list = any(d.get('name', 'icara') == card_name for d in card_list)
if '//' in card_name:
print("// in name {} ".format(card_name))
split_str = card_name.split('//', 1)
for split_card_name in split_str:
split_card_name = split_card_name.rstrip().lstrip()
# in_existing_card_data = mongo.db.cards.count_documents({'_id': split_card_name}, limit=1)
# if "Tear" in split_card_name:
# in_existing_card_data = True if cards.count_documents({'_id': "Tear"},
# limit=1) else False
#
# try:
# pprint(cards.find_one({'_id': "Tear"})['_id'])
# except TypeError:
# print("nema takoa jivotno")
# else:
# in_existing_card_data = True if cards.count_documents({'_id': split_card_name},
# limit=1) else False
# in_existing_card_data = False
# in_existing_card_data = False
in_existing_card_data = any(split_card_name in d for d in card_ids)
if (not card_name_in_list and not in_existing_card_data and (
not is_this_a_basic(card_name))):
# print('// card not in data')
print("// in name {} - not in existing data".format(split_card_name))
dict_to_add = get_single_card_data_from_scryfall(split_card_name)
# dict_to_add[split_card_name]['type'] = card_type
dict_to_add['decklist_id'] = url + "#" + card_deck_list_id
# dict_to_add[split_card_name]['decklist_id'] = url + "#" + card_deck_list_id
existing_card_data.append(dict_to_add)
# existing_card_data.append(dict_to_add)
pprint("new_card_name_list adding split card - {}".format(split_card_name))
new_card_name_list.add(split_card_name)
type_from_atomic = modern_atomic.find_one({"_id": split_card_name})['types'][0]
add_to_type_list(split_card_name, type_from_atomic, card_types_dict)
else:
# print('// card in existing data')
print("{} - // in name and in existing data".format(split_card_name))
# card_name_in_list.append(card_name)
pprint("new_card_name_list adding split card - {}".format(split_card_name))
new_card_name_list.add(split_card_name)
type_from_atomic = modern_atomic.find_one({"_id": split_card_name})['types'][0]
add_to_type_list(split_card_name, type_from_atomic, card_types_dict)
# update_existing_decklist_url_in_db = cards.update_one({"_id": split_card_name},
# {"$set": {
# "decklist_id": url + "#" + card_deck_list_id}},
# upsert=True)
# for card in existing_card_data:
# # pprint(card)
# if split_card_name in card.keys():
# # existing_card_data[card[split_card_name]['decklist_id']] = card_deck_list_id
# index = existing_card_data.index(card)
# existing_card_data[index][split_card_name][
# 'decklist_id'] = url + "#" + card_deck_list_id
# # card[split_card_name]['decklist_id'] = card_deck_list_id
# break
# else:
#
# new_card_name_list.append(card_name)
# # existing_card_data[card_name]['decklist_id'] = card_deck_list_id
# for card in existing_card_data:
# if card_name in card.keys():
# card[card_name]['decklist_id'] = card_deck_list_id
# break
else:
# print('regular name')
# in_existing_card_data = mongo.db.cards.count_documents({'_id': card_name}, limit=1)
# in_existing_card_data = any(card_name in d for d in card_ids)
in_existing_card_data = True if cards.count_documents({'_id': card_name},
limit=1) else False
# in_existing_card_data = False
if not is_this_a_basic(card_name):
if not card_name_in_list and not in_existing_card_data:
# pprint('')
# print("{} - regular name - not in existing card data".format(card_name))
# if (card_name_div.string not in card_list) and (not is_this_a_basic(card_name_div.string)):
dict_to_add = get_single_card_data_from_scryfall(card_name)
# pprint(dict_to_add)
# dict_to_add['name'] = card_name_div.string
dict_to_add['decklist_id'] = url + "#" + card_deck_list_id
# dict_to_add[card_name]['decklist_id'] = url + "#" + card_deck_list_id
existing_card_data.append(dict_to_add)
# existing_card_data.append(dict_to_add)
card_list.append(card_name)
new_card_name_list.add(card_name)
try:
type_from_atomic = modern_atomic.find_one({"_id": card_name})['types'][0]
except TypeError:
print("type not found for - ", card_name)
add_to_type_list(card_name, type_from_atomic, card_types_dict)
else:
# print("{} - regular name - In data file".format(card_name))
# card_name_in_list.append(card_name)
new_card_name_list.add(card_name)
# existing_card_data[card_name]['decklist_id'] = card_deck_list_id
update_existing_decklist_url_in_db = cards.update_one({"_id": card_name},
{"$set": {
"decklist_id": url + "#" + card_deck_list_id}},
upsert=True)
try:
type_from_atomic = modern_atomic.find_one({"_id": card_name})['types'][0]
except TypeError:
print("type not found for - ", card_name)
add_to_type_list(card_name, type_from_atomic, card_types_dict)
# for card in existing_card_data:
# if card_name in card.keys():
# # card[card_name]['decklist_id'] = card_deck_list_idndex
# index = existing_card_data.index(card)
# existing_card_data[index][card_name][
# 'decklist_id'] = url + "#" + card_deck_list_id
# break
# if dict_to_add:
# card_list.append(dict_to_add)
# unique_cards = existing_card_data
# https://stackoverflow.com/questions/11092511/python-list-of-unique-dictionaries
# unique_cards = [dict(s) for s in set(frozenset(d.items()) for d in card_list)]
# existing_card_data.append(list_dicts_to_add)
# urls_to_add.insert(0, url)
urls_to_add.append(url)
# card_names_to_add.insert(0, list(new_card_name_list))
card_names_to_add.append(list(new_card_name_list))
# urls_to_add.append(temp_urls)
# existing_card_names.append(card_names_to_add)
# existing_urls.pop(0)
# card_names_to_add.pop(0)
# pprint(existing_card_data)
# pprint(card_names_to_add)
# pprint(urls_to_add)
# dict_to_file = {
# 'url': urls_to_add,
# "card_set": existing_card_data,
# }
insert_urls_in_db = urls.update_one({"_id": "decklists_urls"},
{"$set": {"urls": urls_to_add}},
upsert=True)
set_card_names = set()
for list_card_names in card_names_to_add:
for card in list_card_names:
set_card_names.add(card)
# latest_card_names_dict = {
# "card_names": card_names_to_add,
# "set_card_names": list(set_card_names)
# }
for type in card_types_dict.keys():
insert_card_types_sets_in_db = card_types_sets.update_one({"_id": type},
{"$set": {
"set_card_names": card_types_dict[type]}},
upsert=True)
insert_list_card_names_in_db = list_card_names_db.update_one({"_id": "card_names"},
{"$set": {"set_card_names": list(set_card_names),
"card_names": card_names_to_add}},
upsert=True)
if existing_card_data:
upserts = [UpdateOne({'_id': x['_id']}, {'$set': x}, upsert=True) for x in existing_card_data]
# upserts = [UpdateOne({'_id': x['_id']}, {'$setOnInsert': x}, upsert=True) for x in existing_card_data]
insert_new_cards = cards.bulk_write(upserts)
# with open('static/card_data_url.json', 'w') as fp:
# json.dump(dict_to_file, fp, sort_keys=True, indent=4)
#
# with open('static/latest_card_names.json', 'w') as fp:
# json.dump(latest_card_names_dict, fp, sort_keys=True, indent=4)
# card_data_json.close()
# card_names_list.close()
else:
modern_league_url = card_data['url']
cards_from = 'cards from file'
unique_cards = card_data['card_set']
print(cards_from)
pprint(card_types_dict)
return {
"modern_league_url": 'https://magic.wizards.com',
# "modern_league_url": 'https://magic.wizards.com' + modern_league_url,
# "unique_cards": unique_cards,
"cards_from": cards_from
}
def replace_card_name_in_oracle(name: str, oracle_text: str) -> str:
"""
Replaces the name of the card in the oracle text with "This card" and returns it
:param name: The name of the card
:param oracle_text: The Oracle text of the card
:return: oracle_text (str):
"""
html = '<span class="badge badge-secondary">This card</span>'
# html = '<span class="badge badge-secondary align-text-top">This card</span>'
names = name.split("//")
for name in names:
name = name.rstrip().lstrip()
if name in oracle_text:
# pprint('name after comma oracle')
oracle_text = oracle_text.replace(name, html)
if ',' in name:
name_before_comma = name[:name.index(",")]
if name_before_comma in oracle_text:
# pprint('name_before_comma in oracle')
oracle_text = oracle_text.replace(name_before_comma, html)
if 'the' in name:
name_before_the = name[:name.index("the")]
if name_before_the in oracle_text:
# pprint('name_before_comma in oracle')
oracle_text = oracle_text.replace(name_before_the, html + " ")
oracle_text = oracle_text.replace('−', '<span class="minus">−</span>')
return oracle_text
def is_this_a_basic(potential_basic: str) -> bool:
"""
Returns True if potential_basic is a name of a basic land
:param potential_basic:str()
:return:
"""
if potential_basic == 'Swamp' or \
potential_basic == 'Mountain' or \
potential_basic == 'Island' or \
potential_basic == 'Plains' or \
potential_basic == 'Forest' or \
potential_basic == 'Wastes' or \
'Snow-Covered' in potential_basic:
return True
return False
def replace_symbols_in_text(oracle_text: str) -> str:
"""
Replaces mana and other special symbols in oracle text with a
HTML <span> with the corresponding class
:param oracle_text: str()
:return: oracle_text
"""
symbols = {
"{T}": "st",
"{Q}": "sq",
"{E}": "se",
"{0}": "s0",
"{1}": "s1",
"{2}": "s2",
"{3}": "s3",
"{4}": "s4",
"{5}": "s5",
"{6}": "s6",
"{7}": "s7",
"{8}": "s8",
"{9}": "s9",
"{10}": "s10",
"{11}": "s11",
"{12}": "s12",
"{13}": "s13",
"{14}": "s14",
"{15}": "s15",
"{16}": "s16",
"{17}": "s17",
"{18}": "s18",
"{19}": "s19",
"{20}": "s20",
"{W/U}": "swu",
"{W/B}": "swb",
"{B/R}": "sbr",
"{B/G}": "sbg",
"{U/B}": "sub",
"{U/R}": "sur",
"{R/G}": "srg",
"{R/W}": "srw",
"{G/W}": "sgw",
"{G/U}": "sgu",
"{2/W}": "s2w",
"{2/U}": "s2u",
"{2/B}": "s2b",
"{2/R}": "s2r",
"{2/G}": "s2g",
"{W/P}": "swp",
"{U/P}": "sup",
"{B/P}": "sbp",
"{R/P}": "srp",
"{G/P}": "sgp",
"{W}": "sw",
"{U}": "su",
"{B}": "sb",
"{R}": "sr",
"{G}": "sg",
"{C}": "sc",
"{S}": "ss",
"{X}": "sx",
}
for key, value in symbols.items():
# pprint(key)
if str(key) in oracle_text:
oracle_text = oracle_text.replace(str(key), '<span class="mana small align-middle ' + value + '"></span>')
# oracle_text = oracle_text.replace(key, )
return oracle_text
def read_from_file(filename: str) -> dict:
"""
Return JSON data from file as a dict object
:param filename: str() Path to file
:return: JSON data
"""
# with open(file_name) as f:
# return json.load(f)
with open(filename, 'r') as fp:
data = json.load(fp)
return data
def get_single_card_data_from_scryfall(card: str) -> dict:
"""
Fetches Oracle text, Image URL, Flavor text from Scryfall's API
:param card: str() name of the card we want to return data for
:return: dict()
"""
card_info = scrython.cards.Named(fuzzy=card)
card_info = vars(card_info)
pprint(card_info['scryfallJson'])
# pprint(card_info['scryfallJson']['mana_cost'])
# pprint(replace_symbols_in_text(card_info['scryfallJson']['mana_cost']))
card_data, card_oracle_txt, card_mana_cost, card_img, card_flavor_txt = {}, '', '', '', card
if 'card_faces' in card_info['scryfallJson']:
for card_faces in card_info['scryfallJson']['card_faces']:
if card in card_faces['name']:
card_oracle_txt = card_faces['oracle_text']
if "flavor_text" in card_faces:
card_flavor_txt = card_faces['flavor_text']
if "mana_cost" in card_faces:
card_mana_cost = card_faces['mana_cost']
# card_mana_cost = replace_symbols_in_text(card_faces['mana_cost'])
if "image_uris" not in card_info['scryfallJson']:
card_img = card_faces['image_uris']['art_crop']
else:
card_img = card_info['scryfallJson']['image_uris']['art_crop']
else:
card_oracle_txt = card_info['scryfallJson']['oracle_text']
if 'mana_cost' in card_info['scryfallJson']:
card_mana_cost = card_info['scryfallJson']['mana_cost']
# card_mana_cost = replace_symbols_in_text(card_info['scryfallJson']['mana_cost'])
card_img = card_info['scryfallJson']['image_uris']['art_crop']
if "flavor_text" in card_info['scryfallJson']:
card_flavor_txt = card_info['scryfallJson']['flavor_text']
card_data = {
'_id': card,
'name': card,
'oracle_text': card_oracle_txt,
'mana_cost': card_mana_cost,
'flavor_text': card_flavor_txt,
'image': card_img,
# 'image_url': pil2datauri(get_mtg_img_from_url(card_img))
# 'image_uri': pil2datauri(get_mtg_img_from_url(card_img))
}
# card_data = {
# card: {
# 'name': card,
# 'oracle_text': card_oracle_txt,
# 'flavor_text': card_flavor_txt,
# 'image': card_img
# }}
# pprint(card_data.keys())
return card_data
def get_card_data_from_local_file(card: str) -> dict:
# random_card_data = {}
cards = mongo.db.cards
# TODO: Check if all keys are persent in card_info before getting oracle text etc from it, not only for URIs
pprint("get_card_data_from_local_file broken card - {}".format(card))
# data_api = read_from_file('static/card_data_url.json')
# card_data_scryfall = data_api['card_set']
# card_data_mtgjson = read_from_file('static/ModernAtomic.json')
# card_data = json.loads(open("static/ModernAtomic.json", encoding="utf8").read())
# for card in card:
# pprint(card)
# card_info = list(filter(lambda x: x if card in x.keys() else None, card_data_scryfall))[0]
card_info = cards.find_one({"_id": card})
# new_card_info = get_single_card_data_from_scryfall(card)
# pprint(card_info['mana_cost'])
if 'oracle_text' not in card_info.keys():
new_card_info = get_single_card_data_from_scryfall(card)
card_info['oracle_text'] = new_card_info['oracle_text']
card_info['flavor_text'] = new_card_info['flavor_text']
card_info['image'] = new_card_info['image']
card_info['mana_cost'] = new_card_info['mana_cost']
# card_info['image_uri'] = new_card_info['image_uri']
try:
if "decklist_id" in card_info.keys():
decklist_id = card_info['decklist_id']
else:
decklist_id = ""
except KeyError:
print("decklist missing")
update_existing_decklist_url_in_db = cards.update_one({"_id": card},
{"$set":
{
"oracle_text": card_info['oracle_text'],
"flavor_text": card_info['flavor_text'],
"decklist_id": decklist_id,
"image": card_info['image'],
"mana_cost": card_info['mana_cost'],
# "image_uri": card_info['image_uri'],
},
},
upsert=True)
# elif card_info is not None and 'image_url' in card_info.keys():
# card_info['image_uri'] = pil2datauri(get_mtg_img_from_url(card_info['image_url']))
# card_info['image_uri'] = card_info['image_uri']
# else:
# card_info['image_uri'] = pil2datauri(get_mtg_img_from_url(card))
# card_info_count = card_info.count()
# pprint(card_info)
# card_info = [x for x in card_data_scryfall if card in x.keys()][0]
# card_info = [x for x in card_data_scryfall if card in x.keys()][0]
# pprint(card_info)
# card_info = next((item for item in card_data_scryfall if card in item), None)
# card_name = next((key for key in card_data['data'].keys() if card in key), None)
# card_name = next(
# (key for key in card_data_mtgjson['data'].keys() if
# card in [key.rstrip().lstrip() for key in key.split("//") if string_found(card, key)]), None)
# TODO FIX ME PLEASE check for each field if it is in card then create vars
# if card_info is not None and 'image_uri' in card_info.keys():
# card_info['image_uri'] = card_info['image_uri']
# else:
# print("update card with no image_uri from scryfall api ")
# new_card_info = get_single_card_data_from_scryfall(card)
# card_info['oracle_text'] = new_card_info['oracle_text']
# card_info['flavor_text'] = new_card_info['flavor_text']
# # card_info['decklist_id'] = new_card_info['decklist_id']
# card_info['image'] = new_card_info['image']
# card_info['image_uri'] = new_card_info['image_uri']
# # oracle_text = new_card_info['oracle_text']
# # image_uri = get_single_card_data_from_scryfall(card)['image_uri']
#
# update_existing_decklist_url_in_db = cards.update_one({"_id": card},
# {"$set":
# {
# "oracle_text": card_info['oracle_text'],
# "flavor_text": card_info['flavor_text'],
# "decklist_id": card_info['decklist_id'],
# "image": card_info['image'],
# "image_uri": card_info['image_uri'],
# },
# },
# upsert=True)
# pprint(card_info)
# card_data_json = card_data_mtgjson['data'][card_name][0]
random_card_data = {
'name': card,
'oracle_text': card_info['oracle_text'],
'flavor_text': card_info['flavor_text'],
'decklist_id': card_info['decklist_id'],
'mana_cost': card_info['mana_cost'],
'image': card_info['image'],
# 'image_uri': card_info['image_uri']
}
# pprint(random_card_data)
return random_card_data
def string_found(string1, string2):
if re.search(r"\b" + re.escape(string1) + r"\b", string2):
return True
return False
def find_all(card_type, card_colors, card_subtypes, card_identity, card_cmc, not_enough, card_name, card_supertypes,
last_chance, card_names_atomic):
modern_atomic = mongo.db.modern_atomic
# count = modern_atomic.count()
# pprint()
# find_all = modern_atomic.aggregate([{"$sample": {"size": 14000}}], batchSize=14000)
# find_all = modern_atomic.aggregate([{"$sample": {"size": count}}])
find_all = modern_atomic.find({})
print(type(find_all))
# pprint(find_all.count())
list_similar_cards = []
# counter = 0
for card in find_all:
# if len(list_similar_cards) > 20:
# break
# if card['_id'] == "Nylea, God of the Hunt":
if card_name != card['_id'] and card['_id'] not in card_names_atomic and "//" not in card['_id']:
# counter += 1
# print(counter, ' ', card['_id'])
try:
current_type = card['type']
except KeyError:
print("No card type for - {0}".format(card['_id']))
current_subtypes = card['subtypes']
current_supertypes = card['supertypes']
current_colors = card['colors']
current_identity = card['colorIdentity']
current_id = card['_id']
# print(current_id,card_name)
current_name = card['name']
# pprint(current_name)
current_cmc = card['convertedManaCost'] if 'convertedManaCost' in card.keys() else ""
# if current_type != card_type:
# continue
if is_this_a_basic(current_id):
# pprint(k)
continue
# if 'Creature' in card_type and \
# 'Creature' in current_type and \
# 'Artifact' in card_type and \
# 'Artifact' in current_type:
# print("wurm here")
if 'Creature' in card_type and 'Creature' in current_type and 'Artifact' in card_type and 'Artifact' in current_type:
# print('creature artifact')
if not_enough:
# print("not enough wurms")
if card_subtypes[0] in current_subtypes:
if current_cmc == card_cmc:
list_similar_cards.append(current_id)
elif current_colors == card_colors:
list_similar_cards.append(current_id)
elif card_supertypes == current_supertypes and current_cmc == card_cmc and (
card_colors and current_colors == card_colors or current_identity == card_identity):
list_similar_cards.append(current_id)
elif last_chance:
if card_supertypes == current_supertypes and (
card_colors and current_colors == card_colors or current_identity == card_identity):
list_similar_cards.append(current_id)
else:
if card_subtypes[
0] in current_subtypes and current_colors == card_colors and current_cmc == card_cmc:
# if subtypes == card_subtypes and colors == card_colors and cmc == card_cmc:
list_similar_cards.append(current_id)
elif 'Creature' in card_type and 'Creature' in current_type and current_id not in list_similar_cards and 'Artifact' not in card_type and 'Artifact' not in current_type:
if not_enough:
if card_subtypes[0] in current_subtypes and card_colors:
if card_supertypes and card_supertypes == current_supertypes and current_colors == card_colors and current_cmc == card_cmc:
list_similar_cards.append(current_id)
# continue
elif card_supertypes and card_supertypes == current_supertypes and current_colors[
0] in card_colors:
list_similar_cards.append(current_id)
# continue
elif card_supertypes and card_supertypes == current_supertypes and current_cmc == card_cmc:
list_similar_cards.append(current_id)
elif current_colors:
if card_supertypes and card_supertypes == current_supertypes and current_cmc == card_cmc and \
current_colors[0] in card_colors:
list_similar_cards.append(current_id)
elif card_subtypes[0] in current_subtypes and current_colors[
0] in card_colors and current_cmc == card_cmc:
list_similar_cards.append(current_id)
elif card_subtypes[0] in current_subtypes:
if card_supertypes and card_supertypes == current_supertypes and current_colors == card_colors and current_cmc == card_cmc:
list_similar_cards.append(current_id)
# continue
elif card_supertypes and card_supertypes == current_supertypes and current_cmc == card_cmc:
list_similar_cards.append(current_id)
elif current_cmc == card_cmc:
list_similar_cards.append(current_id)
elif last_chance:
if card_supertypes and card_supertypes == current_supertypes and current_cmc == card_cmc and \
card_subtypes[0] in current_subtypes:
list_similar_cards.append(current_id)
elif card_supertypes and card_supertypes == current_supertypes and current_cmc == card_cmc:
list_similar_cards.append(current_id)
elif card_supertypes and card_supertypes == current_supertypes:
if current_cmc == card_cmc and current_colors == card_colors:
list_similar_cards.append(current_id)
elif card_subtypes[0] in current_subtypes:
if current_cmc == card_cmc:
list_similar_cards.append(current_id)
# continue
elif current_colors == card_colors:
list_similar_cards.append(current_id)
# continue
# elif current_colors == card_colors:
# list_similar_cards.append(current_id)
#
else:
if card_subtypes == current_subtypes and current_colors == card_colors and current_cmc == card_cmc and card_supertypes == current_supertypes:
# if subtypes == card_subtypes and colors == card_colors and cmc == card_cmc:
list_similar_cards.append(current_id)
elif 'Artifact' in card_type and 'Artifact' in current_type and current_id not in list_similar_cards and 'Creature' not in current_type and 'Creature' not in card_type:
if not_enough:
if current_cmc == card_cmc and current_identity == card_identity:
list_similar_cards.append(current_id)
elif current_colors == card_colors:
list_similar_cards.append(current_id)
else:
if current_identity == card_identity and \
current_cmc == card_cmc and \
(card_subtypes and current_subtypes == card_subtypes):
list_similar_cards.append(current_id)
elif current_identity == card_identity and \
current_cmc == card_cmc and \
current_subtypes == card_subtypes:
list_similar_cards.append(current_id)
return list_similar_cards
def similar_cards(card_name, not_enough=False, last_chance=False):
# TODO: fix similar cards for split cards cause converted CMC is combined and not per card
modern_atomic = mongo.db.modern_atomic
cards = mongo.db.cards
pprint("working in similar_cards_2")
# pprint(card_name)
card_modern_atomic = modern_atomic.find_one({'_id': card_name})
# if not force:
try:
card_from_cards = cards.find_one({'_id': card_name, "similar_cards": {"$exists": True, "$ne": None}})
similar_cards = card_from_cards['similar_cards']
len_sim_cards = len(similar_cards)
if len_sim_cards >= 3:
print("similar_cards from db")
return similar_cards
except TypeError:
print("Card does not have similar_cards")
list_similar_cards = []
try:
card_id_atomic = card_modern_atomic['_id']
except TypeError:
print("Importing latest set from ModernAtomic to Db")
from_atomic_to_db()
card_name_atomic = card_modern_atomic['name']
card_names_atomic = []
if '//' in card_name_atomic:
# card_names_atomic = []
split_card_name_atomic = card_name_atomic.split('//', 1)
for name in split_card_name_atomic:
card_names_atomic.append(name.lstrip().rstrip())
else:
card_names_atomic = ['icara e super', ' lud']
# card_info = cards.find_one({'_id': card_name})
# card_name = card_info['name']
card_colors = card_modern_atomic['colors']
card_type = card_modern_atomic['type']
card_type_s = card_modern_atomic['types']
card_supertypes = card_modern_atomic['supertypes']
# if 'Planeswalker' not in card_type:
# pprint("not a Planeswalker")
# return "1"
pprint(card_type)
card_subtypes = card_modern_atomic['subtypes']
card_identity = card_modern_atomic['colorIdentity']
card_cmc = card_modern_atomic['convertedManaCost'] if 'convertedManaCost' in card_modern_atomic.keys() else ""
if 'Planeswalker' in card_type:
if not_enough:
# similar_cards = modern_atomic.find(
# {"types": card_type_s, "colorIdentity": card_identity, "subtypes": card_subtypes}, limit=10)
similar_cards = modern_atomic.aggregate([
{"$match": {"_id": {"$ne": card_id_atomic}, "types": card_type_s,
"subtypes": card_subtypes}},
{"$sample": {"size": 10}}])
elif last_chance:
similar_cards = modern_atomic.aggregate([
{"$match": {"_id": {"$ne": card_id_atomic}, "types": card_type_s,
"colorIdentity": card_identity,
# "convertedManaCost": card_cmc,
}},
{"$sample": {"size": 10}}])
else:
# similar_cards = modern_atomic.find(
# {"types": card_type_s, "subtypes": card_subtypes}, limit=10)
similar_cards = modern_atomic.aggregate([
{"$match": {"$and": [{"_id": {"$ne": card_id_atomic}},
{"_id": {"$nin": card_names_atomic}}], "types": card_type_s,
"subtypes": card_subtypes,
"colorIdentity": card_identity}},
{"$sample": {"size": 10}}])
list_similar_cards = set([card['_id'] for card in similar_cards])
# if current_identity == card_identity:
# list_similar_cards.append(current_name)
elif 'Tribal' in card_type:
similar_cards = modern_atomic.find({"_id": {"$ne": card_id_atomic}, "types": {"$in": ["Tribal"]},
'subtypes': card_subtypes})
list_similar_cards = set([card['_id'] for card in similar_cards])
elif 'Land' in card_type:
lands = ['Swamp', 'Mountain', 'Island', 'Plains', 'Forest', 'Wastes',
'Snow-Covered Swamp', 'Snow-Covered Mountain', 'Snow-Covered Island', 'Snow-Covered Plains',
'Snow-Covered Forest', card_id_atomic]
if not_enough or last_chance:
similar_cards = modern_atomic.aggregate([
{"$match": {
"$and": [
{"$and": [{"_id": {"$nin": lands}},
{"_id": {"$ne": card_id_atomic}},
{"_id": {"$nin": card_names_atomic}}]},
{"types": card_type_s,
"colorIdentity": card_identity}
]
}},
{"$sample": {"size": 15}}])
# similar_cards = modern_atomic.find(
# {"type": card_type, "colorIdentity": card_identity}, limit=15)
else:
similar_cards = modern_atomic.aggregate([
{"$match": {"$and": [
{"_id": {"$ne": card_id_atomic}},
{"_id": {"$nin": card_names_atomic}}],
"$or": [{"type": card_type, "colorIdentity": card_identity},
{"$and": [{"type": card_type, 'subtypes': card_subtypes},
{"type": card_type, 'subtypes': {"$ne": []}}]}]}},
{"$sample": {"size": 15}}])
list_similar_cards = set([card['_id'] for card in similar_cards])
elif 'Creature' in card_type or 'Artifact' in card_type:
list_similar_cards = find_all(card_type, card_colors, card_subtypes, card_identity, card_cmc,
not_enough, card_name, card_supertypes, last_chance, card_names_atomic)
#
elif 'Sorcery' in card_type or 'Instant' in card_type:
pprint("instant is here")
if not_enough or last_chance:
similar_cards = modern_atomic.aggregate([
# {"$match": {"_id": {"$nin": [card_id_atomic, card_name_atomic]}, "type": card_type,
# "colors": card_colors}},
{"$match": {"$and": [{"_id": {"$ne": card_id_atomic}},
{"_id": {"$nin": card_names_atomic}}],
"type": card_type, "colors": card_colors}},
{"$sample": {"size": 100}}])
# if current_colors == card_colors:
# list_similar_cards.append(current_name)
else:
# print(card_type)
similar_cards = modern_atomic.aggregate(
[
{"$match":
{"$and": [{"_id": {"$ne": card_id_atomic}},
{"_id": {"$nin": card_names_atomic}}],