Advertisement
Guest User

Untitled

a guest
Apr 14th, 2016
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.63 KB | None | 0 0
  1. #!/usr/bin/env ruby
  2.  
  3. require "open3"
  4. require "whatcd"
  5. require "htmlentities"
  6.  
  7. # -- edit below --
  8. username = ""
  9. password = ""
  10.  
  11. flacdir = "/home/costralia/files/"
  12. outdir = "/home/costralia/files/"
  13. watchdir = "/home/costralia/watch/"
  14. whatmp3 = "/home/costralia/files/whatmp3.py"
  15.  
  16. bitrates = %w[320 V0 V2]
  17. # -- edit above --
  18.  
  19. if ARGV.empty? or ARGV.length > 1 or !ARGV.first.start_with? "https"
  20. abort "Usage: ./betterup.rb \"https://what.cd/torrents.php?torrentid=<torrent_id>\""
  21. end
  22.  
  23. class Client < WhatCD::Client
  24. def initialize(username = nil, password = nil)
  25. @connection = Faraday.new(url: "https://what.cd") do |builder|
  26. builder.request :multipart
  27. builder.request :url_encoded
  28. builder.use :cookie_jar
  29. builder.adapter Faraday.default_adapter
  30. end
  31.  
  32. unless username.nil? || password.nil?
  33. authenticate username, password
  34. end
  35. end
  36.  
  37. def authenticate(username, password)
  38. body = { :username => username, :password => password, :keeplogged => 1 }
  39. res = connection.post "/login.php", body
  40.  
  41. unless res["set-cookie"] && res["location"] == "index.php"
  42. raise WhatCD::AuthError
  43. end
  44. File.write('cookie.txt', res["set-cookie"].match('session.*').to_s)
  45. @authenticated = true
  46. end
  47.  
  48. def upload(payload)
  49. unless authenticated?
  50. raise WhatCD::AuthError
  51. end
  52.  
  53. res = connection.post "/upload.php", payload
  54.  
  55. unless res.status == 302 && res.headers["location"] =~ /torrents/
  56. raise WhatCD::APIError
  57. end
  58. end
  59. end
  60.  
  61. if File.exist? "cookie.txt"
  62. api = Client.new
  63. api.set_cookie File.read("cookie.txt")
  64. else
  65. api = Client.new username, password
  66. end
  67.  
  68. authkey = api.fetch(:index)["authkey"]
  69. torrent = api.fetch :torrent, id: ARGV.first.strip.split("=").last.to_i
  70. fpath = HTMLEntities.new.decode(torrent["torrent"]["filePath"])
  71. srcdir = "#{flacdir}/#{fpath}"
  72.  
  73. if torrent["torrent"]["encoding"] != "Lossless"
  74. abort "Abort! PL does not point to a 16bit FLAC."
  75. end
  76.  
  77. if fpath == ""
  78. abort "Skipping; in violation of r2.3.1, FLACs not enclosed in a folder. Report it!"
  79. end
  80.  
  81. unless File.directory? srcdir
  82. abort "FLAC not found in #{flacdir}; nothing to encode."
  83. end
  84.  
  85. torrent["torrent"]["fileList"].split("|||").each do |filename|
  86. pathlen = fpath.length + HTMLEntities.new.decode(filename).partition('{{{').first.length
  87. # if the source folder name did not contain "FLAC", account for whatmp3 adding additional
  88. # chars, e.g. " (V0)"
  89. if not fpath =~ /flac/i
  90. if bitrates.include? "320"
  91. pathlen += 6
  92. else
  93. pathlen += 5
  94. end
  95. end
  96. abort "The transcoded torrent would violate r2.3.12." if pathlen > 180
  97. end
  98.  
  99. def identity(torrent)
  100. %w(remasterTitle remasterYear remasterRecordLabel remasterCatalogueNumber media remastered).collect { |attrib| torrent[attrib] }
  101. end
  102.  
  103. source = identity(torrent["torrent"])
  104. groupid = torrent["group"]["id"]
  105. group = api.fetch :torrentgroup, id: groupid
  106.  
  107. group["torrents"].each do |torrent|
  108. bitrates.reject! { |bitrate| identity(torrent) == source and torrent["encoding"].include? bitrate }
  109. end
  110.  
  111. if bitrates.empty?
  112. abort "#{fpath}: All bitrates already exist; nothing to do."
  113. end
  114.  
  115. payload = {
  116. auth: authkey,
  117. format: "MP3",
  118. remaster_title: source[0],
  119. remaster_year: source[1],
  120. remaster_record_label: source[2],
  121. remaster_catalogue_number: source[3],
  122. media: source[4],
  123. groupid: groupid,
  124. submit: "true"
  125. }
  126.  
  127. if source[5] == true
  128. payload[:remaster] = "on"
  129. end
  130.  
  131. puts "#{fpath}:"
  132.  
  133. bitrates.each do |bitrate|
  134. print "Encoding #{bitrate} ... "
  135. Open3.popen3(*%W(#{whatmp3} --#{bitrate} --output=#{outdir} #{srcdir})) do |stdin, stdout, stderr, wait_thr|
  136. begin
  137. stdout.each { |line| print line }
  138. rescue Errno::EIO
  139. puts "Errno:EIO error, but this probably just means " +
  140. "that the process has finished giving output."
  141. end
  142. while line = stderr.gets
  143. if line.include?("ERROR while") || line.include?("Unsupported number of channels")
  144. Process.kill("TERM", wait_thr.pid)
  145. abort "Stopping; something went wrong: #{line}"
  146. end
  147. end
  148. puts "complete."
  149. end
  150. fpath =~ /flac/i \
  151. ? file = fpath.sub(/flac/i, bitrate) + ".torrent" \
  152. : file = fpath.sub(/$/, " (#{bitrate})") + ".torrent"
  153. bitrate << " (VBR)" if bitrate.start_with? "V" # nuance to oblige upload.php form
  154. payload[:file_input] = Faraday::UploadIO.new("#{file}", 'application/x-bittorrent')
  155. payload[:bitrate] = bitrate
  156. print "Uploading ... "
  157. api.upload payload
  158. puts "done."
  159. print "Moving #{file} to watchdir ... "
  160. system *%W(mv #{file} #{watchdir})
  161. puts "done."
  162. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement