import json
import os
from pathlib import Path
import urllib.request
def Get_Value(key,DB):
if key in DB:
return DB[key]
else:
return ''
def Descargar_Imagen(p_imagen,p_name):
if p_imagen != '':
local_dir = 'imagenes'
local_file = local_dir+'/BGG.tn'+p_name+'.jpg'
if os.path.exists(local_dir) and os.path.isdir(local_dir):
data = 1
else:
os.mkdir(local_dir)
if os.path.exists(local_file) and os.path.isfile(local_file):
data = 2
else:
urllib.request.urlretrieve(p_imagen, local_file)
def Str_Imagen(p_BGGID):
return '[[https://boardgamegeek.com/boardgame/'+ str(p_BGGID)+' | %width=50pct%Path:/uploads/BGG/thumbnails/BGG.tn'+str(p_BGGID)+'.jpg ]]'
def Str_Players(data):
l_min = Get_Value('MinPlayers',data)
l_max = Get_Value('MaxPlayers',data)
if l_min != '':
return '[-- 👥 : '+str(l_min) + '-'+ str(l_max)+' --]'+os.linesep
else:
return ''
def Str_PLayingTime(data):
if 'PlayingTime' in data:
l_time = int(data['PlayingTime'] / 60)
return '[-- ⏳: '+ str(l_time)+' min --]'+os.linesep
else:
return ''
def Str_Peso(data):
l_time = Get_Value('BGGWeight',data)
if l_time != '':
return '[-- 💪:'+ str(l_time)+' --]'+os.linesep
else:
return ''
def Str_Partidas(full_data, BGGID, ID):
t_partidas = full_data["Plays"]
ax = 0
lista = []
for partidas in t_partidas:
if 'GameID' in partidas:
if partidas["GameID"] == ID:
lista.append(Add_Partida(partidas))
ax += 1
if ax > 0:
Print_Partidas(BGGID,lista)
return '[-- 🎲: [[BGG.Plays'+str(BGGID)+' | '+str(ax)+ ']] --]'
else:
return ''
def Add_Partida(l_partida):
fecha = l_partida["Date"]
l_comentario = ''
l_ignore = ''
if "IgnoreWinStats" in l_partida:
l_ignore = "(:head colspan=2:) ❌ '''Partida Ignorada''' ❌"+os.linesep
if "Comments" in l_partida:
l_comentario = "-->''"+l_partida["Comments"]+"''"+os.linesep
l_tiempo = 'N/A'
l_BGGID = ''
if "Duration" in l_partida:
l_tiempo = '⏳ : '+str(int(l_partida["Duration"]/60)) + ' min ' +os.linesep
else:
l_tiempo = ''
if "BGGID" in l_partida:
l_BGGID = '🔗 : [[https://boardgamegeek.com/play/details/'+str(l_partida["BGGID"])+' | BGG ]]' + os.linesep
linea = ''
sitio = ''
for lugar in lugares:
if "LocationID" in l_partida:
if lugar["ID"] == l_partida["LocationID"]:
sitio = lugar["Name"]
break
if "PlayerScores" in l_partida:
for scores in l_partida["PlayerScores"]:
if "Victory" in scores:
ganador_ini = "%green%'''"
ganador_fin = "''' 🏆"
else:
ganador_ini = ''
ganador_fin = ''
for persona in jugadores:
if persona["ID"] == scores["PlayerID"]:
persona_name = '# '+ganador_ini+persona["Name"]+ganador_fin
break
if "TotalScore" in scores:
puntos = scores["TotalScore"]
else:
puntos = 0
linea = linea + persona_name + ' : ' + str(puntos) + os.linesep
linea = '(:title Partidas a '+ l_partida["GameTitle"]+':)'+os.linesep + '%right%[[BGG.BoardRecord | ↩️ ]]' + os.linesep + \
'(:table width=100% align=center :)' + os.linesep + l_ignore + \
'(:cellnr width=30%:) 📅 : '+ fecha.replace('00:00','') + os.linesep + \
'📍 : ' + sitio + os.linesep + \
l_tiempo + l_BGGID + '(:cell:)' + os.linesep + linea + '(:tableend:)' + os.linesep + l_comentario + '----'
return linea
def Print_Partidas(p_BGGID, p_lista):
fplays = open('BGG.Plays'+str(p_BGGID),'w',encoding='utf-8')
fplays.write("version=pmwiki-2.4.2 ordered=1 urlencoded=1"+os.linesep)
fplays.write("agent=Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:137.0) Gecko/20100101 Firefox/137.0"+os.linesep)
fplays.write("charset=UTF-8"+os.linesep)
texto = ''
for key in p_lista:
texto = texto + key.replace(os.linesep,r'%0a')
fplays.write('text='+texto)
fplays.close()
def List_Games(full_data, data):
ax = 0
limite = 4
porcen = str(int(100/(limite*2)))+'%'
l_celda_00 = ''
l_celda_01 = '(:cellnr valign=top width='+porcen+':)'
l_celda_02 = '(:cell valign=top width='+porcen+':)'
fout.write('(:table witdh=100% align=center:)'+os.linesep)
for game in data:
l_imagen = ''
if 'Imagen' in game:
l_imagen = game['Imagen']
Descargar_Imagen(l_imagen,str(game["BGGID"]))
l_imagen = Str_Imagen(game["BGGID"])
else:
l_imagen = Get_Value('Thumbnail', game)
Descargar_Imagen(l_imagen,str(game["BGGID"]))
l_imagen = Str_Imagen(game["BGGID"])
ax += 1
if ax > limite:
l_celda_00 = l_celda_01
ax = 1
else:
if ax == 1:
l_celda_00 = l_celda_01
else:
l_celda_00 = l_celda_02
l_partidas = Str_Partidas(full_data,game["BGGID"],game["ID"])+os.linesep
fout.write(l_celda_00+l_imagen+os.linesep)
fout.write(l_celda_02+"[-- '''"+game["Title"]+"'''--]"+os.linesep+Str_Players(game)+ \
Str_PLayingTime(game)+Str_Peso(game)+l_partidas)
fout.write('(:tableend:)'+os.linesep)
# Abre y carga el archivo JSON
fout = open('salida.txt', 'w', encoding='utf-8')
with open('BoardRecord.json', 'r', encoding='utf-8') as file:
data = json.load(file)
juegos = data["Games"]
juegos.sort( key=lambda x: x["Title"])
jugadores = data["Players"]
lugares = data["Locations"]
List_Games( data, juegos)
fout.close()