Skip to content

Instantly share code, notes, and snippets.

View sunmockyang's full-sized avatar

Sunmock Yang sunmockyang

View GitHub Profile
@sunmockyang
sunmockyang / ruby-class.rb
Last active May 27, 2019 14:13
A ruby template class
class RubyClass
attr_accessor :name, :size # Creates getter and setter methods.
@@class_variable = 0
# constructor
def initialize(name, size)
@name = name
@size = size
@@class_variable += 1
@sunmockyang
sunmockyang / tictactoe_checker.js
Created March 28, 2019 21:05
A function that will check if a 2d array contains a tic tac toe win
var map = [
[1, 1, 1],
[-1, -1, -1],
[0, 0, 0]
];
function checkStatus(team){
// Check vertical wins
for (var i = 0; i < 3; i++) {
if (map[i][0] == team && map[i][1] == team && map[i][2] == team) {
@sunmockyang
sunmockyang / modify_mtime_to_filename.rb
Last active July 25, 2018 05:25
Parse android picture filenames to get the creation time and set the mtime of the file
# Parse android picture filenames to get the creation time and set the mtime of the file
# Example: 20180721_230439.jpg's mtime will be set to 2018-07-21 23:04:39 -0400
require 'date'
require "FileUtils"
dir = ARGV[0]
def get_mtime_from_filepath(path)
filename = File.basename(path, ".*")
@sunmockyang
sunmockyang / compare_file_structure.rb
Created July 24, 2018 04:49
Quickly find differences between two folders that should be the same
# Only compares file names, not the file contents
dir1 = ARGV[0]
dir2 = ARGV[1]
def get_relative_path_of_contents(dir)
return Dir["#{dir}/**/*"].select{|f| File.file?(f)}.map { |e|
e[dir] = ""
e
}
@sunmockyang
sunmockyang / safe_download.rb
Last active September 4, 2017 04:03
Download to a temp directory before moving file to destination
def download_temp_file(url, file_path)
temp_folder = Dir.tmpdir()
temp_file_path = "#{temp_folder}/#{File.basename(file_path)}"
File.open(temp_file_path, "wb") do |file|
file.write open(url).read
end
return temp_file_path
end
@sunmockyang
sunmockyang / nightShot.sh
Created March 9, 2017 19:50
Raspberry pi long exposure shot
raspistill -q 100 -t 1 -ss 6000000 -sh 0 -co 50 -br 50 -sa 0 -ev +2 -ex night -awb auto -ISO 800 -mm average -n -o "/media/OSXSUNNY/timelapse/singleshot/$(date +"%Y-%m-%d_%H%M%S").jpg"
@sunmockyang
sunmockyang / checkIfBashScriptRunning.sh
Created February 17, 2017 18:07
Check if bash script is running in background. Good to get the pid of bash script too.
if pidof -x "abc.sh" >/dev/null; then
echo "Process already running"
fi
@sunmockyang
sunmockyang / customTimelapse.sh
Last active November 2, 2016 19:44
Start the time-lapse at the sunrise of each day. Run timelapseSunrise.sh
#!/bin/sh
WIDTH=1920
HEIGHT=1080
NUM_FRAMES=$1
FRAME_INTERVAL=$2
DIR=$3
DATE=$(date +"%Y-%m-%d_%H%M%S")
if [ ! -n "$DIR" ]; then
@sunmockyang
sunmockyang / get_sunrise_time.rb
Created November 2, 2016 16:21
Outputs today's sunrise time
require "JSON"
require 'net/http'
require 'uri'
def open(url)
Net::HTTP.get(URI.parse(url))
end
# Put in your own latitude and longitude
puts JSON.parse(open('http://api.sunrise-sunset.org/json?lat=45.4215000&lng=-75.6972000&formatted=0&date=today'))["results"]["sunrise"]
@sunmockyang
sunmockyang / singleshot.sh
Last active October 11, 2016 21:31
Single shot raspistill
#!/bin/sh
WIDTH=1920
HEIGHT=1080
DIR="~/"
mkdir -p $DIR
FILE_NAME="${DIR}$(date +"%Y-%m-%d_%H%M%S").jpg"