Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env ruby
- # Create by Daniel �kerud 2008-04-20. Free shit, do whatever you want with it.
- require 'rubygems'
- require 'mysql'
- require 'date'
- # change these to suit your needs
- $host = "localhost"
- $db = "mythconverg"
- $user = "mythtv"
- $pass = "mypassword"
- $dir_recordings = "/home/da/storage/.recordings"
- $dir_reclinks = "/home/da/storage/recordings"
- # create database connection
- my = Mysql::new($host, $user, $pass, $db)
- # create a query to get information for all recordings from database
- query = <<END
- SELECT recorded.basename, channel.callsign, recorded.title, recorded.subtitle, recorded.progstart
- FROM recorded, channel
- WHERE recorded.chanid = channel.chanid
- ORDER BY recorded.starttime
- END
- res = my.query(query)
- links = []
- dups = Hash.new(0)
- # Generate link-names (will create later)
- res.each do |row|
- basename = row[0]
- callsign = row[1]
- title = row[2]
- subtitle = row[3]
- date = Date.parse(row[4])
- ext = File.extname(basename)
- filename_old = File.join($dir_recordings, basename)
- # TV4.0413.Andra Avenyn (Del 2 av 2) (i).mpg
- subt = nil
- subt = "(#{subtitle})" if !subtitle.nil? && !subtitle.empty?
- filename_new = nil
- dup = 0
- begin
- dup_text = nil
- dup_text = ".#{dup}" if dup > 0
- filename_new = "#{callsign}.#{date.strftime("%m%d")}.#{title}#{subt}#{dup_text}#{ext}"
- dup += 1
- end while(dups[filename_new] > 0)
- dups[filename_new] += 1
- filename_new = File.join($dir_reclinks, filename_new)
- links << [filename_old, filename_new]
- end
- # Remove old links
- d = Dir.new($dir_reclinks)
- d.each do |f|
- next if f[0] == ?.
- f = File.join($dir_reclinks, f)
- is_link = File.symlink?(f)
- if is_link
- File.unlink(f)
- puts "Removed existing link: #{f}"
- end
- end
- # Create new links
- links.each do |fold, fnew|
- begin
- File.symlink(fold, fnew)
- puts "Created link: #{fnew} => #{fold}"
- rescue Errno::EEXIST
- puts "link '#{fnew}' already exists"
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement