Advertisement
fafhrd

gogaku2016.rb

Jun 18th, 2016
345
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.14 KB | None | 0 0
  1. #!bundle exec ruby
  2. # -*- coding: utf-8 -*-
  3. require 'open-uri'
  4. require 'openssl'
  5. require 'rexml/document'
  6. require 'rubygems'
  7. require 'taglib'
  8.  
  9. OP_NO_TLSv1_2 = 0x08000000
  10. OpenSSL::SSL::SSLContext::DEFAULT_PARAMS[:options] |= OP_NO_TLSv1_2
  11.  
  12. def make_path file
  13. "https://nhk-vh.akamaihd.net/i/gogaku-stream/mp4/#{file}/master.m3u8"
  14. end
  15.  
  16. def listdataxml(lang, course)
  17. "https://www2.nhk.or.jp/gogaku/st/xml/#{lang}/#{course}/listdataflv.xml"
  18. end
  19.  
  20. def download path, outfile
  21. system "ffmpeg -i #{path} -absf aac_adtstoasc -acodec copy #{outfile}"
  22. end
  23.  
  24. def proc_xml url
  25. open(url) do |f|
  26. doc = REXML::Document.new(f)
  27. doc.elements.each("musicdata/music") do |e|
  28. title = e.attributes["title"]
  29. hdate = e.attributes["hdate"]
  30. kouza = e.attributes["kouza"]
  31. code = e.attributes["code"]
  32. file = e.attributes["file"]
  33. path = make_path(file)
  34. outfile = "data/" + file.sub("mp4", "m4a")
  35. if FileTest.exist?(outfile) && FileTest.size?(outfile) >= 1000000
  36. puts "Skipped . . . #{outfile}"
  37. else
  38. puts "Downloading . . . #{outfile}"
  39. download path, outfile
  40. set_title outfile, title, hdate, kouza, code
  41. end
  42. end
  43. end
  44. end
  45.  
  46. def proc_url_list(list)
  47. list.each do |url|
  48. proc_xml url
  49. end
  50. end
  51.  
  52. def language(lang)
  53. listdataxml(lang, "kouza")
  54. end
  55.  
  56. def levelup(lang)
  57. listdataxml(lang, "levelup")
  58. end
  59.  
  60. def english(course)
  61. listdataxml("english", course)
  62. end
  63.  
  64. def set_title file, title, hdate, kouza, code
  65. TagLib::MP4::File.open(file) do |mp4|
  66. tag = mp4.tag
  67. tag.genre = "Education"
  68. tag.artist = 'NHK'
  69. tag.album = title
  70. tag.title = hdate
  71. tag.year = 2013
  72. tag.track = code.to_i
  73. tag.comment = code
  74. mp4.save
  75. end
  76. end
  77.  
  78.  
  79. list = [
  80. language("german"),
  81. language("french"),
  82. language("italian"),
  83. language("spanish"),
  84. language("russian"),
  85. language("chinese"),
  86. levelup("chinese"),
  87. language("hangeul"),
  88. levelup("hangeul"),
  89. english("business1"),
  90. english("business2"),
  91. english("yomu"),
  92. ]
  93.  
  94. # list = [ english("business1") ]
  95. # list = [ english("yomu") ]
  96.  
  97. proc_url_list list
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement