ruby miwim upload video


SUBMITTED BY: Guest

DATE: Sept. 29, 2014, 6:05 p.m.

FORMAT: Text only

SIZE: 3.8 kB

HITS: 1453

  1. require 'mechanize'
  2. module Mivim # :nodoc:
  3. class Uploader
  4. =begin rdoc
  5. In order to create valid uploader instance pass credentials of the registered account.
  6. =end
  7. def initialize(username, password)
  8. @agent = Mechanize.new do |a|
  9. a.user_agent_alias = 'Mac Safari'
  10. a.max_history = 1
  11. end
  12. @username = username
  13. @password = password
  14. end
  15. =begin rdoc
  16. Logs into the site using credentials passed to constructor.
  17. Returns true value on success and false value if login failed.
  18. =end
  19. def login
  20. @agent.get('http://video.miwim.fr/login')
  21. form = @agent.page.form_with(:name => 'loginForm')
  22. form.username = @username
  23. form.password = @password
  24. form.submit
  25. login_successful?
  26. end
  27. =begin rdoc
  28. Checks login status.
  29. Returns true value if logged in and false value otherwise.
  30. =end
  31. def login_successful?
  32. !! @agent.page.at('#userinfo #userinfo-title')
  33. end
  34. =begin rdoc
  35. Uploads video file and returns URL on success.
  36. On failure returns false value.
  37. +params+ must contain following values:
  38. :title name of the video
  39. :keywords list of tags assigned to video
  40. :description some info about the video
  41. :category video channel name (one of the predefined on the site)
  42. :path file system path where video file is located
  43. It is possible to use short category name like "Tech" for "Hi-Tech, science"
  44. +params+ may contain following values:
  45. :privacy indicates video visibility and should be "private" (default) or "public"
  46. =end
  47. def upload(params = {})
  48. @params = params
  49. fill_info
  50. upload_file if has_upload_form?
  51. video_link
  52. rescue
  53. false
  54. end
  55. private
  56. def fill_info
  57. @agent.get('/upload')
  58. form = @agent.page.form_with(:name => 'theForm')
  59. form.field_myvideo_title = @params[:title]
  60. form.field_myvideo_keywords = @params[:keywords]
  61. form.field_myvideo_descr = @params[:description]
  62. form.checkboxes_with(:name => 'chlist[]').each do |category|
  63. category_name = category.node.at('.//following-sibling::text()').text.strip
  64. if category_name.include? @params[:category]
  65. category.check
  66. @params[:category_id] = category.value
  67. break
  68. end
  69. end
  70. form.add_field! 'submit.x', rand(30)
  71. form.add_field! 'submit.y', rand(30)
  72. form.submit
  73. end
  74. def has_upload_form?
  75. !! @agent.page.at('form#form_upload')
  76. end
  77. def upload_file
  78. form = @agent.page.form_with(:name => 'form_upload')
  79. form.action = '/upload'
  80. form.delete_field!('no_script') rescue nil
  81. form.delete_field!('upload_range') rescue nil
  82. form.add_field! 'chlist[]', @params[:category_id]
  83. form.radiobutton_with(:name => 'field_privacy', :value => (@params[:privacy] || 'private')).check
  84. form.submit
  85. @agent.get("/ubr_link_upload.php?config_file=ubr_default_config.php&rnd_id=#{Time.now.to_i}")
  86. upload_id = @agent.page.root.to_s.match(/startUpload\("([^"]+)"/)[1]
  87. @agent.get("/ubr_set_progress.php?upload_id=#{upload_id}")
  88. form.action = "/cgi-bin/ubr_upload.pl?upload_id=#{upload_id}"
  89. form.file_uploads.first.file_name = @params[:path]
  90. form.submit
  91. @agent.get("/upload?upload_id=#{upload_id}")
  92. end
  93. def video_link
  94. @agent.page.at('input[name="video_link"]')[:value] rescue nil
  95. end
  96. end
  97. end

comments powered by Disqus