Showing posts with label python. Show all posts
Showing posts with label python. Show all posts

Saturday, 1 January 2011

Right-click, view movie's IMDB page

This is coded for chromium but just the bit at the top for any other. Use it with nautilus-actions, or thunar's custom actions etc. It'll try and make sense of release names by cropping before the year.. obviously this will fail for films like 2012 ;)

Grab the script here

Tuesday, 9 November 2010

Quickly search several PDFs at once

Takes PDFs as arguments, and can be used with nautilus-actions or thunar's custom actions. The script uses grep and thus grep regex, but i've added easy conditional searching at the expense of some limitations regarding normal grep conditionals. You're presented with this when you run it, which just about covers things:

The search ignores case. Conditionals cannot be joined. Let me know if this is a problem.
Wildcards:
A single . represents any character, and an asterisk represents zero or more occurances of the preceeding character eg:
'c...h' will find catch, clash, cloth, coach etc.
'c.*h' will find caliph, cash, catch, cheesecloth etc.
Conditionals:
OR : '(this|that|the)' will show lines containing 'this', 'that', or 'the'.
AND : '(this&&that&&the)' will search for lines that contain 'this', 'that' and 'the'.
NOT : '(this!that)' will search for lines that contain 'this' but don't contain 'that'

The script can be downloaded here. You'll need pdftotext for this to work.

Saturday, 10 July 2010

Short term 'back in time' snapshots




If files stand a chance of getting corrupted (such as openoffice documents), or you run the risk of hacking away and accidentally breaking one part of your code, it can be nice to have a 'back in time' feature. This script is intended for short term snapshots. Add it to your nautilus-actions or whatever, and select the files or folders you want snapshots of and the time in minutes between each snapshot. 

It uses rsync to create a backup folder every x minutes containing your selected file(s), and 'cp -al' to create hardlinks to prior versions to keep disk space low. This means it'll only save new copies of the bits that change, but give you all available data for any given snapshot)

Perhaps ill create a GUI for it at some point, but for now its CLI only, so if you add it to nautilus-actions, make sure you specify that it's to be launched with a terminal, eg:
gnome-terminal -x python ~/scripts/short_term_sync.py

Click here to grab it, and don't forget to edit the 'dest', around line 27.

Merge and burst PDFs with nautilus-actions

Two scripts, one for merging and one for bursting (splitting up into single pages). Grab them here.

They output to the directory the PDFs are in. The merge script collates the PDFs in alphabetical order and outputs to 'merged.pdf', the burst script numbers the pages.

Add it to nautilus-actions the normal way, with %M as the parameter and to only show when *.pdf is displayed, obviously.

These scripts rely on pdftk to do the work.

Friday, 7 May 2010

Update to Deluge scheduling script

This is a little update. The original post has been updated too.

So the syntax has changed for versions of deluge including and above 1.2.0. There's also a handy scheduler plugin again, finally, but i also use my deluge_sched.py for toggling between 3 sets of bandwidth limits, so its still sitting in my gnome menu next to deluge client.

The only difference is the syntax used for deluge-client. Since you may have changed your bandwidth limits to ones different from mine, you may want to just do a find-and-replace on the script instead of downloading it afresh.
Change all instances of: 'deluge -u console -a'
To: 'deluge-console'

Simple as that. The updated script can be found here and the original one can now be found here

Thursday, 15 April 2010

Batch renaming script updated

I added some command line arguments so you can easily use it directly from the terminal. You can navigate to the directory where your files are and use the argument * to select all the files in the current working directory, or use the -c argument to add conditionals (boolean 'not', 'and', 'or') that'll apply a filter on the current working directory, eg:

renamer.py -c 'foo'
will select all files with 'foo' in their name

renamer.py -c 'foo' and not 'bar'
will select all files with 'foo' in their name, except for those with 'bar'.

Click here to get it

Tuesday, 13 April 2010

Batch renamer using python string methods

I'll admit it: I'm crap at regex. I find python's string manipulation a lot more intuitive, and I'm finding this script a lot quicker to work with than any GUI renaming tools or the bash shell, too.

Add it to your nautilus or thunar action menu and work with the filenames like you've put them all in a python list and are writing the for loop expression


You have a few variables available to you: the filename, it's basename, it's extention and the element's index within the list. It lets you do most things to strings that python lets you do since it passes your command directly to python. The only limitation is that it need to be a one-liner, and it needs to return a string. Click here to familiarise yourself with python's string methods.

It'll let you confirm before applying changes, and reset if you tell it not to confirm.



Here are some examples from the help section if you're new to python:

n = b.replace('foo', 'bar')+e
Renames feefoobar.txt to feebarbar.txt
Replaces foo with bar in basename, and appends the extention

n = n.split('foo')[0]+'.log'
Renames feefoobar.txt to fee.log
Splits the filename at 'foo' and returns the part before the split, and appends a .log extention

n = b+str(i)+e
Renames feefoobar.txt to feefoobar0.txt
Adds the current element's index integer as a string to the end of the basename, then appends the extention


Click here to get it

Edit: This has been updated 

Tuesday, 16 March 2010

Limit Deluge based on time of day, command arguments or a toggle button

I live with my girlfriend and share the apartment with two others, so I can't use all of the bandwidth all of the time. I also work from home, and like to limit my torrent sharing if I'm doing something that requires more bandwidth, so I made this script.

