-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscrape_playlists.rmd
523 lines (463 loc) · 16.5 KB
/
scrape_playlists.rmd
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
---
title: "R Notebook"
output: html_notebook
---
#DEPRICATED. scrape_playlists.r is current version
Setup environment
```{r}
# scrape playlists
library(rvest)
library(stringr)
library(xml2)
library(tidyverse)
# ----------------------------------------------
ROOT_URL<-"http://wfmu.org"
#limit analysis to DJs with at least numShows shows.
# This also excludes DJs where we couldn't extract valid playlist URLs.
numShows <- 50
```
Get URLS of the the web pages containin links to the playlists
```{r}
#-------------------------------------------
getDJURLs <- function(){
rawDJURLs<- read_html(paste(ROOT_URL,"/playlists",sep=""))
# get the urls of the each DJs RSS playlist feed
t<-rawDJURLs%>%html_nodes(xpath='//html//body//center[2]//table[1]//table//a[contains(.,"Playlists")]') %>%
html_attr(name="href")
DJURLs<-paste("http://wfmu.org",t,sep="")[-1]
# above got the RSS feed links but we want the longer list of shows. Below modifies
# the URL to get the right link
DJURLs<- gsub("playlistfeed","playlists",DJURLs)
DJURLs<- gsub(".xml","",DJURLs)
return(DJURLs)
}
DJURLs<-getDJURLs()
```
If the DJKey file is already built load it. If not run the chunk following the loading step to build it.
```{r}
#load(file='djkey.rdata')
```
Get the show names for each DJ and identify those that are not currently on the air. Here is where we build the DJKey data frame initially. If the file is already built, load it instead of running the chunk.
```{r}
#-------------------------------------------
getDJsOffSched <- function(){
#table 9 is off sched. 2-8 are monday through sunday
rawDJURLs<- read_html(paste(ROOT_URL,"/playlists",sep=""))
t_off<-rawDJURLs%>%html_nodes(xpath='//html//body//center[2]//table[1]//table[9]//a[contains(.,"Playlists")]') %>% html_attr(name="href")
d_off <- str_replace(t_off,"/playlistfeed/","")
d_off <- str_replace(d_off,".xml","")
return(d_off)
}
#---------------------------------------------------
# get the shownames for a DJ
getShowNames<-function(DJURLs) {
DJKey <- data.frame()
for (page in DJURLs) {
singleDJ<- read_html(page)
showName <- html_node(singleDJ,"title")%>%html_text()
showName <- gsub("\n","",sub("Playlists and Archives for ","",showName))
showName<-str_replace(showName,'WFMU:',"")
showName<-str_replace_all(showName,':Playlists and Archives',"")
DJ <- sub("http://wfmu.org/playlists/","",page)
DJKey<-rbind(DJKey,data.frame(DJ=DJ,ShowName=showName))
print(showName)
}
# now identifty those DJs which are currently on the schedule
DJKey$onSched <- 'YES'
DJKey$onSched[which(DJKey$DJ %in% getDJsOffSched())]<-'NO'
#strip "WFMU" and "Playlists and Archives" and some punctuation
DJKey$ShowName<-str_replace_all(DJKey$ShowName,"(P|p)laylists (and|&) (A|a)rchives","")
DJKey$ShowName<-str_replace_all(DJKey$ShowName,"-","")
DJKey$ShowName<-str_replace_all(DJKey$ShowName,"(P|p)laylist|(R|r)ecent","")
DJKey$ShowName<-str_replace_all(DJKey$ShowName,"WFMU|wfmu","")
DJKey$ShowName<-str_replace_all(DJKey$ShowName,"The ","")
DJKey$ShowName<-str_trim(DJKey$ShowName)
return (DJKey)
}
DJKey<-getShowNames(DJURLs)
save(DJKey,file="DJKey.RData")
```
Eliminate non-music DJ's from the DJKey table. This list is created by hand and was last updated at the end of 2017.
```{r}
excludeDJs <-
c('SD',
'HA',
'BC',
'AF',
'CP',
'HP',
'JP',
'GM',
'DC',
'CC',
'DU',
'ES',
'LW',
'IM',
'LL',
'NW',
'NP',
'ZZ',
'FC',
'SY',
'TI',
'LK',
'TP',
'RC',
'VC')
music_djs<-DJKey %>%
select(DJ) %>%
anti_join(data_frame(DJ=excludeDJs)) %>%
pull(DJ)
```
Get the URLs of the pages actually containing the playlists. The tricky bit here is that some DJ pages contain all their playlists going back multiple years and some pages contain just the current year with further links to prior years. I try to catch this and go into the next level down.
```{r}
# -------------get the URLs of the playlist pages for a DJ ----------
#should work to delve into earlier years
get_playlist_page_URLs<-function(url_suffix) {
#first call should be the base DJ page with links to any earlier year playlist lists
if (str_length(url_suffix)==2){
dj<-url_suffix
latest_url<-paste0("/playlists/",url_suffix)
url_suffix<-latest_url
}
singleDJ<- read_html(paste0("http://wfmu.org",url_suffix))
#this assumes the earlier year playlist links are of the form
# wfmu.org/playlists/<dj><year>/
pl_url<-singleDJ %>%
html_nodes(xpath=paste0("//a[contains(@href,'playlists/",dj,"')]")) %>%
html_attr("href")
pl_url<-c(latest_url,pl_url) %>% unique()
return(pl_url)
}
# #---------------------------------------------------
# get the URLs of the playlists for a DJ
getDJPlaylistURLs<-function(music_djs) {
DJ_playlists = NULL
dudList<-NULL
#DJKey = data.frame()
for (dj in music_djs) {
print(dj)
url_suffixes<-get_playlist_page_URLs(dj)
for (u in url_suffixes){
print(u)
singleDJ<- read_html(paste0("http://wfmu.org",u))
pl<-singleDJ%>%
html_nodes(xpath="//a[contains(@href,'playlists/shows')]") %>%
html_attr("href")
#format for newer shows
pl<-as.character(na.omit(pl[str_detect(pl,"playlists/shows")]))
# format for older shows
if (length(pl)<1) pl<-as.character(na.omit(pl[str_detect(pl,"Playlist")]))
#assume a full URL is a fill-in DJ. We omit these from the analysis
pl<-pl[!str_detect(pl,"http")]
playlistURL<-pl %>% as.character()
#omit shows without valid playlists. Talk shows?
if (length(playlistURL)>0) {
DJ_playlists = bind_rows(DJ_playlists, data_frame(DJ=dj,playlistURL = playlistURL))
} else {
print("DUD")
dudList<-c(dudList,dj) }
}
}
return(DJ_playlists)
}
playlistURLs<-getDJPlaylistURLs(music_djs)
```
Add show counts to the DJKey table.
```{r}
showCounts<-playlistURLs %>%
group_by(DJ) %>%
summarise(showCount=n()) %>%
arrange(desc(showCount))
DJKey<-left_join(DJKey,showCounts)
# non-music shows
djList <- DJKey %>%
filter(showCount > numShows, !(DJ %in% excludeDJs)) %>%
pull(DJ)
```
The chunk below is essentially depricated. Instead of gettting this actual playlists, this function gets the lists of every artist the DJ has played, which is displayed on each DJ's playlist page. I keep the function here but it is not called anywhere.
```{r}
#-------------------------------------------------
# Get all Artists ever played by a DJ
#WFMU maintains this as a separate page
getDJArtistNames<-function(DJURLs) {
# scrape artist names for all DJs from the link at the bottom of each DJ page
allDJArtists<-data.frame()
URL_BRANCH<- "/artistkeywords.php/"
for (page in DJURLs) {
singleDJ<- read_html(page)
showName <- html_node(singleDJ,"title")%>%html_text()
showName <- gsub("\n","",sub("Playlists and Archives for ","",showName))
DJ <- sub("http://wfmu.org/playlists/","",page)
DJKey<-rbind(DJKey,data.frame(DJ=DJ,ShowName=showName))
print(showName)
artistListPage <- paste(ROOT_URL,URL_BRANCH,DJ, sep="")
artistList<-read_html(artistListPage)%>%html_node(xpath="//body/div")%>%html_text()%>%str_split("\n")
DJArtists<-data.frame(DJ,artistRaw=unlist(artistList))
if (nrow(DJArtists) >0) allDJArtists = rbind(allDJArtists,DJArtists)
#remove factor level of DJs with no artists
save(allDJArtists,file="allDJArtists.RData")
}
return(allDJArtists)
}
```
```{r}
#--------------------------------------------------------------------------
altArtistNames <- c('THE STOOGE', 'Band', 'Singer', 'Artist')
altTitleNames <- c('THE SONG', 'Track', 'Song','Title')
altHeaderNames <- c(altArtistNames, altTitleNames)
MAXLEN = 60 #how long should we let artist and title names be. Truncate longer
header_th_xpath <- paste(
"//th='Track'",
"or //th='Title'",
"or //th='Song'",
"or //th='THE SONG'"
)
header_td_xpath <- paste(
"//td='Track'",
"or //td='Title'",
"or //td='Song'",
"or //td='THE SONG'",
"or //td='Artist'",
"or //td='THE SONG'",
"or //td='THE STOOGE'"
)
```
The code below constains DJ-specific scraping routines. They get called when other methods fail. I could choose to call them just when that specific DJ comes up but I call them on all fails on the off chance that another DJ has the same playlist format.
```{r}
#------------------------------------------------------------------
try_BK<-function(wp){
#Bill Kelly's format
#assume first field is title and second is artist separated by dash
#doing it it two steps assure same number of artists and titles
title_artist<-wp %>%
html_nodes(xpath="//table[2]") %>%
html_text() %>%
str_extract_all("\n[\\S ]+\n-\n[\\S ]+\n") %>%
.[[1]]
Title<-title_artist %>%
str_extract_all("\n[\\S ]+\n-") %>%
str_replace_all("\n(-)?","")
Artist<-title_artist %>%
str_extract_all("\n-\n[\\S ]+\n") %>%
str_replace_all("\n(-)?","")
plraw<-tibble(Artist,Title)
}
#------------------------------------------------------------------
try_HN<-function(wp){
#assume first field is title and second is artist separated by colon
#doing it it two steps assure same number of artists and titles
title_artist<-wp %>%
html_nodes(xpath="//table[2]") %>%
html_text() %>%
str_extract_all("\n[\\S ]+: [\\S ]+\n") %>%
.[[1]]
Artist<-title_artist %>%
str_extract_all("\n[\\S ]+: ") %>%
str_replace_all("(\n)|(: )","")
Title<-title_artist %>%
str_extract_all(": [\\S ]+\n") %>%
str_replace_all("(\n)|(: )","")
plraw<-tibble(Artist,Title)
}#------------------------------------------------------------------
fixHeaders <- function(pl) {
#takes a data frame
nm<-which(names(pl) %in% altArtistNames)
tt<-which(names(pl) %in% altTitleNames)
if (length(nm)>0){
names(pl)[nm] <- "Artist"
} else{
pl=NULL
return(pl)
}
if (length(tt)>0){
names(pl)[tt] <- "Title"
} else{
pl$Title=NA
}
return(pl)
}
#--------------------------------------------------------------------
get_playlist <- function(plURL, dj) {
wholepage <- read_html(paste0(ROOT_URL, plURL))
#try to pull out the show date. assume first date in text on page is the show date
airDate <- wholepage %>%
html_text() %>%
str_extract('[A-Za-z]+ [0-9]{1,2}, [0-9]{4}') %>%
as.Date("%B %d, %Y")
if (is.na(airDate)) {
#try something else
airDate <- wholepage %>%
html_text() %>%
str_extract('[0-9]{1,2} [A-Za-z]+ [0-9]{4}') %>%
as.Date("%d %B %Y")
}
print(airDate)
if (airDate < most_recent_date-7) {
print("Back Far Enough")
return("stop")
}
plraw <- NULL
#hand-rolled
#simplest case. A table with obvious header names
if (!is.na(wholepage %>% html_node(xpath = "//th[@class='song']"))) {
table_shell<-xml_new_root("table")
#remove single column rows, I hope nothing else.
wholepage %>% html_nodes(xpath="//td[@colspan='8']") %>% xml_remove(free=T)
plraw<-wholepage%>%
html_nodes(xpa="//tr[td[@class ='song']] | //tr[th[@class ='song']]")
for (node in plraw) xml_add_child(table_shell,node)
plraw<-table_shell %>%
html_node(xpath="//table") %>%
html_table(fill=TRUE)
} else {
# no 'th' but are there rows in a table with td of class=song? get the table
if (!is.na(wholepage %>% html_node(xpath = "//td[@class='song']"))) {
plraw <- wholepage %>%
html_node(xpath = "//td[@class='song']/ancestor::table") %>%
html_table(fill = T)
#now find the row that has the header
for (n in 1:nrow(plraw)) {
if (TRUE %in% (plraw[n, ] %in% altHeaderNames)) {
names(plraw) <- plraw[n, ]
plraw <- plraw[n + 1:nrow(plraw), ]
break
}
if (n == nrow(plraw)) {
print("DUD. Can't find header")
plraw <- NULL
}
}
}
}
if (is.null(plraw)) {
# no song class, now what? is it a table? try to find header
#seems like cellspacing means its a row column thing
pl_table<-wholepage %>% html_node(xpath = "//table[@cellspacing and @cellpadding]")
num_rows<-pl_table %>% html_nodes("tr") %>% length()
if (num_rows>2) {
pl_table<-html_table(pl_table,fill = TRUE)
if (any(names(pl_table) %in% altHeaderNames)) {
plraw<-pl_table
} else{
# try one more ``
#scan until we find the playlist header
for (n in 1:nrow(pl_table)) {
if (TRUE %in% (pl_table[n,] %in% altHeaderNames)) {
names(pl_table) <- pl_table[n, ]
plraw <- pl_table[n + 1:nrow(pl_table), ]
break
}
}
}
}
}
# SPECIAL DJ TREATMENT
if (is.null(plraw)){
#try idiosyncratic djs
if (dj=="TW") plraw<-try_BK(wholepage)
if (dj=="HN") plraw<-try_HN(wholepage)
if (is.null(plraw)) print("DUD. Can't find header")
#final dead end
}
plraw<-fixHeaders(plraw)
# final clean up if we have something
if (is.null(plraw)) {
playlist <- NULL
} else {
if (TRUE %in% is.na(names(plraw)))
plraw <- plraw[, -which(is.na(names(plraw)))] #sometimes an NA column
playlist <- plraw %>%
select(Artist,Title) %>%
na.omit() %>%
transmute(DJ = dj,
AirDate = airDate,
Artist= Artist %>%
str_trunc(MAXLEN,"right") %>%
str_to_title() %>%
str_extract("[\\S ]+") %>% #get rid of extra lines in mutiple line cells
str_extract("[^\\(]+") %>% #dump parentheticals
str_trim(),
Title = Title %>%
str_trunc(MAXLEN,"right") %>%
str_to_title() %>%
str_extract("[\\S ]+[^()]") %>%
str_extract("[^\\(]+") %>%
str_trim()
) %>%
filter(Artist != '') %>%
filter(!is.na(Artist))
print(playlist[1:5, ])
}
return(playlist)
}
```
Main loop
I don't like this much. It makes an assumption that DJs have just one show per week in determining how far back to go when updating and existing playlist file. A better approach would be to go by each DJ. Find the latest date for each dj in the existing file, extract the show number for that date, then reject all URLs with lower show numbers. We still have to start with every creating every URL.
```{r include=FALSE}
#-------------- MAIN -----------------
showCounts<-playlistURLs %>%
group_by(DJ) %>%
summarise(showCount=n()) %>%
arrange(desc(showCount))
DJKey<-left_join(DJKey,showCounts)
#limit analysis to DJs with at least numShows shows.
# This also excludes DJs where we couldn't extract valid playlist URLs.
numShows <- 50
# non-music shows
djList <- DJKey %>%
filter(showCount > numShows, !(DJ %in% excludeDJs)) %>%
pull(DJ)
#careful not to trash intermediate results!
#playlists_raw = data_frame()
load("~/R Projects/wfmu/playlists_raw.RData")
UPDATE_ONLY =TRUE
if (UPDATE_ONLY) {
#assume at most 5 shows per week
#most shows are 1/week except the morning show
most_recent_date<-max(playlists_raw$AirDate)
go_back_num<- as.integer(round ((Sys.Date() - most_recent_date) * 5/7))
print(paste("Updating ONLY last",go_back_num,"Shows"))
} else{
go_back_num<-as.Date("1900-01-01") #arbitrarily far back so it's not a binding constraint
}
# if this breaks at a certain dj, correct and start with that DJ, in this case "TW."
# line below will process all djs after the matched DJ
#ideally not needed
#djList<-djList[match("TW",djList):length(djList)]
for (dj in djList) {
plURLs <- playlistURLs %>%
filter(DJ == dj) %>%
rowid_to_column() %>%
select(playlistURL)
if (UPDATE_ONLY) {
print(paste("Updating at most last",go_back_num,"Shows to",most_recent_date))
} else{
go_back_num<-nrow(plURLs)
}
for (n in 1:go_back_num){
plURL<-plURLs[n,1]
print(paste(dj, n, plURL))
if (!is.na(pull(plURLs[n,1]))){
new_playlist<- get_playlist(plURL, dj)
#end loop if we have reached most_recent_date beacuse get_playlist returns "stop" character string.
if (TRUE %in% (is(new_playlist)=="character")) {
break
}else{
playlists_raw <- bind_rows(playlists_raw,new_playlist)
}
}
}
#save to disk after each dj
if (!(TRUE %in% (is(new_playlist)=="character"))) {
print(paste("Saving playlists at",Sys.time()))
save(playlists_raw,file="playlists_raw.Rdata")
}
}
bad_Tables<-anti_join(tibble(DJ=djList),playlists_raw)
playlists_raw<-playlists_raw %>%
filter(Artist != Title) %>% #single column span across table. Not a song.
distinct()
save(playlists_raw,file="playlists_raw.Rdata")
```