mudancas
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
plugins {
|
plugins {
|
||||||
id("java")
|
id("java")
|
||||||
|
application
|
||||||
}
|
}
|
||||||
|
|
||||||
group = "org.example"
|
group = "org.example"
|
||||||
@@ -20,4 +21,8 @@ dependencies {
|
|||||||
|
|
||||||
tasks.test {
|
tasks.test {
|
||||||
useJUnitPlatform()
|
useJUnitPlatform()
|
||||||
|
}
|
||||||
|
|
||||||
|
application {
|
||||||
|
mainClass.set("org.example.Main")
|
||||||
}
|
}
|
||||||
@@ -13,7 +13,7 @@ import org.jsoup.Connection;
|
|||||||
import org.jsoup.Jsoup;
|
import org.jsoup.Jsoup;
|
||||||
|
|
||||||
import java.io.FileInputStream;
|
import java.io.FileInputStream;
|
||||||
import java.util.ArrayList;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
@@ -39,9 +39,62 @@ public class Main {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
int[] idsEscaloes = {1, 3, 2};
|
|
||||||
String[] nomesEscaloes = {"seniores", "juniores", "feminino"};
|
|
||||||
Gson gson = new Gson();
|
Gson gson = new Gson();
|
||||||
|
Map<Integer, Club> clubesMap = new HashMap<>();
|
||||||
|
|
||||||
|
// 1. Obter todos os Clubes e seus meta-dados iniciais
|
||||||
|
System.out.println("\n--- A EXTRAIR DADOS DE TODOS OS CLUBES ---");
|
||||||
|
try {
|
||||||
|
String urlTodosClubes = "https://api.afavcd.pt/teams";
|
||||||
|
String jsonTodosClubes = Jsoup.connect(urlTodosClubes)
|
||||||
|
.ignoreContentType(true)
|
||||||
|
.userAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64)")
|
||||||
|
.header("Referer", "https://www.afavcd.pt/")
|
||||||
|
.header("Origin", "https://www.afavcd.pt")
|
||||||
|
.method(Connection.Method.GET)
|
||||||
|
.execute()
|
||||||
|
.body();
|
||||||
|
|
||||||
|
List<Map<String, Object>> dadosClubes = gson.fromJson(jsonTodosClubes,
|
||||||
|
new TypeToken<List<Map<String, Object>>>() {
|
||||||
|
}.getType());
|
||||||
|
|
||||||
|
for (Map<String, Object> c : dadosClubes) {
|
||||||
|
if (c.get("teamID") != null) {
|
||||||
|
int clubId = (int) Double.parseDouble(c.get("teamID").toString());
|
||||||
|
Club club = new Club();
|
||||||
|
club.setId(clubId);
|
||||||
|
if (c.get("name") != null)
|
||||||
|
club.setName(c.get("name").toString());
|
||||||
|
if (c.get("logoURL") != null)
|
||||||
|
club.setImageUrl(c.get("logoURL").toString());
|
||||||
|
if (c.get("president") != null)
|
||||||
|
club.setPresident(c.get("president").toString());
|
||||||
|
if (c.get("address") != null)
|
||||||
|
club.setAddress(c.get("address").toString());
|
||||||
|
if (c.get("field") != null)
|
||||||
|
club.setStadium(c.get("field").toString());
|
||||||
|
|
||||||
|
if (c.get("dateFundation") != null) {
|
||||||
|
try {
|
||||||
|
club.setFoundationYear((int) Double.parseDouble(c.get("dateFundation").toString()));
|
||||||
|
} catch (NumberFormatException e) {
|
||||||
|
club.setFoundationYear(0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
clubesMap.put(clubId, club);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
System.out.println("Encontrados " + clubesMap.size() + " clubes.");
|
||||||
|
} catch (Exception e) {
|
||||||
|
System.err.println("Erro ao obter lista de clubes.");
|
||||||
|
e.printStackTrace();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2. Obter escalões
|
||||||
|
int[] idsEscaloes = { 1, 3, 2 };
|
||||||
|
String[] nomesEscaloes = { "seniores", "juniores", "feminino" };
|
||||||
|
|
||||||
for (int i = 0; i < idsEscaloes.length; i++) {
|
for (int i = 0; i < idsEscaloes.length; i++) {
|
||||||
int idAtual = idsEscaloes[i];
|
int idAtual = idsEscaloes[i];
|
||||||
@@ -63,19 +116,22 @@ public class Main {
|
|||||||
.execute()
|
.execute()
|
||||||
.body();
|
.body();
|
||||||
|
|
||||||
List<Map<String, Object>> dadosClubes = gson.fromJson(jsonResponse, new TypeToken<List<Map<String, Object>>>() {}.getType());
|
List<Map<String, Object>> equipasEscalao = gson.fromJson(jsonResponse,
|
||||||
|
new TypeToken<List<Map<String, Object>>>() {
|
||||||
|
}.getType());
|
||||||
|
|
||||||
for (Map<String, Object> dadoClube : dadosClubes) {
|
for (Map<String, Object> equipa : equipasEscalao) {
|
||||||
int clubId = -1;
|
int clubId = -1;
|
||||||
|
|
||||||
// CORREÇÃO AQUI: Tratar o número como Double primeiro para evitar NumberFormatException
|
if (equipa.get("teamID") != null) {
|
||||||
if (dadoClube.get("teamID") != null) {
|
clubId = (int) Double.parseDouble(equipa.get("teamID").toString());
|
||||||
clubId = (int) Double.parseDouble(dadoClube.get("teamID").toString());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (clubId == -1) continue; // Salta se o ID for inválido
|
if (clubId == -1 || !clubesMap.containsKey(clubId))
|
||||||
|
continue;
|
||||||
|
|
||||||
String urlJogadores = "https://api.afavcd.pt/teams/" + clubId + "/modality/" + idAtual + "/season/33";
|
String urlJogadores = "https://api.afavcd.pt/teams/" + clubId + "/modality/" + idAtual
|
||||||
|
+ "/season/33";
|
||||||
|
|
||||||
try {
|
try {
|
||||||
String jsonJogadores = Jsoup.connect(urlJogadores)
|
String jsonJogadores = Jsoup.connect(urlJogadores)
|
||||||
@@ -87,36 +143,50 @@ public class Main {
|
|||||||
.execute()
|
.execute()
|
||||||
.body();
|
.body();
|
||||||
|
|
||||||
List<Map<String, Object>> dadosJogadores = gson.fromJson(jsonJogadores, new TypeToken<List<Map<String, Object>>>() {}.getType());
|
List<Map<String, Object>> dadosJogadores = gson.fromJson(jsonJogadores,
|
||||||
ArrayList<Player> listaJogadoresDoClube = new ArrayList<>();
|
new TypeToken<List<Map<String, Object>>>() {
|
||||||
|
}.getType());
|
||||||
|
|
||||||
|
Club clubToUpdate = clubesMap.get(clubId);
|
||||||
|
|
||||||
for (Map<String, Object> atleta : dadosJogadores) {
|
for (Map<String, Object> atleta : dadosJogadores) {
|
||||||
if (atleta.get("fullName") != null) {
|
if (atleta.get("fullName") != null) {
|
||||||
Player p = new Player();
|
Player p = new Player();
|
||||||
|
|
||||||
|
if (atleta.get("playerID") != null) {
|
||||||
|
p.setId((int) Double.parseDouble(atleta.get("playerID").toString()));
|
||||||
|
}
|
||||||
|
|
||||||
p.setName((String) atleta.get("fullName"));
|
p.setName((String) atleta.get("fullName"));
|
||||||
|
|
||||||
String foto = (String) atleta.get("photoURL");
|
String foto = null;
|
||||||
|
if (atleta.get("photoURL") != null) {
|
||||||
|
foto = (String) atleta.get("photoURL");
|
||||||
|
} else if (atleta.get("photoURl") != null) {
|
||||||
|
foto = (String) atleta.get("photoURl");
|
||||||
|
}
|
||||||
|
|
||||||
if (foto != null && !foto.startsWith("http")) {
|
if (foto != null && !foto.startsWith("http")) {
|
||||||
foto = "https://api.afavcd.pt" + foto;
|
foto = "https://api.afavcd.pt" + foto;
|
||||||
}
|
}
|
||||||
p.setPhotoUrl(foto);
|
p.setPhotoUrl(foto);
|
||||||
|
|
||||||
if (atleta.get("birth") != null) p.setBirthDate((String) atleta.get("birth"));
|
if (atleta.get("birth") != null) {
|
||||||
if (atleta.get("naturalness") != null) p.setNaturalness((String) atleta.get("naturalness"));
|
String date = (String) atleta.get("birth");
|
||||||
|
if (date.contains("T")) {
|
||||||
|
date = date.split("T")[0];
|
||||||
|
}
|
||||||
|
p.setBirthDate(date);
|
||||||
|
}
|
||||||
|
if (atleta.get("naturalness") != null)
|
||||||
|
p.setNaturalness((String) atleta.get("naturalness"));
|
||||||
|
|
||||||
listaJogadoresDoClube.add(p);
|
clubToUpdate.addJogador(nomeAtual, p);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
DatabaseReference ref = FirebaseDatabase.getInstance()
|
System.out.println("SUCESSO: Extraiu " + dadosJogadores.size() + " jogadores para o clube ID "
|
||||||
.getReference("clubes")
|
+ clubId + " (" + nomeAtual + ").");
|
||||||
.child(String.valueOf(clubId - 1)) // Mantive o teu -1, assume-se que é a tua lógica de IDs
|
|
||||||
.child("listaDeJogadores");
|
|
||||||
|
|
||||||
// CORREÇÃO AQUI: Gravar a listaJogadoresDoClube e não a listaParaFirebase que estava vazia
|
|
||||||
ref.setValueAsync(listaJogadoresDoClube);
|
|
||||||
|
|
||||||
System.out.println("SUCESSO: Jogadores do Clube ID " + clubId + " (" + nomeAtual.toUpperCase() + ") enviados para o Firebase!");
|
|
||||||
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
System.err.println("Erro ao processar jogadores do clube ID: " + clubId);
|
System.err.println("Erro ao processar jogadores do clube ID: " + clubId);
|
||||||
@@ -125,7 +195,22 @@ public class Main {
|
|||||||
}
|
}
|
||||||
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
System.err.println("Erro a processar o escalão " + nomeAtual + ": HTTP error fetching URL.");
|
System.err.println("Erro a processar o escalão " + nomeAtual);
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println("\n========================================");
|
||||||
|
System.out.println("A ENVIAR DADOS PARA O FIREBASE...");
|
||||||
|
System.out.println("========================================");
|
||||||
|
|
||||||
|
DatabaseReference refClubes = FirebaseDatabase.getInstance().getReference("clubes");
|
||||||
|
for (Club club : clubesMap.values()) {
|
||||||
|
try {
|
||||||
|
refClubes.child(String.valueOf(club.getId())).setValueAsync(club);
|
||||||
|
System.out.println("Clube ID " + club.getId() + " - " + club.getName() + " enviado para o Firebase.");
|
||||||
|
} catch (Exception e) {
|
||||||
|
System.err.println("Erro a enviar clube ID " + club.getId());
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,7 +2,9 @@ package org.example.models;
|
|||||||
|
|
||||||
import com.google.firebase.database.PropertyName;
|
import com.google.firebase.database.PropertyName;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
public class Club {
|
public class Club {
|
||||||
@PropertyName("id")
|
@PropertyName("id")
|
||||||
@@ -17,7 +19,7 @@ public class Club {
|
|||||||
@PropertyName("campo")
|
@PropertyName("campo")
|
||||||
private String stadium;
|
private String stadium;
|
||||||
|
|
||||||
@PropertyName("ano fundacao")
|
@PropertyName("ano_fundacao")
|
||||||
private int foundationYear;
|
private int foundationYear;
|
||||||
|
|
||||||
@PropertyName("morada")
|
@PropertyName("morada")
|
||||||
@@ -27,11 +29,11 @@ public class Club {
|
|||||||
private String president;
|
private String president;
|
||||||
|
|
||||||
@PropertyName("jogadores")
|
@PropertyName("jogadores")
|
||||||
private ArrayList<Player> players;
|
private Map<String, List<Player>> jogadores;
|
||||||
|
|
||||||
public Club() {
|
public Club() {
|
||||||
// Default constructor for Firebase
|
// Default constructor for Firebase
|
||||||
players = new ArrayList<>();
|
jogadores = new HashMap<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public Club(int id, String name, String imageUrl, String stadium, int foundationYear, String address,
|
public Club(int id, String name, String imageUrl, String stadium, int foundationYear, String address,
|
||||||
@@ -43,7 +45,7 @@ public class Club {
|
|||||||
this.foundationYear = foundationYear;
|
this.foundationYear = foundationYear;
|
||||||
this.address = address;
|
this.address = address;
|
||||||
this.president = president;
|
this.president = president;
|
||||||
this.players = new ArrayList<>();
|
this.jogadores = new HashMap<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
@PropertyName("id")
|
@PropertyName("id")
|
||||||
@@ -86,12 +88,12 @@ public class Club {
|
|||||||
this.stadium = stadium;
|
this.stadium = stadium;
|
||||||
}
|
}
|
||||||
|
|
||||||
@PropertyName("ano fundacao")
|
@PropertyName("ano_fundacao")
|
||||||
public int getFoundationYear() {
|
public int getFoundationYear() {
|
||||||
return foundationYear;
|
return foundationYear;
|
||||||
}
|
}
|
||||||
|
|
||||||
@PropertyName("ano fundacao")
|
@PropertyName("ano_fundacao")
|
||||||
public void setFoundationYear(int foundationYear) {
|
public void setFoundationYear(int foundationYear) {
|
||||||
this.foundationYear = foundationYear;
|
this.foundationYear = foundationYear;
|
||||||
}
|
}
|
||||||
@@ -117,18 +119,22 @@ public class Club {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@PropertyName("jogadores")
|
@PropertyName("jogadores")
|
||||||
public ArrayList<Player> getPlayersMap() {
|
public Map<String, List<Player>> getJogadores() {
|
||||||
return players;
|
return jogadores;
|
||||||
}
|
}
|
||||||
|
|
||||||
@PropertyName("jogadores")
|
@PropertyName("jogadores")
|
||||||
public void setPlayersMap(ArrayList<Player> players) {
|
public void setJogadores(Map<String, List<Player>> jogadores) {
|
||||||
this.players = players;
|
this.jogadores = jogadores;
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<Player> getPlayersList() {
|
public void addJogador(String escalao, Player player) {
|
||||||
if (players == null)
|
if (this.jogadores == null) {
|
||||||
return new ArrayList<>();
|
this.jogadores = new HashMap<>();
|
||||||
return new ArrayList<>(players);
|
}
|
||||||
|
if (!this.jogadores.containsKey(escalao)) {
|
||||||
|
this.jogadores.put(escalao, new ArrayList<>());
|
||||||
|
}
|
||||||
|
this.jogadores.get(escalao).add(player);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user