adicao do campo taça no fireabse
This commit is contained in:
153
src/main/java/org/example/CupScraper.java
Normal file
153
src/main/java/org/example/CupScraper.java
Normal file
@@ -0,0 +1,153 @@
|
|||||||
|
package org.example;
|
||||||
|
|
||||||
|
import com.google.auth.oauth2.GoogleCredentials;
|
||||||
|
import com.google.firebase.FirebaseApp;
|
||||||
|
import com.google.firebase.FirebaseOptions;
|
||||||
|
import com.google.firebase.database.DatabaseReference;
|
||||||
|
import com.google.firebase.database.FirebaseDatabase;
|
||||||
|
import com.google.gson.Gson;
|
||||||
|
import com.google.gson.reflect.TypeToken;
|
||||||
|
import org.jsoup.Connection;
|
||||||
|
import org.jsoup.Jsoup;
|
||||||
|
|
||||||
|
import java.io.FileInputStream;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
public class CupScraper {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
String caminhoChave = "service-account.json";
|
||||||
|
String urlDatabase = "https://vdcscore-default-rtdb.firebaseio.com/";
|
||||||
|
|
||||||
|
try {
|
||||||
|
System.out.println("--- A CONECTAR AO FIREBASE ---");
|
||||||
|
if (FirebaseApp.getApps().isEmpty()) {
|
||||||
|
FileInputStream serviceAccount = new FileInputStream(caminhoChave);
|
||||||
|
FirebaseOptions options = FirebaseOptions.builder()
|
||||||
|
.setCredentials(GoogleCredentials.fromStream(serviceAccount))
|
||||||
|
.setDatabaseUrl(urlDatabase)
|
||||||
|
.build();
|
||||||
|
FirebaseApp.initializeApp(options);
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
System.err.println("ERRO FIREBASE!");
|
||||||
|
e.printStackTrace();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
Gson gson = new Gson();
|
||||||
|
String[] urls = {
|
||||||
|
"https://api.afavcd.pt/cup/seasons/33/modality/1",
|
||||||
|
"https://api.afavcd.pt/cup/seasons/33/modality/3"
|
||||||
|
};
|
||||||
|
String[] escaloes = { "seniores", "juniores" };
|
||||||
|
|
||||||
|
DatabaseReference refTaca = FirebaseDatabase.getInstance().getReference("taça");
|
||||||
|
|
||||||
|
for (int i = 0; i < urls.length; i++) {
|
||||||
|
String url = urls[i];
|
||||||
|
String escalao = escaloes[i];
|
||||||
|
System.out.println("\n=== A PROCESSAR TAÇA PARA: " + escalao.toUpperCase() + " ===");
|
||||||
|
|
||||||
|
try {
|
||||||
|
String json = Jsoup.connect(url)
|
||||||
|
.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();
|
||||||
|
|
||||||
|
Map<String, Object> root = gson.fromJson(json, new TypeToken<Map<String, Object>>() {}.getType());
|
||||||
|
|
||||||
|
if (!root.containsKey("games")) {
|
||||||
|
System.out.println("Sem jogos para " + escalao);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
Map<String, List<Map<String, Object>>> phases = (Map<String, List<Map<String, Object>>>) root.get("games");
|
||||||
|
|
||||||
|
for (Map.Entry<String, List<Map<String, Object>>> entry : phases.entrySet()) {
|
||||||
|
String phaseKeyRaw = entry.getKey();
|
||||||
|
// Firebase friendly phase name
|
||||||
|
String phaseKey = phaseKeyRaw.replaceAll("[\\.#\\$\\[\\]]", "_");
|
||||||
|
|
||||||
|
for (Map<String, Object> game : entry.getValue()) {
|
||||||
|
try {
|
||||||
|
Map<String, Object> matchMap = new HashMap<>();
|
||||||
|
|
||||||
|
matchMap.put("home_nome", game.get("homeName"));
|
||||||
|
matchMap.put("away_nome", game.get("awayName"));
|
||||||
|
matchMap.put("home_logo", game.get("homeLogo"));
|
||||||
|
matchMap.put("away_logo", game.get("awayLogo"));
|
||||||
|
matchMap.put("cupJorney", game.get("cupJorney"));
|
||||||
|
|
||||||
|
String hGoals = String.valueOf(game.get("homeGoals"));
|
||||||
|
String aGoals = String.valueOf(game.get("awayGoals"));
|
||||||
|
|
||||||
|
boolean played = false;
|
||||||
|
if (hGoals != null && !hGoals.equals("null") && !hGoals.trim().isEmpty()) {
|
||||||
|
matchMap.put("home_golos", hGoals); // Some cup scores might be like "1p"
|
||||||
|
played = true;
|
||||||
|
}
|
||||||
|
if (aGoals != null && !aGoals.equals("null") && !aGoals.trim().isEmpty()) {
|
||||||
|
matchMap.put("away_golos", aGoals);
|
||||||
|
played = true;
|
||||||
|
}
|
||||||
|
matchMap.put("played", played);
|
||||||
|
|
||||||
|
Object dateObj = game.get("date");
|
||||||
|
if (dateObj != null && !String.valueOf(dateObj).equals("null")) {
|
||||||
|
String dateStr = String.valueOf(dateObj);
|
||||||
|
if (dateStr.contains("T")) {
|
||||||
|
String[] parts = dateStr.split("T");
|
||||||
|
matchMap.put("data", parts[0]);
|
||||||
|
if (parts[1].length() >= 5) {
|
||||||
|
matchMap.put("hora", parts[1].substring(0, 5));
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
matchMap.put("data", dateStr);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Object fieldObj = game.get("field");
|
||||||
|
if (fieldObj != null && !String.valueOf(fieldObj).equals("null")) {
|
||||||
|
matchMap.put("campo", String.valueOf(fieldObj));
|
||||||
|
}
|
||||||
|
|
||||||
|
Object reportUrlObj = game.get("reportURL");
|
||||||
|
if (reportUrlObj != null && !String.valueOf(reportUrlObj).equals("null") && !String.valueOf(reportUrlObj).trim().isEmpty()) {
|
||||||
|
matchMap.put("matchReportUrl", "https://api.afavcd.pt" + String.valueOf(reportUrlObj));
|
||||||
|
}
|
||||||
|
|
||||||
|
String cupID = String.valueOf(game.get("cupID"));
|
||||||
|
if (cupID.contains(".")) {
|
||||||
|
cupID = cupID.substring(0, cupID.indexOf("."));
|
||||||
|
}
|
||||||
|
|
||||||
|
refTaca.child(escalao).child(phaseKey).child(cupID).setValueAsync(matchMap);
|
||||||
|
} catch (Exception ex) {
|
||||||
|
System.err.println("Erro ao processar jogo: " + ex.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
System.out.println("-> Dados de " + escalao + " agendados para envio.");
|
||||||
|
} catch (Exception e) {
|
||||||
|
System.err.println("Erro a obter taça para " + escalao);
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
System.out.println("\nAguardando confirmação do servidor (10s)...");
|
||||||
|
Thread.sleep(10000);
|
||||||
|
System.out.println("Processo de Taça concluído!");
|
||||||
|
System.exit(0);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user