Advertisement
Guest User

Untitled

a guest
May 3rd, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.98 KB | None | 0 0
  1. #!/usr/bin/env ruby
  2.  
  3. # Create by Daniel �kerud 2008-04-20. Free shit, do whatever you want with it.
  4.  
  5. require 'rubygems'
  6. require 'mysql'
  7. require 'date'
  8.  
  9. # change these to suit your needs
  10. $host = "localhost"
  11. $db = "mythconverg"
  12. $user = "mythtv"
  13. $pass = "mypassword"
  14. $dir_recordings = "/home/da/storage/.recordings"
  15. $dir_reclinks = "/home/da/storage/recordings"
  16.  
  17. # create database connection
  18. my = Mysql::new($host, $user, $pass, $db)
  19.  
  20. # create a query to get information for all recordings from database
  21. query = <<END
  22. SELECT recorded.basename, channel.callsign, recorded.title, recorded.subtitle, recorded.progstart
  23. FROM recorded, channel
  24. WHERE recorded.chanid = channel.chanid
  25. ORDER BY recorded.starttime
  26. END
  27.  
  28. res = my.query(query)
  29.  
  30. links = []
  31. dups = Hash.new(0)
  32.  
  33. # Generate link-names (will create later)
  34. res.each do |row|
  35.  
  36. basename = row[0]
  37. callsign = row[1]
  38. title = row[2]
  39. subtitle = row[3]
  40. date = Date.parse(row[4])
  41.  
  42. ext = File.extname(basename)
  43. filename_old = File.join($dir_recordings, basename)
  44. # TV4.0413.Andra Avenyn (Del 2 av 2) (i).mpg
  45. subt = nil
  46. subt = "(#{subtitle})" if !subtitle.nil? && !subtitle.empty?
  47. filename_new = nil
  48. dup = 0
  49. begin
  50. dup_text = nil
  51. dup_text = ".#{dup}" if dup > 0
  52. filename_new = "#{callsign}.#{date.strftime("%m%d")}.#{title}#{subt}#{dup_text}#{ext}"
  53. dup += 1
  54. end while(dups[filename_new] > 0)
  55. dups[filename_new] += 1
  56. filename_new = File.join($dir_reclinks, filename_new)
  57. links << [filename_old, filename_new]
  58. end
  59.  
  60. # Remove old links
  61. d = Dir.new($dir_reclinks)
  62. d.each do |f|
  63. next if f[0] == ?.
  64. f = File.join($dir_reclinks, f)
  65. is_link = File.symlink?(f)
  66. if is_link
  67. File.unlink(f)
  68. puts "Removed existing link: #{f}"
  69. end
  70. end
  71.  
  72. # Create new links
  73. links.each do |fold, fnew|
  74. begin
  75. File.symlink(fold, fnew)
  76. puts "Created link: #{fnew} => #{fold}"
  77. rescue Errno::EEXIST
  78. puts "link '#{fnew}' already exists"
  79. end
  80. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement