Last active
April 6, 2020 21:16
-
-
Save dubsnipe/6a99e7702c57c4aafb302789bc50924e to your computer and use it in GitHub Desktop.
Quick script to generate Open Know-How manifests out of a Google Form
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require(googledrive) | |
url <- "" | |
# Download form responses to a .csv file | |
filename <- "okh_data.csv" | |
drive_download(file = as_id(url), | |
path = filename, | |
overwrite = TRUE) | |
# Read file and rename columns | |
# to something more readable | |
okh_row_names <- c( | |
"timestamp", | |
"doc_url", | |
"project_url", | |
"title", | |
"description", | |
"date_published", | |
"doc_language", | |
"username", | |
"doc_contains", | |
"built", | |
"sdg", | |
"image_filename", | |
"intended_use", | |
"designer_name", | |
"affiliation", | |
"country", | |
"version", | |
"built_independently", | |
"derivative_of", | |
"variant_of", | |
"schematics_url", | |
"bom_url", | |
"manufacturing_files_url", | |
"tool_list_url", | |
"main_materials", | |
"estimated_cost", | |
"estimated_cost_currency" | |
) | |
okh_data <- read.csv(filename, | |
header=T, | |
fill=T, | |
stringsAsFactors=F | |
) | |
colnames(okh_data) <- okh_row_names | |
# Transforming timestamp into a workable date | |
okh_data$date <- as.Date(strptime(okh_data$timestamp, "", tz="America/El_Salvador")) | |
okh_data$date_published_2 <- as.Date(strptime(okh_data$date_published, "%m/%d/%Y", tz="America/El_Salvador")) | |
okh_data$date_published_3 <- format(okh_data$date_published_2, "%Y") | |
# Images | |
image_links <- paste0("https://appropedia.org/File:", okh_data$image_filename) | |
# Web crawler to retrieve an image url | |
# http://mazamascience.com/WorkingWithData/?p=912 | |
image_url <- vector() | |
for(i in 1:length(image_links)){ | |
image_url[i] <- tryCatch({ | |
session <- html_session(image_links[i]) | |
session %>% | |
html_nodes(".fullImageLink a") %>% | |
html_attr("href") %>% | |
head(1) | |
}, warning = function(w){ | |
NA | |
}, error = function(x){ | |
NA | |
}) | |
} | |
okh_data$image_filename_full <- ifelse(!is.na(image_url), paste0("https://appropedia.org", image_url), NA) | |
# This function concatenates content into | |
# a manifest as a YAML file. | |
apply(okh_data, 1, function(test) cat("# Open know-how manifest v0.1 | |
--- | |
", | |
"date-created: ", ifelse(is.na(test["date"]), "", as.character(test["date"])), "\n", | |
"date-updated: ", as.character(Sys.Date()), "\n", | |
"manifest-author:\n name: Kathy Nativi\n affiliation: Appropedia\n email: info@appropedia.org\nmanifest-language: eng-us\n", | |
"documentation-language: ", test["doc_language"], "\n", | |
"\n# Properties\n", | |
"title: ", gsub('"', '', test["title"]), "\n", | |
if(nchar(test["description"]) > 0 && !is.na(test["description"])){paste0("description: |\n ", test["description"], "\n")}, | |
if(nchar(test["intended_use"]) > 0 && !is.na(test["intended_use"])){paste0("intended-use: |\n ", test["intended_use"], "\n")}, | |
if(nchar(test["variant_of"]) > 0 && !is.na(test["variant_of"])){paste0("keywords: \n - ", gsub(", ", "\n - ", tolower(test["variant_of"])), "\n")}, | |
if(nchar(test["project_url"]) > 0 && !is.na(test["project_url"])){paste0("project-link: ", test["project_url"], "\n")}, | |
if(nchar(test["username"]) > 0 && !is.na(test["username"])){paste0("contact:\n name: ", test["username"], "\n")}, | |
# if(nchar(test["username"]) > 0 && !is.na(test["username"])){paste0("contact:\n name: ", gsub(" ", "_", test["username"]), "\n")}, | |
if(nchar(test["affiliation"]) > 0 && !is.na(test["affiliation"])){paste0(" affiliation: ", test["affiliation"], "\n")}, | |
if(!is.na(test["image_filename_full"])){paste0("image: ", test["image_filename_full"], "\n")}, | |
ifelse(test["built"]=="Yes", "made: FALSE\n", "made: TRUE\n"), | |
ifelse(test["built_independently"] == "TRUE", "", paste0("made-independently: TRUE\n")), | |
if(nchar(test["derivative_of"]) > 0 && !is.na(test["derivative_of"])){paste0("derivative-of: \n ", test["derivative_of"], "\n")}, | |
"license:\n documentation: CC-BY-SA 3.0\n\n", | |
"# Documentation\n", | |
if(nchar(test["doc_url"]) > 0 && !is.na(test["doc_url"])){paste0("documentation-home: ", test["doc_url"], "\n\n")}, | |
"# Appropedia-specific fields\n", | |
if(nchar(test["date_published"]) > 0 && !is.na(test["date_published"])){paste0("date-published: ", test["date_published_2"], "\n")}, | |
if(nchar(test["sdg"]) > 0 && !is.na(test["sdg"])){paste0("sustainable-development-goals: \n - ", gsub(", SDG", "\n - SDG", test["sdg"]), "\n")}, | |
sep = "", file = paste0("R/manifests/okh-", gsub('"|/', '', test["title"]), ".yml") | |
)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment