Skip to content

Instantly share code, notes, and snippets.

View MattSandy's full-sized avatar
💭
Hungry

Matt Sandy MattSandy

💭
Hungry
View GitHub Profile
@MattSandy
MattSandy / geo_location.php
Created September 21, 2015 20:58
Checks if within a defined box based on longitude and latitude
<?php
$url = 'https://api.smartystreets.com/street-address?';
$url .= 'auth-id=[auth-id]&auth-token=[auth-token]';
$url .= '&street=' . $street . '&zipcode=' . $zip;
$urlData = json_decode(file_get_contents($url),true);
//print_r($urlData);
if(isset($urlData[0]['metadata']['latitude'])) {
$latitude = $urlData[0]['metadata']['latitude'];
$longitude = $urlData[0]['metadata']['longitude'];
@MattSandy
MattSandy / server.R
Created September 28, 2015 01:46
Creates a websocket based JSON server
#Connect to this using websockets on port 9454
#Send in the format of {"data":[1,2,3]}
#The ppp returns the standard deviation of the sent array
library(jsonlite)
library(httpuv)
#server
app <- list(
onWSOpen = function(ws) {
ws$onMessage(function(binary, message) {
write(message, file = "log.txt",append = TRUE, sep = "\n")
@MattSandy
MattSandy / run.sql
Created October 27, 2015 17:16
Grab all columns from rows matching prefix.
SELECT `TABLE_NAME`,`COLUMN_NAME`
FROM `INFORMATION_SCHEMA`.`COLUMNS`
WHERE `TABLE_SCHEMA`='wordpress'
AND `TABLE_NAME` LIKE 'wp_%';
@MattSandy
MattSandy / remove-themes.php
Created January 15, 2016 09:06
Remove Inactive Wordpress Themes
<?php define('WP_USE_THEMES', false); require('./wp-blog-header.php');
remove_inactive_themes();
function remove_inactive_themes()
{
echo '<h1>Removing Inactive Themes</h1><ul>';
$theme_dir = get_theme_root();
$template_dir = get_template_directory();
foreach (scandir($theme_dir) as $res) {
if (is_dir($theme_dir . '/' . $res)&&($res!='.')&($res!='..')) {
@MattSandy
MattSandy / app.js
Created February 22, 2016 19:56
Food Tracker Notification for Pizza Luce
var request = require('request');
var cheerio = require('cheerio');
var notifier = require('node-notifier');
var status = "";
var url = "[tracking url]";
var interval = setInterval(function(url, notifier) {
get_status(url, notifier);
}, 30000, url, notifier);
@MattSandy
MattSandy / csv-wide.R
Last active March 5, 2016 03:42
Creates a wide format for matches
# How to run the script
# rscript csv-wide.R "~/path/to/in.csv" "~/path/to/out.csv" column_name_for_matches
args = commandArgs(trailingOnly=TRUE)
match_column <- args[3]
max_width <- 0
csv <- read.csv(args[1], header = T, sep = ",", quote = "\"",
stringsAsFactors = F, encoding="UTF-8", fill=T)
csv[[match_column]] <- as.character(csv[[match_column]])
#grabs the max length of columns that will exist
for(needle in unique(csv[[match_column]])) {
@MattSandy
MattSandy / story.R
Last active September 17, 2016 19:47
Creates an ordered frequency count based on input from the terminal. echo "test1 test2 test2" | Rscript story.R
input<-file('stdin', 'r')
rows <- readLines(input, n=1)
story <- c()
story <- unlist(lapply(rows,function(x) {
split <- strsplit(gsub("[[:punct:]]", "",x), " |\t")
return(split)
}))
df <- data.frame(sort(table(story),decreasing=TRUE))
@MattSandy
MattSandy / herd.R
Created April 4, 2016 20:28
Calculate percentage of occurrences for diseases based on the herd and date.
df <- read.csv(paste("~/R/df/","import.csv",sep=""),
header = TRUE, sep = ",", quote = "\"",stringsAsFactors = FALSE, encoding="UTF-8")
names(df) <- c("herd","date","disease")
output <- matrix(, nrow = 0, ncol = 4)
for(herd in unique(df$herd)) {
for(date in unique(df$date)) {
for(disease in unique(df$disease)) {
subset <- df[which(df$herd==herd & df$date==date),]
output <- rbind(output,c(herd,date,disease,nrow(subset[which(subset$disease==disease),])/nrow(subset)))
}
@MattSandy
MattSandy / app.js
Created May 2, 2016 01:13
Scrape Subreddit Submissions
var http = require('http');
var https = require('https');
var fs = require('fs');
fs.writeFile('posts.csv', 'Author,ID,Post Date,Comments,Score,Stickied,Pull,Subreddit\n', function(){console.log('done')});
fs.writeFile('users.csv', 'Author,Author Date\n', function(){console.log('done')});
scrape_hot("",1,"The_Donald");
scrape_hot("",1,"SandersForPresident");
scrape_hot("",1,"hillaryclinton");
@MattSandy
MattSandy / Find_Matches.R
Created May 14, 2016 00:11
Create Column that Mark Rows as Matched between Two Datasets
pop <- read.csv(paste("~/R/dumb_shit/import/","pop.csv",sep=""), header = FALSE, sep = ",", quote = "\"",stringsAsFactors = FALSE, encoding="UTF-8")
coverage <- read.csv(paste("~/R/dumb_shit/import/","coverage.csv",sep=""), header = FALSE, sep = ",", quote = "\"",stringsAsFactors = FALSE, encoding="UTF-8")
names(pop) <- c("City","State","Pop")
names(coverage) <- c("City","State")
find_match <- function(x) {
result <- NA
if(nrow(coverage[which(coverage$City==x["City"] & coverage$State==x["State"]),])>0) {
result <- "Match"
}