-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathscrape_flags.R
67 lines (60 loc) · 1.92 KB
/
scrape_flags.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
##
## scrape_players: players in squads
## scrape_flag: national team flags
## scrape_colours: national team kits
## scrape_comp: competition teams and logos
##
library(tidyverse)
library(magick)
w <- read_csv("./data/wiki_players.csv", guess_max = 1e5)
n0 <- w %>%
select(nat_team, nat_team_alpha3) %>%
distinct() %>%
rename(label = nat_team,
alpha3 = nat_team_alpha3)
d0 <- w %>%
select(club_country_harm, club_alpha3, club_country_flag, year) %>%
filter(club_alpha3 %in% n0$alpha3) %>%
group_by_at(1:3) %>%
mutate(year_min = min(year),
year_max = max(year),
years = paste0(unique(sort(year)), collapse = ",")) %>%
ungroup() %>%
select(-year) %>%
distinct() %>%
rename(label = club_country_harm,
alpha3 = club_alpha3,
flag_url = club_country_flag) %>%
drop_na() %>%
arrange(label) %>%
mutate(label = ifelse(str_detect(string = flag_url, pattern = "Wales"),
"Wales", label),
alpha3 = ifelse(str_detect(string = flag_url, pattern = "Wales"),
"GB-WLS", alpha3),
flag_url = paste0("https:", flag_url))
# any countries not that never had any national team players
# in their league?
n0 %>%
filter(!alpha3 %in% unique(d0$alpha3))
d1 <- n0 %>%
filter(!alpha3 %in% unique(d0$alpha3)) %>%
mutate(
flag_url = "https://upload.wikimedia.org/wikipedia/commons/4/45/Flag_of_Ireland.svg",
years = "1990,1994,2002",
year_min = 1990,
year_max = 2002)
# use most upto date flag
d2 <- d0 %>%
bind_rows(d1) %>%
arrange(alpha3, year_min) %>%
group_by(alpha3) %>%
slice(n())
b <- image_blank(width = 100, height = 75, color = "grey40")
for(i in 1:nrow(d2)){
message(d2$alpha3[i])
f <- image_read_svg(path = d2$flag_url[i])
f %>%
image_resize("100x75") %>%
image_composite(image = b, composite_image = ., gravity = "center") %>%
image_write(path = paste0("./flag/",d2$alpha3[i], ".png"))
}