It runs at startup and at 7.10pm on week days. If my computer is turned on in the evening, it limits the bandwidth deluge uses so others in the apartment can do what they like without me clogging things up. This works better than a normal cron job would since I may not have my computer turned on when the cron job is scheduled to run.

I also run this command via a menu entry, with the argument 'toggle', so i can cycle through the three bandwidth settings when i need to.



For deluge versions >= 1.2.0, the script is available here. For pre 1.2.0, the script is available here. See update post for details.

Change the config values at the top of the script to ones that are suitable for you. The script sends a notification via send-notify, and also logs actions to /var/log/messages.


Because it also accepts the arguments 'limit', 'partial' and 'unlimit', it can also be run from the command line, perhaps via ssh if you run deluge on your server or if you want to create buttons for every setting, rather than rely on the 'toggle' functionality.

Tuesday, 10 February 2009

DVD authoring with one click

I have this set up with nautilus-actions so i can select 1, 2 or 3 video files and it will automatically make a dvd.iso for me. You select:

1 video file : A dvd is created with no menu.

2 video files (containing cd1 & cd1 in their filename) : Both files are joined and dvd is created with no menu.

2 video files (not containing cd1 & cd2) : The script assumes they are episodes or individual films and creates a dvd with a menu.

3 video files : The script creates a dvd with a menu for all 3 files.

Requirements:
ffmpeg, dvdauthor, tovid scripts, mkisofs

I've got my dvd3 script in my path so the target I use for nautilus-actions is: gnome-terminal -x dvd3

Here's the script, and here's the dvd menu background image I use. Be sure to specify it's path near the top of the script. You'll also want to change the ffmpeg option -target pal-dvd on line 82 to -target ntsc-dvd if you live in that part of the world.

Thursday, 9 October 2008

automatically add cd2 to playlist if present

This will , as the name suggests, add movie cd1.avi and movie cd2.avi to a playlist, if its present. This means there's no messing around in the middle of a film if your films are encoded to fit on two 700mb cds for example; it'll just play the 2nd part right after the first.

It'll only work if the first filename contains cd1 or CD1. I put this script in my /usr/bin folder and mark it as executible, then just using nautilus i set my video files to open with it instead of with a normal video player. If the filename doesn't contain cd1 or CD1, it'll just play the file normally. This is set up for gmplayer, but it works fine in xine (the -idx flag can be removed if using xine or a xine-based player)

#! /usr/bin/python

import sys, os

s = sys.argv[1]
cd2 = s.strip().replace('cd1', 'cd2')
cd2 = t.strip().replace('CD1', 'CD2')
if s == cd2:
os.system('''gmplayer "''' + s +'''"''')
else:
os.system('''gmplayer -idx "''' + s + '''" "''' + cd2+'''"''')

Wednesday, 8 October 2008

play from rar archive


Another right-click menu entry I've set up in nautilus-actions is a 'play from rar' button. Now, for some irritating reason, i cant get it to pass anything beyond the parameter %M, so i had to put it in a script. It doesn't really need to be a python script, but mine is python anyway because i like messing with python, no matter how trivial the task at hand may be ;)

The other reason it's in python is because it's part of a movie catalogue program im writing, much like a lot of my other scripts.

All this script really does is execute the command:
unrar p -inul ARCHIVE.rar |mplayer -fs -noidx -

I use mplayer, and i like it to come up fullscreen, hence the -fs flag. -noidx is needed because the file is essentially being streamed from the rar archive. Xine and VLC should probably work just as well with the right setting flags.

#! /usr/bin/python

import sys, os

video = sys.argv[1]
os.system("unrar p -inul " + video + "|mplayer -fs -noidx -")

right-click, view movie's imdb page

I've been using this for a while now. If like me, you have a large proportion of your films legally backed up on your hard drive but dont deal with a media centre app, you want a nice quick method for looking up the plot synopsys, rating, or review on imdb.com. Well this is the script i use:

#! /usr/bin/python

import sys, os, os.path

myPath = sys.argv[1]
(dirName, fileName) = os.path.split(myPath)
(fileBaseName, fileExtension)=os.path.splitext(fileName)
t = fileBaseName.lower()
t = t.strip().replace("'", "")
t = t.strip().replace('cd1', '')
t = t.strip().replace('cd2', '')
t = t.strip().replace('CD1', '')
t = t.strip().replace('CD2', '')
t = t.strip().replace('.', ' ')
t = t.strip().replace('-', ' ')
t = t.strip().replace('_', ' ')
t = t.split('x264')
t = t[0].split('xvid')
t = t[0].split('divx')
t = t[0].split('unrated')
t = t[0].split('dvdr')
t = t[0].split('dvdrip') 
t = t[0].split(' cam ')
t = t[0].split(' ts ')
t = t[0].split(' scr ')
t = t[0].split('screener')
t = t[0].split('dvdscr')
t = t[0].split('r5')
t = t[0].split('limited')
t = t[0].split('internal')
t = t[0]
os.system('''firefox "google.com/search?btnI=1&q='''+ t + ''' imdb"''')

It's located at ~/scripts/imdb but you can put it anywhere of course. I create an entry on the right-click menu using nautilus-actions but if you don't use gnome you'll have to use an alternative method. This screencap should illustrate how I've set up nautilus-actions: