library("curl")
library("jsonlite")

urlstring<- "https://tools.wmflabs.org/mediaplaycounts/api/1/FilePlaycount/date"

# setup header for Cross Origin Request
h <- new_handle()
handle_setheaders(h, "ORIGIN" = "https://wmflabs.org")


dates <- c("20161231","20170101","20170102","20170103","20170104","20170105", "20170106")
pages <- c("ADA-Video_Mach_mit_bei_Wikipedia.webm")

#generate empty dataframe with appropriate number of col/rows
myframe <- data.frame(matrix(nrow =length(dates),ncol=length(pages)))

colnames(myframe) <- pages 
rownames(myframe) <- dates

for(page in pages){
  for (date in dates){ 
    requeststring <- paste(urlstring,page,date, sep="/") #assemble the URI to be asked
    print(requeststring) # print URI; poor programmer’s progress indicator
    apiDataObject<- curl_fetch_memory(requeststring, handle = h) #get the data
    viewcount <- fromJSON(rawToChar(apiDataObject$content))$count #data will be in a non-human readable object. Get the JSONs text, them trun it into an R object and read the part "count"
    if(is.numeric(viewcount)){ # if the count is a number put it in the table
      myframe[date,page] <- viewcount
    } else { # but maybe there are no views or there was another problem. 
      myframe[date,page] <- NA #In this case, put NA (a no data-Value) there
    }
  }
}

for(page in pages){
  print(paste(page,"- Views:",sum(myframe[[page]],na.rm = TRUE)))
  print(plot(myframe[[page]],type="l"))
}
[1] "https://tools.wmflabs.org/mediaplaycounts/api/1/FilePlaycount/date/ADA-Video_Mach_mit_bei_Wikipedia.webm/20161231"
[1] "https://tools.wmflabs.org/mediaplaycounts/api/1/FilePlaycount/date/ADA-Video_Mach_mit_bei_Wikipedia.webm/20170101"
[1] "https://tools.wmflabs.org/mediaplaycounts/api/1/FilePlaycount/date/ADA-Video_Mach_mit_bei_Wikipedia.webm/20170102"
[1] "https://tools.wmflabs.org/mediaplaycounts/api/1/FilePlaycount/date/ADA-Video_Mach_mit_bei_Wikipedia.webm/20170103"
[1] "https://tools.wmflabs.org/mediaplaycounts/api/1/FilePlaycount/date/ADA-Video_Mach_mit_bei_Wikipedia.webm/20170104"
[1] "https://tools.wmflabs.org/mediaplaycounts/api/1/FilePlaycount/date/ADA-Video_Mach_mit_bei_Wikipedia.webm/20170105"
[1] "https://tools.wmflabs.org/mediaplaycounts/api/1/FilePlaycount/date/ADA-Video_Mach_mit_bei_Wikipedia.webm/20170106"
[1] "ADA-Video_Mach_mit_bei_Wikipedia.webm - Views: 1307"
NULL