Skip to content

Instantly share code, notes, and snippets.

@andy318
andy318 / gist:7032511
Created October 17, 2013 21:28
Rename TV Episode video files using data from the TVDB api and add the season and episode number to the file name. For this to work the name of the file should contain the episode name in it.
require 'xmlsimple'
# XML file was retrieved using TVDB api
doc = XmlSimple.xml_in('/Users/xyz/smurfs.xml')
doc['Episode'].each do |episode_hash|
# Find the next valid episode (with a season and episode number)
if episode_hash['SeasonNumber'][0].class == String && episode_hash['SeasonNumber'][0].to_i == 5
episode_season = episode_hash['SeasonNumber'][0].rjust(2, "0")
episode_num = episode_hash['EpisodeNumber'][0].rjust(2, "0")
@andy318
andy318 / gist:7032415
Created October 17, 2013 21:23
Find a set of records in the db and update their contents based on JSON data retrieved from a web service
require 'rubygems'
require 'json'
require 'open-uri'
require 'active_support'
require 'net/http'
def save_video_metadata
# x = ZencoderJob.find_by_sql("select Z.* from zencoder_jobs as Z inner join items as I on I.id = Z.item_id where I.file_content_type like 'video%' and Z.status = 'finished'")
# x.each do |job|
ZencoderJob.where(status:"finished").joins(:item).where("items.file_content_type LIKE 'video%'").find_each do |job|
@andy318
andy318 / gist:5461940
Created April 25, 2013 18:26
Read the lines of a csv file and extract a certain field and list the unique values in that field and sort that list based on how many times a value is repeated
cdrs = File.readlines ARGV[0]
cdrs.shift 1 # Delete the header row
cdrs.map! { |x| x.split(',')[3] } # Keep only the callid puts
duplicate_callids = cdrs.group_by(&:freeze). # Group by occurence
sort_by { |k, v| v.length}.reverse. # Sort by number of occurences desc
map { |x| "#{x[0]} - #{x[1].length}" if x[1].length > 1 }. # Format for printing compact
@andy318
andy318 / AddLeadingZeroToSongFile.rb
Created January 10, 2012 05:14
Ruby shell code - Find song files that have a single digit track number and prefix 0 to it
#!/usr/bin/env ruby
# Tested in REE 1.8.7. To run from command line, type -
# ruby AddLeadingZeroToSongFile.rb
`ls -1 *.mp3`.split("\n").each do | songfile |
x = /^(\d .*)/.match(songfile)
`mv "#{x.captures[0]}" "0#{x.captures[0]}"` if x
end
@andy318
andy318 / cards.rb
Created May 7, 2011 01:57
Create a deck of cards and return 13 of them at random
cards = [*'2'..'10', 'J', 'Q', 'K', 'A']
suits = ['C', 'D', 'H', 'S']
deck = cards.product(suits).map(&:join)
hand = deck.shuffle.each_slice(13).first