Skip to content

Instantly share code, notes, and snippets.

@m4tthumphrey
Last active December 26, 2015 10:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save m4tthumphrey/7137941 to your computer and use it in GitHub Desktop.
Save m4tthumphrey/7137941 to your computer and use it in GitHub Desktop.
<?php
function remove_dir($dir)
{
$files = array_diff(scandir($dir), array('.','..'));
foreach ($files as $file) {
(is_dir("$dir/$file")) ? remove_dir("$dir/$file") : unlink("$dir/$file");
}
return rmdir($dir);
}
function create_test_files($dir, $empty = true)
{
if ($empty) {
remove_dir($dir);
mkdir($dir);
}
for ($s = 1; $s <= 6; $s++) {
for ($e = 1; $e <= 24; $e++) {
touch($dir.'/'.sprintf('S%02sE%02s.mkv', $s, $e));
}
}
}
function rename_files($input, $output = null, $copy = true)
{
if (is_null($output) || !is_dir($output)) {
$output = $input;
}
$file = 'episodes.txt';
$seasons = array();
foreach (file($file) as $line) {
list ($name, $date, $numbers) = explode('|', trim($line));
list ($seq, $number) = explode(' - ', $numbers);
$season = substr($number, 0, 1);
$episode = substr($number, 1, 2);
$seasons[$season][$episode] = $name;
}
foreach ($seasons as $season => $episodes) {
foreach ($episodes as $number => $name) {
$episode_string = sprintf('S%02sE%02s', $season, $number);
$season_string = sprintf('Season %d', $season);
$file = $input.'/'.$episode_string.'.mkv';
if (file_exists($file)) {
$season_dir = $output.'/'.$season_string;
$new_name = preg_replace('/\s+/', '.', $name);
$new_name = preg_replace('/[^a-z0-9.]/i', '', $new_name);
if (!is_dir($season_dir)) {
mkdir($season_dir);
}
if ($copy) {
copy($file, $season_dir.'/'.$episode_string.'.'.$new_name.'.mkv');
} else {
rename($file, $season_dir.'/'.$episode_string.'.'.$new_name.'.mkv');
}
} else {
printf("%s.mkv could not be found...\n", $episode_string);
}
}
}
}
$dir = 'files';
if ($argv[1] == 'test') {
create_test_files($dir);
}
rename_files($dir, 'output');
Pilot|September 24, 2007|1 - 101
The Big Bran Hypothesis|October 1, 2007|2 - 102
The Fuzzy Boots Corollary|October 8, 2007|3 - 103
The Luminous Fish Effect|October 15, 2007|4 - 104
The Hamburger Postulate|October 22, 2007|5 - 105
The Middle Earth Paradigm|October 29, 2007|6 - 106
The Dumpling Paradox|November 5, 2007|7 - 107
The Grasshopper Experiment|November 12, 2007|8 - 108
The Cooper-Hofstadter Polarization|March 17, 2008|9 - 109
The Loobenfeld Decay|March 24, 2008|10 - 110
The Pancake Batter Anomaly|March 31, 2008|11 - 111
The Jerusalem Duality|April 14, 2008|12 - 112
The Bat Jar Conjecture|April 21, 2008|13 - 113
The Nerdvana Annihilation|April 28, 2008|14 - 114
The Porkchop Indeterminacy|May 5, 2008|15 - 115
The Peanut Reaction|May 12, 2008|16 - 116
The Tangerine Factor|May 19, 2008|17 - 117
The Bad Fish Paradigm|September 22, 2008|18 - 201
The Codpiece Topology|September 29, 2008|19 - 202
The Barbarian Sublimation|October 6, 2008|20 - 203
The Griffin Equivalency|October 13, 2008|21 - 204
The Euclid Alternative|October 20, 2008|22 - 205
The Cooper-Nowitzki Theorem|November 3, 2008|23 - 206
The Panty Piñata Polarization|November 10, 2008|24 - 207
The Lizard-Spock Expansion|November 17, 2008|25 - 208
The White Asparagus Triangulation|November 24, 2008|26 - 209
The Vartabedian Conundrum|December 8, 2008|27 - 210
The Bath Item Gift Hypothesis|December 15, 2008|28 - 211
The Killer Robot Instability|January 12, 2009|29 - 212
The Friendship Algorithm|January 19, 2009|30 - 213
The Financial Permeability|February 2, 2009|31 - 214
The Maternal Capacitance|February 9, 2009|32 - 215
The Cushion Saturation|March 2, 2009|33 - 216
The Terminator Decoupling|March 9, 2009|34 - 217
The Work Song Nanocluster|March 16, 2009|35 - 218
The Dead Hooker Juxtaposition|March 30, 2009|36 - 219
The Hofstadter Isotope|April 13, 2009|37 - 220
The Vegas Renormalization|April 27, 2009|38 - 221
The Classified Materials Turbulence|May 4, 2009|39 - 222
The Monopolar Expedition|May 11, 2009|40 - 223
The Electric Can Opener Fluctuation|September 21, 2009|41 - 301
The Jiminy Conjecture|September 28, 2009|42 - 302
The Gothowitz Deviation|October 5, 2009|43 - 303
The Pirate Solution|October 12, 2009|44 - 304
The Creepy Candy Coating Corollary|October 19, 2009|45 - 305
The Cornhusker Vortex|November 2, 2009|46 - 306
The Guitarist Amplification|November 9, 2009|47 - 307
The Adhesive Duck Deficiency|November 16, 2009|48 - 308
The Vengeance Formulation|November 23, 2009|49 - 309
The Gorilla Experiment|December 7, 2009|50 - 310
The Maternal Congruence|December 14, 2009|51 - 311
The Psychic Vortex|January 11, 2010|52 - 312
The Bozeman Reaction|January 18, 2010|53 - 313
The Einstein Approximation|February 1, 2010|54 - 314
The Large Hadron Collision|February 8, 2010|55 - 315
The Excelsior Acquisition|March 1, 2010|56 - 316
The Precious Fragmentation|March 8, 2010|57 - 317
The Pants Alternative|March 22, 2010|58 - 318
The Wheaton Recurrence|April 12, 2010|59 - 319
The Spaghetti Catalyst|May 3, 2010|60 - 320
The Plimpton Stimulation|May 10, 2010|61 - 321
The Staircase Implementation|May 17, 2010|62 - 322
The Lunar Excitation|May 24, 2010|63 - 323
The Robotic Manipulation|September 23, 2010|64 - 401
The Cruciferous Vegetable Amplification|September 30, 2010|65 - 402
The Zazzy Substitution|October 7, 2010|66 - 403
The Hot Troll Deviation|October 14, 2010|67 - 404
The Desperation Emanation|October 21, 2010|68 - 405
The Irish Pub Formulation|October 28, 2010|69 - 406
The Apology Insufficiency|November 4, 2010|70 - 407
The 21-Second Excitation|November 11, 2010|71 - 408
The Boyfriend Complexity|November 18, 2010|72 - 409
The Alien Parasite Hypothesis|December 9, 2010|73 - 410
The Justice League Recombination|December 16, 2010|74 - 411
The Bus Pants Utilization|January 6, 2011|75 - 412
The Love Car Displacement|January 20, 2011|76 - 413
The Thespian Catalyst|February 3, 2011|77 - 414
The Benefactor Factor|February 10, 2011|78 - 415
The Cohabitation Formulation|February 17, 2011|79 - 416
The Toast Derivation|February 24, 2011|80 - 417
The Prestidigitation Approximation|March 10, 2011|81 - 418
The Zarnecki Incursion|March 31, 2011|82 - 419
The Herb Garden Germination|April 7, 2011|83 - 420
The Agreement Dissection|April 28, 2011|84 - 421
The Wildebeest Implementation|May 5, 2011|85 - 422
The Engagement Reaction|May 12, 2011|86 - 423
The Roommate Transmogrification|May 19, 2011|87 - 424
The Skank Reflex Analysis|September 22, 2011|88 - 501
The Infestation Hypothesis|September 22, 2011|89 - 502
The Pulled Groin Extrapolation|September 29, 2011|90 - 503
The Wiggly Finger Catalyst|October 6, 2011|91 - 504
The Russian Rocket Reaction|October 13, 2011|92 - 505
The Rhinitis Revelation|October 20, 2011|93 - 506
The Good Guy Fluctuation|October 27, 2011|94 - 507
The Isolation Permutation|November 3, 2011|95 - 508
The Ornithophobia Diffusion|November 10, 2011|96 - 509
The Flaming Spittoon Acquisition|November 17, 2011|97 - 510
The Speckerman Recurrence|December 8, 2011|98 - 511
The Shiny Trinket Maneuver|January 12, 2012|99 - 512
The Recombination Hypothesis|January 19, 2012|100 - 513
The Beta Test Initiation|January 26, 2012|101 - 514
The Friendship Contraction|February 2, 2012|102 - 515
The Vacation Solution|February 9, 2012|103 - 516
The Rothman Disintegration|February 16, 2012|104 - 517
The Werewolf Transformation|February 23, 2012|105 - 518
The Weekend Vortex|March 8, 2012|106 - 519
The Transporter Malfunction|March 29, 2012|107 - 520
The Hawking Excitation|April 5, 2012|108 - 521
The Stag Convergence|April 26, 2012|109 - 522
The Launch Acceleration|May 3, 2012|110 - 523
The Countdown Reflection|May 10, 2012|111 - 524
The Date Night Variable|September 27, 2012|112 - 601
The Decoupling Fluctuation|October 4, 2012|113 - 602
The Higgs Boson Observation|October 11, 2012|114 - 603
The Re-Entry Minimization|October 18, 2012|115 - 604
The Holographic Excitation|October 25, 2012|116 - 605
The Extract Obliteration|November 1, 2012|117 - 606
The Habitation Configuration|November 8, 2012|118 - 607
The 43 Peculiarity|November 15, 2012|119 - 608
The Parking Lot Escalation|November 29, 2012|120 - 609
The Fish Guts Displacement|December 6, 2012|121 - 610
The Santa Simulation|December 13, 2012|122 - 611
The Egg Salad Equivalency|January 3, 2013|123 - 612
The Bakersfield Expedition|January 10, 2013|124 - 613
The Cooper/Kripke Inversion|January 31, 2013|125 - 614
The Spoiler Alert Segmentation |February 7, 2013|126 - 615
The Tangible Affection Proof|February 14, 2013|127 - 616
The Monster Isolation|February 21, 2013|128 - 617
The Contractual Obligation Implementation|March 7, 2013|129 - 618
The Closet Reconfiguration|March 14, 2013|130 - 619
The Tenure Turbulence|April 4, 2013|131 - 620
The Closure Alternative|April 25, 2013|132 - 621
The Proton Resurgence|May 2, 2013|133 - 622
The Love Spell Potential|May 9, 2013|134 - 623
The Bon Voyage Reaction|May 16, 2013|135 - 624
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment