Tuesday, October 28, 2008
mediawiki live preview
just recently i wanted to have live preview for my school notes wiki. i looked around for a while on the internet and finally found this. It almost gives live editing. What it does give is an instant preview under the edit box. That wasn't good enough for me so I changed a few lines, added a few others(in total <20) and now I have live preview for the wiki. Still wasn't good enough because it erases the preview notice(the "this is only a preview, changes hav enot been saved" notice). I modified the mediawiki edit page template a little, I just added a div that comes right after the preview note and ends just before the edit box. it works perfectly!
Friday, October 24, 2008
TV Show Episodes
Update: There was a few bugs in the script as is. The contents of the script have been replaced in this post. It will now rotate monthly as well. (I hadn't actually run the script before posting it although I had traced through it and everything had looked OK but when run, it didn't work hence the new version)
I watch several shows right now and I download each and every one of them(they are all published online for everyone to watch). I've always had a mess of a download folder because the standard naming schemes are easy to parse but aren't that fun to read(to me anyways). I'd like to see
($seasonis the season number, $episode is the episode number)
as my folder hierarchy for these shows. I've done it using a mix of java, regular expressions, and bash scripts. The java parses the regular expressions to see if a file matches and it also retrieves the backreferences for the show name, season, and episode.
I've got that done and I've been running it for a few weeks now. It works nicely except I lose track of what I've downloaded for the week. I decided to log what the script does for each file. That worked out OK, but I wanted to go one step further: I wanted a link to the episodes that I've downloaded in the log directory. I lied about *one* step further: I also wanted logs consolidated every week and any unwatched episodes brought to the root directory so I know there's some to watch. After I watch an episode, the link gets removed.
At the end of every week, the script goes through the week's logs and if there's any unwatched episodes, it moves the link to the root of the logs folder and places a week log beside it.
First, the directory structure:
TV Shows/Show Name - each episode is placed according to the scheme above
TV Shows/Incoming - the script resides here, where each show gets transferred to after downloaded
TV Shows/Logs - the log folder
TV Shows/Logs/Month, Week x/ - a week's log folder where x is the week number IN THE CURRENT MONTH, not year. It contains all the days' logs
TV Shows/Logs/Month, Week x/Day y (z) - a day's log folder where x is the week, y is the day number(0-6), and z is the day name(ie Monday)
TV Shows/Logs/Month, Week x/Day y (z)/Log.txt - the day's log
TV Shows/Logs/Month, Week x/Day y (z)/a b.c.avi - a link to a tv show such that "a b.c.avi" points to TV Shows/a/Season b/Episode c.avi" where a is the show name, b is the season number, and c is the episode number
TV Shows/Incoming/Links - a script called by the main script links all files for the day into this folder
~/bin/do_move - a script, called by find, that parses each filename down to it's components and creates a script that will be executed by the main script. It sets up a temporary log for the session, sets up the move operation for the episode, and sets up the linking to TV Shows/Links
~/bin/getregep - a script that calls a java program(kinda slow and not needed. I don't know how to do it a quicker way although I'm not exactly moving a ton of files :) ). It can test a regexp against a string to see if it matches and it can return the given backreference.
TV Shows/Incoming/Single.sh - the main script that calls all other ones.
contents of Singls.sh
contsnts of ~/bin/do_move
My regular expression for parsing a show where the filename is like any of the following:
the delimiter between Name and the season could be one of
Anyways, the regular expression is
Backref 3 is the season number
Backref 4 is the episode number
Note: get_name, get_season, get_ep are small scripts I wrote that call getregexp with the regexp above and getting the respective backrefs(see list above).
Contents of get_name:
If I get a bunch of episodes in a particular series and they all have the episode name in the filename, I write another regexp and I write get_epn to match to get the backref.
All shows get organized into the incoming directory's parent (ie Incoming is a subdirectory of the same directory where the tv shows will be stored)
I think I'm done with the scripts now.
Instructions for use(a directory, X, is the incoming directory and a file, E is an episode):
1) Download your episodes to a directory, say Y. Y cannot be in X or be X.
2) Move the episodes from Y to X.
3) Open Terminal and change directory to X.
4) Execute Single.sh "E" for each episode, E.
5) Watch episode
The episode will now be in TV Shows/Name/Season/Episode.avi
There will be a link in "Logs/CurrentMonth, Week CurrentWeekNumber/Day CurrentDayNumber (CurrentDayName)" to the episode you execute Single.sh on.
There will be a file ""Logs/CurrentMonth, Week CurrentWeekNumber/Day CurrentDayNumber (CurrentDayName)/Log.txt" that contains a list of episodes managed during that day.
6) After you watch an episode, remove the link from "Logs/CurrentMonth, Week CurrentWeekNumber/Day CurrentDayNumber (CurrentDayName)". This is very important and enables log management.
Every Saturday, after the script is run for the first time that day, All episodes for the week that still have links will have their links moved to "Logs/". The Log.txt in "Logs/CurrentMonth, Week CurrentWeekNumber/Day CurrentDayNumber (CurrentDayName)" will be appended to "Logs/CurrentMonth, Week CurrentWeekNumber.txt"(will be created if it doesn't exist) and the entire week's directory will be removed. If you haven't run the script in over a week, create a file Force.m with the month in which the script was last run. Create a file Force.w with the week in which the script was last run during that month. Then create a file ASDFG.S01E01.HDTV.XviD-FORCE.avi and run Single.sh with that file.
An example: Say I have downloaded a show, let's call it Ajax and it's episode 3 from season 2, and I'm ready to watch it. I open terminal and change to Incoming. I run "./Single.sh Ajax.S03E02.HDTV.XviD-ME.avi". Suppose we do this today(October 24th). If I browse to "Logs/October, Week 2/Day 5 (Friday)" I see a link called "Ajax 2.3.avi"(There's also a file, Log.txt). I open that link and watch my episode. After I finish, I remove the link.
If I haven't been keeping up with the shows during the week, on Saturday, all the links will be moved to "Logs/" along with the combined log for the week. The Logs tree will be pruned of the empty Week directory as well.
Workflow: Download Show, Run Single.sh on it, Watch show, Delete link. Make sure to run it on something every saturday(using ASDFG.S01E01.HDTV.XviD-FORCE.avi as a show, it won't get into your TV Shows directory).
After several weeks, I've found this to be the most useful way to keep track of the episodes I've watched versus the ones I haven't.
I now have the most organized tv show directory I can think of.
Any questions, comments, complaints in the comments please.
I watch several shows right now and I download each and every one of them(they are all published online for everyone to watch). I've always had a mess of a download folder because the standard naming schemes are easy to parse but aren't that fun to read(to me anyways). I'd like to see
Show Name/Season $season/Episode $episode($seasonis the season number, $episode is the episode number)
as my folder hierarchy for these shows. I've done it using a mix of java, regular expressions, and bash scripts. The java parses the regular expressions to see if a file matches and it also retrieves the backreferences for the show name, season, and episode.
I've got that done and I've been running it for a few weeks now. It works nicely except I lose track of what I've downloaded for the week. I decided to log what the script does for each file. That worked out OK, but I wanted to go one step further: I wanted a link to the episodes that I've downloaded in the log directory. I lied about *one* step further: I also wanted logs consolidated every week and any unwatched episodes brought to the root directory so I know there's some to watch. After I watch an episode, the link gets removed.
At the end of every week, the script goes through the week's logs and if there's any unwatched episodes, it moves the link to the root of the logs folder and places a week log beside it.
First, the directory structure:
TV Shows/Show Name - each episode is placed according to the scheme above
TV Shows/Incoming - the script resides here, where each show gets transferred to after downloaded
TV Shows/Logs - the log folder
TV Shows/Logs/Month, Week x/ - a week's log folder where x is the week number IN THE CURRENT MONTH, not year. It contains all the days' logs
TV Shows/Logs/Month, Week x/Day y (z) - a day's log folder where x is the week, y is the day number(0-6), and z is the day name(ie Monday)
TV Shows/Logs/Month, Week x/Day y (z)/Log.txt - the day's log
TV Shows/Logs/Month, Week x/Day y (z)/a b.c.avi - a link to a tv show such that "a b.c.avi" points to TV Shows/a/Season b/Episode c.avi" where a is the show name, b is the season number, and c is the episode number
TV Shows/Incoming/Links - a script called by the main script links all files for the day into this folder
~/bin/do_move - a script, called by find, that parses each filename down to it's components and creates a script that will be executed by the main script. It sets up a temporary log for the session, sets up the move operation for the episode, and sets up the linking to TV Shows/Links
~/bin/getregep - a script that calls a java program(kinda slow and not needed. I don't know how to do it a quicker way although I'm not exactly moving a ton of files :) ). It can test a regexp against a string to see if it matches and it can return the given backreference.
TV Shows/Incoming/Single.sh - the main script that calls all other ones.
contents of Singls.sh
#!/bin/bash
export PATH=$PATH:/Users/r0ssar00/bin
domove=do_move
incoming="/Volumes/DATA/TORRENTS/TV Shows/Incoming"
cd "$incoming"
if [ "$1" ]; then
if [ "$1" != "ASDFG.S01E01.HDTV.XviD-FORCE.avi" ]; then
# moving a particular file
fname="`basename "$1"`";
$domove "$fname";
else
FAKE="1";
fi;
else
echo "Only moving one..."
exit;
fi
DOMOVE="$incoming/domove.sh"
if [ "$FAKE" != "1" ]; then
sh "$DOMOVE";
fi
month="`date "+%B"`"
week="$((`date "+%U"`-(`date "+%m"`*4)))"
day="`date "+%w"`"
dayn="`date "+%A"`"
tfile="$month/Week $week"
if [ "$FAKE" != "1" ]; then
mkdir -p "Logs/$tfile/Day $day ($dayn)"
cat completed.show >>"Logs/$tfile/Day $day ($dayn)/Log.txt"
mv Links/* "Logs/$tfile/Day $day ($dayn)/"
rm completed.show
rm "$DOMOVE"
fi
# rotate all logs for the week into a Week folder
# if you haven't run the script regularly, create a file called Force.m in Incoming. It must contain the month in which the script was last run.
# Also, create Force.w which contains the week in which the script was last run
# do this for each week that you haven't run the script
if [ -f ./Force.m ]; then
day="6";
tfile="`cat Force.m`/Week `cat Force.w`";
rm ./Force.w ./Force.m;
rm ./ASDFG.S01E01.HDTV.XviD-FORCE.avi;
fi
function weekly {
tfile="$1";
# first, store the days of the week in an array
days[0]="Sunday"
days[1]="Monday"
days[2]="Tuesday"
days[3]="Wednesday"
days[4]="Thursday"
days[5]="Friday"
days[6]="Saturday"
# if it's saturday
if [ "$day" = "6" ]; then
# move the logs
pushd "Logs/$tfile" >/dev/null;
if [ $? != 0 ]; then
return 1;
fi;
FILES="`find . -type f -iname "*.avi"`";
if [ "$FILES" = "" ]; then
# no episodes left for this week, archive the logs
for i in 0 1 2 3 4 5 6; do
tday="${days[$i]}";
pushd "Logs/$tfile/Day $i ($tday)" >/dev/null;
if [ $? = 0 ]; then
echo "************ ($tday) ************" >day.note;
cat day.note Log.txt >> "$incoming/Logs/$month, Week $week.txt";
rm Log.txt;
rm .DS_Store >/dev/null 2>/dev/null;
rm day.note;
fi;
popd >/dev/null;
done;
else
# there's eps left, move to root Logs folder
find . -type f -iname "*.avi" -exec mv "{}" "$incoming/Logs/" ';' ;
# still need to move the logs into a central week log
for i in 0 1 2 3 4 5 6; do
tday="${days[$i]}";
pushd "ls Logs/$tfile/Day $i ($tday)" >/dev/null;
if [ $? = 0 ]; then
echo "************ ($tday) ************" >day.note;
cat day.note Log.txt >> "$incoming/Logs/$month, Week $week.txt";
rm Log.txt;
rm .DS_Store >/dev/null 2>/dev/null;
rm day.note;
fi;
popd >/dev/null;
done;
RMWEEK="1";
fi;
popd >/dev/null;
# remove all the empty directories in the month directory
perl -MFile::Find -e"finddepth(sub{rmdir},'.')";
if [ "$RMWEEK" = "1" ]; then
rm -r "Logs/$tfile";
fi
fi
}
function monthly {
# first go through each week and set tfile for each week
if [ "$week" = "3" ]; then
for i in 0 1 2 3; do
weekly "$month/Week $i";if [ -d "$month" ]; then
rm -r "$month";
fi; done;
fi;
}
weekly "$tfile";
montly;
You must change the lineincoming="/Volumes/DATA/TORRENTS/TV Shows/Incoming"to the location of your TV Shows completed files directory
contsnts of ~/bin/do_move
#!/bin/bash
#setup the regexp
rm ~/bin/regexp
ln -s ~/bin/regexp.normal ~/bin/regexp
# for each file, mkdir the show directory and season directory in that show
# then mv the file to show/season/ep.avi
i="";
for arg in $@
do
i="$i ${arg}"
done
echo $i
# get the filename
i="`basename $i`"
filename="$i";
echo $i;
fileext="`echo "$i" |awk -F . '{print $NF}'`"
if [ "$fileext" != "avi" ]; then
exit;
fi
echo " $filename";
showname="`get_name "$filename"`";
showname="`echo $showname | tr '.' ' ' | tr '_' ' '`";
season="`get_season "$filename"`";
ep="`get_ep "$filename"`";
# special cases go here. example: a particular show I watch is only recognized by Plex when the 'and' in the name is '&'
# end special cases
echo " Show: $showname";
echo " Season: $season";
echo " Episode: $ep";
reg="`cat ~/bin/regexp`";
if [ "`getregexp "$reg" "$@" "test"`"=="true" ]; then
echo "mkdir -p \"`pwd`/../$showname/Season $season\"" >>"`pwd`/domove.sh";
echo "mv \"$@\" \"`pwd`/../$showname/Season $season/Episode $ep.avi\"" >>"`pwd`/domove.sh";
echo "echo \"TV Show: $showname, Season $season, Episode $ep\" >>\"`pwd`/completed.show\"" >>"`pwd`/domove.sh";
echo "ln -s \"`pwd`/../$showname/Season $season/Episode $ep.avi\" \"`pwd`/Links/$showname $season.$ep.avi\"" >>"`pwd`/domove.sh"
else
echo "echo \"Other: $@\" >>\"`pwd`/completed.show\"" >>"`pwd`/domove.sh";
echo "mv \"$@\" \"`pwd`/../../Done/\"" >>"`pwd`/domove.sh"
fi
At one point, I needed several regular expressions(each is contained in a file in ~/bin/) so I just make regexp.normal my main expression and any special case gets it's own file and (slightly modified) do_move script.My regular expression for parsing a show where the filename is like any of the following:
Show.Name.S01E01.Quality.Codec-Release.avi
Show.Name.1x01.Quality.Codec-Release.avi
the delimiter between Name and the season could be one of
.
{space}
{space}-
-{space}
{space}-{space}
Anyways, the regular expression is
([A-Za-z\._ ]*)(\.| |\-|\- | \-| \- )[Ss]?0?([0-9])[Xx]?[Ee]?0?([0-9]?[0-9])(.*)Backreference 1 is the showname
Backref 3 is the season number
Backref 4 is the episode number
Note: get_name, get_season, get_ep are small scripts I wrote that call getregexp with the regexp above and getting the respective backrefs(see list above).
Contents of get_name:
#!/bin/bash regexp="`cat ~/bin/regexp`" echo "`getregexp "$regexp" "$1" "1"`"
If I get a bunch of episodes in a particular series and they all have the episode name in the filename, I write another regexp and I write get_epn to match to get the backref.
All shows get organized into the incoming directory's parent (ie Incoming is a subdirectory of the same directory where the tv shows will be stored)
I think I'm done with the scripts now.
Instructions for use(a directory, X, is the incoming directory and a file, E is an episode):
1) Download your episodes to a directory, say Y. Y cannot be in X or be X.
2) Move the episodes from Y to X.
3) Open Terminal and change directory to X.
4) Execute Single.sh "E" for each episode, E.
5) Watch episode
The episode will now be in TV Shows/Name/Season/Episode.avi
There will be a link in "Logs/CurrentMonth, Week CurrentWeekNumber/Day CurrentDayNumber (CurrentDayName)" to the episode you execute Single.sh on.
There will be a file ""Logs/CurrentMonth, Week CurrentWeekNumber/Day CurrentDayNumber (CurrentDayName)/Log.txt" that contains a list of episodes managed during that day.
6) After you watch an episode, remove the link from "Logs/CurrentMonth, Week CurrentWeekNumber/Day CurrentDayNumber (CurrentDayName)". This is very important and enables log management.
Every Saturday, after the script is run for the first time that day, All episodes for the week that still have links will have their links moved to "Logs/". The Log.txt in "Logs/CurrentMonth, Week CurrentWeekNumber/Day CurrentDayNumber (CurrentDayName)" will be appended to "Logs/CurrentMonth, Week CurrentWeekNumber.txt"(will be created if it doesn't exist) and the entire week's directory will be removed. If you haven't run the script in over a week, create a file Force.m with the month in which the script was last run. Create a file Force.w with the week in which the script was last run during that month. Then create a file ASDFG.S01E01.HDTV.XviD-FORCE.avi and run Single.sh with that file.
An example: Say I have downloaded a show, let's call it Ajax and it's episode 3 from season 2, and I'm ready to watch it. I open terminal and change to Incoming. I run "./Single.sh Ajax.S03E02.HDTV.XviD-ME.avi". Suppose we do this today(October 24th). If I browse to "Logs/October, Week 2/Day 5 (Friday)" I see a link called "Ajax 2.3.avi"(There's also a file, Log.txt). I open that link and watch my episode. After I finish, I remove the link.
If I haven't been keeping up with the shows during the week, on Saturday, all the links will be moved to "Logs/" along with the combined log for the week. The Logs tree will be pruned of the empty Week directory as well.
Workflow: Download Show, Run Single.sh on it, Watch show, Delete link. Make sure to run it on something every saturday(using ASDFG.S01E01.HDTV.XviD-FORCE.avi as a show, it won't get into your TV Shows directory).
After several weeks, I've found this to be the most useful way to keep track of the episodes I've watched versus the ones I haven't.
I now have the most organized tv show directory I can think of.
Any questions, comments, complaints in the comments please.
Thursday, October 9, 2008
lol
I just realized the irony of my current situation: I'm writing about the recommended solution in a report for school while listening to "There's No Solution" by Sum 41.
I suddenly wish I was saying this on IRC so that it could be thrown into bash.org. It sounds exactly like what would be on that site.
I suddenly wish I was saying this on IRC so that it could be thrown into bash.org. It sounds exactly like what would be on that site.
Subscribe to:
Posts (Atom)