Guest User

Untitled

a guest
Jul 30th, 2016
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.61 KB | None | 0 0
  1. class Iplayer_data < Array
  2. #for iPlayer API documentation, see http://mermade.github.io/swagger/index.html?url=https://raw.githubusercontent.com/Mermade/bbcparse/master/iblApi/swagger.json#!/Search/Search
  3. require 'httpclient' #I had difficulty installing a cUrl gem on windows, so I used this instead
  4. require 'JSON'
  5. attr_reader :categories, :regions
  6.  
  7. def initialize
  8. @http = HTTPClient.new
  9. @lang = "en"
  10. @rights = "web"
  11. @version = "v1"
  12. # load categories and regions straight away, for future use, and so we have a local copy of the data
  13. @categories = load_standing_data("categories")
  14. @regions = load_standing_data("regions")
  15. end
  16.  
  17. def i_search(query)
  18. url = "http://ibl.api.bbci.co.uk/ibl/#{@version}/search?q="
  19. url += URI.escape(query)
  20. url += "&lang=#{@en}&rights=#{@rights}&availability=available"
  21. get_http_content(url)["search"]["results"].each do |c|
  22. #don't bring back all the attributes. Basically just a test to see if I could do it
  23. #plus it's less data to display, which helps with testing.
  24. #there's no real good programmatic reason to limit what we bring back
  25. c.select! { |k,v| ["id", "title", "synopses"].include?(k) }
  26. end
  27. end
  28.  
  29. def add_programme(name)
  30. if self.select {|x| x["title"].downcase==name.downcase}.count > 0
  31. return "Programme {name} is already in your list of programmes"
  32. end
  33. s = i_search(name)
  34. return "No program found with a name like that" if s.count==0
  35. if s[0]["title"].downcase == name.downcase
  36. self << s[0]; "Successfully added #{s[0]['title']}"
  37. else
  38. "Could not find a program of the name #{name}. Did you mean #{s[0]['title']}?"
  39. end
  40. end
  41.  
  42. def delete_programme(name)
  43. if self.select {|x| x["title"].downcase==name.downcase}.count == 0
  44. return "Programme #{name} is not in your list of programmes"
  45. end
  46. self.delete_if {|x| x["title"].downcase==name.downcase}
  47. "#{name} has been deleted"
  48. end
  49.  
  50. def get_episodes
  51. episodes = []
  52. self.each do |programme|
  53. pid = programme["id"]
  54. url = "http://ibl.api.bbci.co.uk/ibl/#{@version}/programmes/#{pid}/episodes?rights=#{@rights}"
  55. url += "&availability=available&initial_child_count=2&per_page=200"
  56. episodes += get_http_content(url)["programme_episodes"]["elements"]
  57. end
  58. episodes
  59. end
  60.  
  61. def get_http_content(url);JSON.parse(@http.get(url).content);end
  62.  
  63. private
  64. #categories and regions have pretty much the same API so we can load them both through 1 method
  65. def load_standing_data(name)
  66. raw = get_http_content("http://ibl.api.bbci.co.uk/ibl/#{@version}/#{name}?lang=#{@lang}")
  67. # from an array of hashes, return a hash of hashes, with the id field as the key
  68. output = Hash.new
  69. raw["#{name}"].each { |x| output[x["id"]] = x }
  70. output
  71. end
  72. end
  73.  
  74. ##################### END OF CLASS #####################
  75. ###### NOW CREATE A SIMPLE COMMAND-LINE INTERFACE ######
  76.  
  77. def display_help
  78. puts 'Iplayer_data helps you keep track of your favourite programs.'
  79. puts 'It can store the programs you want to follow, then display information regarding episodes of those programs.'
  80. puts 'You can (a)dd, (d)elete or (v)iew your favourite programs...'
  81. puts 'Display (e)pisodes, (c)ategories, or (r)egions, or e(x)it.'
  82. puts 'Some instructions require extra input - just put a space after the first letter than type it in.'
  83. puts 'eg to add Top Gear, input "a top gear" (case insensitive)'
  84. #(d)elete, (p)rogrammes
  85. end
  86.  
  87. require 'pp' # pretty print
  88.  
  89. ip = Iplayer_data.new
  90. re=''
  91. until re[0] == 'x' do
  92.  
  93. print '>'
  94. re = gets.chomp
  95. case re[0]
  96. when '?' then display_help
  97. when 'a' then puts ip.add_programme(re[2..-1])
  98. when 'd' then puts ip.delete_programme(re[2..-1])
  99. when 'v' then pp ip
  100. when 'c' then pp ip.categories
  101. when 'r' then pp ip.regions
  102. when 'e' then
  103. #list of episodes, ordered by soonest to latest to expire
  104. ep = ip.get_episodes.sort_by { |episode| episode["versions"][0]["availability"]["end"]}
  105. ep.each do |e|
  106. e["time_left"] = e["versions"][0]["availability"]["remaining"]["text"]
  107. puts e.select {|k,v| ["id", "title", "subtitle", "time_left"].include?(k) }
  108. end
  109. puts "No episodes found" if ep.count==0
  110. when 'x' then puts "Exiting..."
  111. else puts "Sorry, I did not recognize that input. Type "?" for help."
  112. end
  113. end
Add Comment
Please, Sign In to add comment