
Coronavirus
package lab_3;
//1
//=5
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
public class Coronavirus {
public static void main(String[] args) {
String url = "https://www.worldometers.info/coronavirus/"; // Замените это URL-адресом вашего сайта
//
List<String> links = extractLinks(url); //Полученные страны
List<String> filteredLinks = filterLinks(links); //Страны для задания
/*
// Вывод найденных ссылок
for (String link : filteredLinks) {
System.out.println(link);
}
*/
//
Map<String, Integer> dataMap = parseAndStoreData(links, url); //Страны со значениями
/*
// Вывод данных
for (Map.Entry<String, Integer> entry : dataMap.entrySet()) {
String link = entry.getKey();
int data = entry.getValue();
System.out.println(link + ": " + data);
}
*/
//
Map<String, Integer> filteredDataMap = sortData(dataMap);
filteredDataMap = filterData(filteredDataMap); //Отсортированные страные со значениями
//filteredDataMap = transformKeys(filteredDataMap);
Map<String, Integer> foundDataMap = searchInDataMap(filteredLinks, dataMap); //Страны из задания со значениями
Map<String, Integer> mergedDataMap = new HashMap<>(); //Объединенные страны со значениями
mergedDataMap.putAll(filteredDataMap);
mergedDataMap.putAll(foundDataMap);
mergedDataMap = sortData(mergedDataMap);
//Map<String, Integer> tableDataMap = mergedDataMap;
mergedDataMap.put("country/world/", 0);
/*
// Вывод данных
for (Map.Entry<String, Integer> entry : mergedDataMap.entrySet()) {
String link = entry.getKey();
int data = entry.getValue();
System.out.println(link + ": " + data);
}
*/
/*
try {
getDataCountry("country/japan/", url);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
*/
//
ArrayList<CountryData> data = new ArrayList<>();
//CountryData cData = new CountryData();
for (Map.Entry<String, Integer> entry : mergedDataMap.entrySet()) {
String link = entry.getKey();
//int data = entry.getValue();
try {
data.add(getDataCountry(link, url));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Map <String, Integer> sortedMap = new LinkedHashMap<>(mergedDataMap);
sortedMap = transformKeys(sortedMap);
sortedMap = sortData(sortedMap);
/*
for (Map.Entry<String, Integer> entry : sortedMap.entrySet()) {
String link = entry.getKey();
int dataa = entry.getValue();
System.out.println(link + ": " + dataa);
}
*/
//Map <String, Integer> sortedMap = new LinkedHashMap<>(mergedDataMap);
//
CountryData cData = new CountryData();
Map<String, CountryData> countryDataMap = new LinkedHashMap<>();
int count = 0;
for (Map.Entry<String, Integer> entry : sortedMap.entrySet()) {
String key = entry.getKey();
cData = data.get(count);
countryDataMap.put(key, cData);
//System.out.println(key + ": " + cData);
count += 1;
}
//printTable(countryDataMap, tableDataMap);
/*
Map<String, Integer> aa = new LinkedHashMap<>();
aa.put("q", 0);
aa.put("w", 0);
aa.put("e", 0);
aa.put("r", 0);
aa.put("t", 0);
aa.put("y", 0);
aa.put("u", 0);
aa.put("i", 0);
aa.put("o", 0);
aa.put("p", 0);
aa.put("a", 0);
aa.put("s", 0);
aa.put("d", 0);
aa.put("f", 0);
aa.put("g", 0);
aa.put("h", 0);
aa.put("j", 0);
aa.put("k", 0);
aa.put("l", 0);
aa.put("z", 0);
aa.put("x", 0);
aa.put("c", 0);
aa.put("v", 0);
aa.put("b", 0);
aa.put("n", 0);
*/
CountryStatsFrame frame = new CountryStatsFrame(countryDataMap);
//CountryStatsFrame frame = null;
//frame.createFrame(aa);
frame.setVisible(true);
}
//Получение списка стран
private static List<String> extractLinks(String url) {
List<String> links = new ArrayList<>();
try {
//Загрузка HTML-страницы
Document doc = Jsoup.connect(url).get();
//Поиск всех элементов <a> с классом "mt_a"
Elements elements = doc.select("a.mt_a");
//Извлечение ссылок из элементов
for (Element element : elements) {
if (links.size() >= 229) {
break; //Прекратить добавление ссылок при достижении 219
}
String href = element.attr("href");
links.add(href);
}
} catch (IOException e) {
e.printStackTrace();
}
return links;
}
//Получение списка стран, необходимые для задание, при их наличии
private static List<String> filterLinks(List<String> links) {
List<String> filteredLinks = new ArrayList<>();
for (String link : links) {
if (link.equals("country/israel/") || link.equals("country/switzerland/") || link.equals("country/japan/") || link.equals("country/china/")) {
filteredLinks.add(link);
}
}
return filteredLinks;
}
//Получение словаря, где ключ: страна, а значение: сумма вылечившихся и умерших людей
private static Map<String, Integer> parseAndStoreData(List<String> links, String url) {
Map<String, Integer> dataMap = new HashMap<>();
//String[] status = {"Deaths:", "Recovered:"};
for (String link : links) {
String fullUrl = url + link;
try {
// Загрузка HTML-страницы
Document doc = Jsoup.connect(fullUrl).get();
// Поиск элементов с id "maincounter-wrap"
Elements counterElements = doc.select("#maincounter-wrap");
// Парсинг данных
Integer data = parseCounterData(counterElements);
if (data > 0) {
dataMap.put(link, data);
}
} catch (IOException e) {
e.printStackTrace();
}
}
return dataMap;
}
//Получение суммы вылечившихся и умерших людей
private static Integer parseCounterData(Elements counterElements) {
String[] status = {"Deaths:", "Recovered:"};
Integer count = 0;
for (Element counterElement : counterElements) {
for(int i = 0; i < status.length; i++) {
Element h1Element = counterElement.selectFirst("h1");
if (h1Element != null && h1Element.text().equals(status[i])) {
Element numberElement = counterElement.selectFirst(".maincounter-number > span");
if (numberElement != null) {
String data = numberElement.text().replaceAll(",", "");
if (data.matches("\\d+")) {
count += Integer.parseInt(data);
}
}
}
}
}
//System.out.println(count);
return count;
}
//Получение топ-20 стран по получившемуся значению
public static Map<String, Integer> sortData(Map<String, Integer> dataMap) {
// Сортировка данных по убыванию значений
Map<String, Integer> sortedMap = dataMap.entrySet()
.stream() //Преобразуем набор записей в поток элементов
.sorted(Map.Entry.comparingByValue(Comparator.reverseOrder())) //Компаратор сортирует записи по значению в убывающем порядке
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e1, LinkedHashMap::new)); //Преобразование поток элементов обратно в Map
return sortedMap;
}
public static Map<String, Integer> filterData(Map<String, Integer> dataMap) {
// Оставляем только первые 20 элементов
Map<String, Integer> filteredMap = dataMap.entrySet()
.stream()
.limit(20)
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e1, LinkedHashMap::new));
return filteredMap;
}
//Получение изменившегося ключа
public static Map<String, Integer> transformKeys(Map<String, Integer> map) {
Map<String, Integer> transformedMap = new HashMap<>();
for (Map.Entry<String, Integer> entry : map.entrySet()) {
String originalKey = entry.getKey();
Integer value = entry.getValue();
// Преобразование ключа: country/us/ -> Us
String[] transformedKey = originalKey.split("/");
String newTransformedKey = transformedKey[1];
newTransformedKey = newTransformedKey.substring(0, 1).toUpperCase() + newTransformedKey.substring(1);
transformedMap.put(newTransformedKey, value);
}
return transformedMap;
}
//Получение ключа и значения для стран, необходимых по заданию
public static Map<String, Integer> searchInDataMap(List<String> filteredLinks, Map<String, Integer> dataMap) {
Map<String, Integer> foundDataMap = new HashMap<>();
for (String link : filteredLinks) {
if (dataMap.containsKey(link)) {
foundDataMap.put(link, dataMap.get(link));
}
}
return foundDataMap;
}
private static String extractDataCases(String sourceCode) {
String pattern = "series:\\s*\\[\\s*\\{\\s*name:\\s*'Cases'.*?\\}\\s*]";
Pattern regex = Pattern.compile(pattern);
Matcher matcher = regex.matcher(sourceCode);
if (matcher.find()) {
return matcher.group();
} else {
return null;
}
}
private static String extractDataInfected(String sourceCode) {
String pattern = "series:\\s*\\[\\s*\\{\\s*name:\\s*'Currently Infected'.*?\\}\\s*]";
Pattern regex = Pattern.compile(pattern);
Matcher matcher = regex.matcher(sourceCode);
if (matcher.find()) {
return matcher.group();
} else {
return null;
}
}
private static String extractDataDeaths(String sourceCode) {
String pattern = "series:\\s*\\[\\s*\\{\\s*name:\\s*'Deaths'.*?\\}\\s*]";
Pattern regex = Pattern.compile(pattern);
Matcher matcher = regex.matcher(sourceCode);
if (matcher.find()) {
return matcher.group();
} else {
return null;
}
}
//Получение списка данных
private static ArrayList<Integer> extractData(String sourceCode, String status) {
//String d = "Deaths";
String dataString = null;
String patternOne = "series:\\s*\\[\\s*\\{\\s*name:\\s*'";
String patternTwo = "'.*?\\}\\s*]";
String patternn = patternOne + status + patternTwo;
//String pattern = "series:\\s*\\[\\s*\\{\\s*name:\\s*'Deaths'.*?\\}\\s*]";
//System.out.println(patternn);
//System.out.println(pattern);
Pattern regex = Pattern.compile(patternn);
Matcher matcher = regex.matcher(sourceCode);
int dataInt = 0;
ArrayList<Integer> dataCasesInt = new ArrayList<>();
if (matcher.find()) {
dataString = matcher.group();
String[] dataList = dataString.split("data:");
dataList = dataList[1].split("]");
String data = dataList[0].substring(2);
dataList = data.split(",");
for (String dataEnum : dataList) {
if (dataEnum.matches("\\d+")) {
dataInt = Integer.parseInt(dataEnum);
if(dataInt>-1) {
dataCasesInt.add(dataInt);
}
else {
dataCasesInt.add(0);
}
}
else {
dataCasesInt.add(0);
}
}
return dataCasesInt;
}else {
return null;
}
}
private static String getSourceCode(String url) throws IOException {
URL website = new URL(url);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(website.openStream()));
StringBuilder sourceCode = new StringBuilder();
String inputLine;
while ((inputLine = bufferedReader.readLine()) != null) {
sourceCode.append(inputLine);
}
bufferedReader.close();
return sourceCode.toString();
}
private static CountryData getDataCountry(String key, String url) throws IOException {
String newUrl = "";
String code = "";
ArrayList<Integer> dataCases = new ArrayList<>();
ArrayList<Integer> dataInfected = new ArrayList<>();
ArrayList<Integer> dataDeaths = new ArrayList<>();
ArrayList<Integer> dataRecovered = new ArrayList<>();
ArrayList<Double> effectiveness = new ArrayList<>();
//ArrayList<CountryData> countryData = new ArrayList<>();
//CountryData data = null;
if(key.equals("country/japan/")) {
newUrl = url + key;
code = getSourceCode(newUrl);
dataCases = extractData(code, "Cases");
dataDeaths = extractData(code, "Deaths");
newUrl = url + "country/south-korea/";
code = getSourceCode(newUrl);
dataInfected = extractData(code, "Currently Infected");
}
else if(key.equals("country/world/")){
newUrl = url;
code = getSourceCode(newUrl);
dataCases = extractData(code, "Cases");
dataInfected = extractData(code, "Currently Infected");
dataDeaths = extractData(code, "Deaths");
for(int i = 0; i < 24; i++) {
dataCases.remove(i);
dataInfected.remove(i);
dataDeaths.remove(i);
}
}
else {
newUrl = url + key;
code = getSourceCode(newUrl);
dataCases = extractData(code, "Cases");
dataInfected = extractData(code, "Currently Infected");
dataDeaths = extractData(code, "Deaths");
}
int check = 0;
for(int i = 0; i < dataCases.size(); i++) {
check = dataCases.get(i) - dataInfected.get(i) - dataDeaths.get(i);
if(check>0) {
dataRecovered.add(check);
}
else {
dataRecovered.add(0);
}
}
double effect = 0.0;
for(int i = 0; i < dataCases.size(); i++) {
int dC = dataCases.get(i);
int dCI = dataInfected.get(i);
int dD = dataDeaths.get(i);
int dR = dataRecovered.get(i);
if(dC == 0 || dC == dR) {
effectiveness.add(100.0);
}
else if(dC == dCI) {
effectiveness.add(50.0);
}
else{
effect = (1.0 - ((dR+dCI*2.0+dD*3.0)/(dC*3.0)))*100.0;
effect = Math.round(effect*10.0)/10.0;
if(effect >= 0) {
effectiveness.add(effect);
}
else {
effectiveness.add(0.0);
}
}
}
/*
System.out.println(key);
System.out.println(dataCases);
System.out.println(dataInfected);
System.out.println(dataDeaths);
System.out.println(dataRecovered);
System.out.println(dataRecovered.size());
*/
/*
System.out.println(effectiveness);
System.out.println(effectiveness.size());
*/
System.out.println("Данные из " + key + " успешно получены!");
CountryData data = new CountryData(effectiveness);
return data;
}
/*
public static void printTable(Map<String, CountryData> countryDataMap, Map<String, Integer> tableDataMap) {
System.out.printf("%-3s | %-15s | %-12s | %-5s%n", "№", "Страна", "Сумма", "Коэф.");
ArrayList<Double> d = new ArrayList<>();
int counter = 1;
int a = 0;
int n = 0;
for (Map.Entry<String, CountryData> entry : countryDataMap.entrySet()) {
a = 0;
n = 0;
System.out.println("--------------------------------------------------");
if(counter > 25) {
break;
}
String key = entry.getKey();
CountryData value = entry.getValue();
d = value.getData();
double sum = 0;
for (double valuee : d) {
sum += valuee;
}
a = (int) (sum/d.size());
n = tableDataMap.get(key);
System.out.printf("%-3d | %-15s | %-12d | %-5.2f%n", counter, key, value, a);
System.out.println("--------------------------------------------------");
counter++;
d.clear();
}
}
*/
}
CountryData
package lab_3;
//2
Import java.Util.ArrayList;
public class CountryData {
/*
public ArrayList<Integer> dataCases = new ArrayList<>();
public ArrayList<Integer> dataInfected = new ArrayList<>();
public ArrayList<Integer> dataDeaths = new ArrayList<>();
public ArrayList<Integer> dataRecovered = new ArrayList<>();
*/
public ArrayList<Double> data;
/*
public CountryData(ArrayList<Integer> dataCases, ArrayList<Integer> dataInfected, ArrayList<Integer> dataDeaths, ArrayList<Integer> dataRecovered) {
this.dataCases = dataCases;
this.dataInfected = dataInfected;
this.dataDeaths = dataDeaths;
this.dataRecovered = dataRecovered;
}
*/
public CountryData() {
this.data = new ArrayList<>();
}
public CountryData(ArrayList<Double> data) {
this.data = data;
}
public ArrayList<Double> getData(){
return this.data;
}
}
CountryStatsFrame
package lab_3;
//3
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
Import java.Util.ArrayList;
Import java.Util.List;
Import java.Util.Map;
public class CountryStatsFrame extends JFrame {
private Map<String, CountryData> mergedDataMap;
private int width = 1100;
private int height = 650;
public CountryStatsFrame(Map<String, CountryData> mergedDataMap) {
this.mergedDataMap = mergedDataMap;
initializeFrame();
addComponents();
}
private void initializeFrame() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(width+16, height-35);
setTitle("Country Statistics");
setLocationRelativeTo(null);
setLayout(new BorderLayout());
}
private void addComponents() {
JLabel titleLabel = new JLabel("Топ-20 стран по выздоровевшим и умершим");
titleLabel.setHorizontalAlignment(SwingConstants.CENTER);
add(titleLabel, BorderLayout.NORTH);
JPanel mainPanel = new JPanel(null); // Создаем основную панель
add(mainPanel, BorderLayout.CENTER);
int newWidth = width / 5;
int newHeight = 100;
int stepW = 0;
//int stepOne = 170;
int stepH = 20;
int count = 0;
List<String> top5Keys = new ArrayList<>(mergedDataMap.keySet());
top5Keys = top5Keys.subList(0, Math.min(top5Keys.size(), 5));
for (int i = 0; i < top5Keys.size(); i++) {
String key = top5Keys.get(i);
CountryData value = mergedDataMap.get(key);
String buttonText = (i + 1) + ". " + key;
JButton button = new JButton(buttonText);
button.setBounds(stepW, stepH, newWidth, newHeight);
stepW += newWidth;
//stepH += newHeight;
button.addActionListener(new ButtonClickListener(key, value));
mainPanel.add(button);
}
stepW = 0;
stepH += newHeight;
List<String> top10Keys = new ArrayList<>(mergedDataMap.keySet());
top10Keys = top10Keys.subList(5, Math.min(top10Keys.size(), 10));
for (int i = 0; i < top10Keys.size(); i++) {
String key = top10Keys.get(i);
CountryData value = mergedDataMap.get(key);
String buttonText = (i + 6) + ". " + key;
JButton button = new JButton(buttonText);
button.setBounds(stepW, stepH, newWidth, newHeight);
stepW += newWidth;
//stepH += newHeight;
button.addActionListener(new ButtonClickListener(key, value));
mainPanel.add(button);
}
stepW = 0;
stepH += newHeight;
List<String> top15Keys = new ArrayList<>(mergedDataMap.keySet());
top15Keys = top15Keys.subList(10, Math.min(top15Keys.size(), 15));
for (int i = 0; i < top15Keys.size(); i++) {
String key = top15Keys.get(i);
CountryData value = mergedDataMap.get(key);
String buttonText = (i + 11) + ". " + key;
JButton button = new JButton(buttonText);
button.setBounds(stepW, stepH, newWidth, newHeight);
stepW += newWidth;
//stepH += newHeight;
button.addActionListener(new ButtonClickListener(key, value));
mainPanel.add(button);
}
stepW = 0;
stepH += newHeight;
List<String> top20Keys = new ArrayList<>(mergedDataMap.keySet());
top20Keys = top20Keys.subList(15, Math.min(top20Keys.size(), 20));
for (int i = 0; i < top20Keys.size(); i++) {
String key = top20Keys.get(i);
CountryData value = mergedDataMap.get(key);
String buttonText = (i + 16) + ". " + key;
JButton button = new JButton(buttonText);
button.setBounds(stepW, stepH, newWidth, newHeight);
stepW += newWidth;
//stepH += newHeight;
button.addActionListener(new ButtonClickListener(key, value));
mainPanel.add(button);
}
JLabel titleLabel1 = new JLabel("Другие страны");
//titleLabel1.setHorizontalAlignment(SwingConstants.CENTER);
titleLabel1.setBounds(516, 420, 200, 30); // Установка координат и размеров для надписи
mainPanel.add(titleLabel1);
stepW = 0;
stepH += 140;
List<String> top25Keys = new ArrayList<>(mergedDataMap.keySet());
top25Keys = top25Keys.subList(20, Math.min(top25Keys.size(), 25));
for (int i = 0; i < top25Keys.size(); i++) {
String key = top25Keys.get(i);
CountryData value = mergedDataMap.get(key);
String buttonText = key;
JButton button = new JButton(buttonText);
button.setBounds(stepW, stepH, newWidth, newHeight);
stepW += newWidth;
//stepH += newHeight;
button.addActionListener(new ButtonClickListener(key, value));
mainPanel.add(button);
}
}
}
ButtonClickListener
package lab_3;
//4
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;