BGG Android

  1. import json
  2. import os
  3. from pathlib import Path
  4. import urllib.request
  5.  
  6. def Get_Value(key,DB):
  7.     if key in DB:
  8.         return DB[key]
  9.     else:
  10.         return ''
  11.  
  12. def Descargar_Imagen(p_imagen,p_name):
  13.     if p_imagen != '':
  14.         local_dir = 'imagenes'
  15.         local_file = local_dir+'/BGG.tn'+p_name+'.jpg'
  16.  
  17.         if os.path.exists(local_dir) and os.path.isdir(local_dir):
  18.             data = 1
  19.         else:
  20.             os.mkdir(local_dir)
  21.  
  22.         if os.path.exists(local_file) and os.path.isfile(local_file):        
  23.             data = 2
  24.         else:
  25.             urllib.request.urlretrieve(p_imagen, local_file)
  26.  
  27. def Str_Imagen(p_BGGID):
  28.     return '[[https://boardgamegeek.com/boardgame/'+ str(p_BGGID)+' | %width=50pct%Path:/uploads/BGG/thumbnails/BGG.tn'+str(p_BGGID)+'.jpg ]]'
  29.  
  30. def Str_Players(data):
  31.     l_min = Get_Value('MinPlayers',data)
  32.     l_max = Get_Value('MaxPlayers',data)
  33.  
  34.     if l_min != '':
  35.         return '[-- 👥 : '+str(l_min) + '-'+ str(l_max)+' --]'+os.linesep
  36.     else:
  37.         return ''
  38.  
  39. def Str_PLayingTime(data):
  40.     if 'PlayingTime' in data:
  41.         l_time = int(data['PlayingTime'] / 60)
  42.         return '[-- ⏳: '+ str(l_time)+' min  --]'+os.linesep
  43.     else:
  44.         return ''
  45.  
  46. def Str_Peso(data):
  47.     l_time = Get_Value('BGGWeight',data)
  48.     if l_time != '':
  49.         return '[-- 💪:'+ str(l_time)+' --]'+os.linesep
  50.     else:
  51.         return ''    
  52.  
  53. def Str_Partidas(full_data, BGGID, ID):
  54.     t_partidas = full_data["Plays"]
  55.     ax = 0
  56.     lista = []
  57.     for partidas in t_partidas:
  58.         if 'GameID' in partidas:
  59.             if partidas["GameID"] == ID:                
  60.                 lista.append(Add_Partida(partidas))
  61.                 ax += 1
  62.  
  63.     if ax > 0:
  64.         Print_Partidas(BGGID,lista)
  65.         return '[-- 🎲: [[BGG.Plays'+str(BGGID)+' | '+str(ax)+ ']] --]'
  66.     else:
  67.         return ''
  68.  
  69. def Add_Partida(l_partida):
  70.     fecha      = l_partida["Date"]
  71.     l_comentario = ''
  72.     l_ignore = ''
  73.     if "IgnoreWinStats" in l_partida:
  74.         l_ignore = "(:head colspan=2:) ❌ '''Partida Ignorada''' ❌"+os.linesep
  75.     if "Comments" in l_partida:
  76.         l_comentario = "-->''"+l_partida["Comments"]+"''"+os.linesep
  77.  
  78.     l_tiempo = 'N/A'
  79.     l_BGGID = ''
  80.     if "Duration" in l_partida:
  81.         l_tiempo = '⏳ : '+str(int(l_partida["Duration"]/60)) + ' min ' +os.linesep
  82.     else:
  83.         l_tiempo = ''
  84.     if "BGGID" in l_partida:
  85.         l_BGGID = '🔗 : [[https://boardgamegeek.com/play/details/'+str(l_partida["BGGID"])+' | BGG ]]' + os.linesep    
  86.     linea = ''
  87.     sitio = ''
  88.     for lugar in lugares:
  89.         if "LocationID" in l_partida:
  90.             if lugar["ID"] == l_partida["LocationID"]:
  91.                 sitio = lugar["Name"]
  92.                 break
  93.  
  94.     if "PlayerScores" in l_partida:
  95.         for scores in l_partida["PlayerScores"]:
  96.             if "Victory" in scores:
  97.                 ganador_ini = "%green%'''"
  98.                 ganador_fin = "''' 🏆"
  99.             else:
  100.                 ganador_ini = ''
  101.                 ganador_fin = ''
  102.             for persona in jugadores:
  103.                 if persona["ID"] == scores["PlayerID"]:
  104.                     persona_name = '# '+ganador_ini+persona["Name"]+ganador_fin
  105.                     break
  106.             if "TotalScore" in scores:
  107.                 puntos = scores["TotalScore"]
  108.             else:
  109.                 puntos = 0
  110.  
  111.             linea = linea + persona_name + ' : ' + str(puntos) + os.linesep
  112.  
  113.  
  114.     linea = '(:title Partidas a '+ l_partida["GameTitle"]+':)'+os.linesep + '%right%[[BGG.BoardRecord | ↩️ ]]' + os.linesep + \
  115.             '(:table width=100% align=center :)' +  os.linesep + l_ignore + \
  116.             '(:cellnr width=30%:) 📅 : '+ fecha.replace('00:00','') + os.linesep + \
  117.             '📍 : '  +  sitio + os.linesep + \
  118.              l_tiempo + l_BGGID + '(:cell:)' + os.linesep + linea + '(:tableend:)' + os.linesep + l_comentario + '----'
  119.     return linea
  120.  
  121. def Print_Partidas(p_BGGID, p_lista):
  122.     fplays = open('BGG.Plays'+str(p_BGGID),'w',encoding='utf-8')
  123.     fplays.write("version=pmwiki-2.4.2 ordered=1 urlencoded=1"+os.linesep)
  124.     fplays.write("agent=Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:137.0) Gecko/20100101 Firefox/137.0"+os.linesep)
  125.     fplays.write("charset=UTF-8"+os.linesep)  
  126.     texto = ''
  127.     for key in p_lista:
  128.         texto = texto + key.replace(os.linesep,r'%0a')
  129.  
  130.     fplays.write('text='+texto)
  131.     fplays.close()
  132.  
  133.  
  134. def List_Games(full_data, data):
  135.     ax = 0
  136.  
  137.     limite = 4
  138.     porcen = str(int(100/(limite*2)))+'%'
  139.     l_celda_00 = ''
  140.     l_celda_01 = '(:cellnr valign=top width='+porcen+':)'
  141.     l_celda_02 = '(:cell valign=top  width='+porcen+':)'
  142.  
  143.  
  144.     fout.write('(:table witdh=100% align=center:)'+os.linesep)
  145.     for game in data:
  146.         l_imagen = ''
  147.         if 'Imagen' in game:
  148.             l_imagen = game['Imagen']
  149.             Descargar_Imagen(l_imagen,str(game["BGGID"]))
  150.             l_imagen = Str_Imagen(game["BGGID"])
  151.         else:
  152.             l_imagen = Get_Value('Thumbnail', game)
  153.             Descargar_Imagen(l_imagen,str(game["BGGID"]))
  154.             l_imagen = Str_Imagen(game["BGGID"])
  155.  
  156.         ax += 1
  157.         if ax > limite:
  158.             l_celda_00 = l_celda_01
  159.             ax = 1
  160.         else:
  161.             if ax == 1:
  162.                 l_celda_00 = l_celda_01
  163.             else:
  164.                 l_celda_00 = l_celda_02
  165.         l_partidas = Str_Partidas(full_data,game["BGGID"],game["ID"])+os.linesep
  166.         fout.write(l_celda_00+l_imagen+os.linesep)
  167.         fout.write(l_celda_02+"[-- '''"+game["Title"]+"'''--]"+os.linesep+Str_Players(game)+ \
  168.                    Str_PLayingTime(game)+Str_Peso(game)+l_partidas)        
  169.  
  170.     fout.write('(:tableend:)'+os.linesep)
  171.  
  172.  
  173. # Abre y carga el archivo JSON
  174.  
  175. fout = open('salida.txt', 'w', encoding='utf-8')
  176. with open('BoardRecord.json', 'r', encoding='utf-8') as file:
  177.  
  178.  
  179.     data = json.load(file)
  180.     juegos = data["Games"]
  181.     juegos.sort( key=lambda x: x["Title"])
  182.     jugadores = data["Players"]
  183.     lugares   = data["Locations"]
  184.     List_Games( data, juegos)
  185.  
  186. fout.close()  
  187.