ruby smotri upload video


SUBMITTED BY: Guest

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

FORMAT: Text only

SIZE: 4.7 kB

HITS: 1308

  1. require 'mechanize'
  2. module Smotri # :nodoc:
  3. class Uploader
  4. START_URL = 'http://smotri.com/'
  5. =begin rdoc
  6. In order to create a valid uploader instance pass credentials of some registered account.
  7. =end
  8. def initialize(username, password)
  9. @username = username
  10. @password = password
  11. make_agent
  12. end
  13. =begin rdoc
  14. Logs into the site using credentials passed to constructor.
  15. Returns true value on success and false value if login failed.
  16. =end
  17. def login
  18. @agent.get START_URL
  19. form = @agent.page.form_with(:id => 'login')
  20. form.login = @username
  21. form.password = @password
  22. form.submit
  23. login_successful?
  24. end
  25. =begin rdoc
  26. Checks login status.
  27. Returns true value if logged in and false value otherwise.
  28. =end
  29. def login_successful?
  30. !! @agent.page.at('.users-office')
  31. end
  32. =begin rdoc
  33. Uploads video file and returns video link on success.
  34. On failure returns false value.
  35. +params+ must contain following values:
  36. :title name of the video
  37. :path file system path where video file is located
  38. +params+ may contain following options:
  39. :category category name (one of the predefined on the site)
  40. :keywords list of tags assigned to video separated by commas
  41. :description some info about the video
  42. :privacy indicates video visibility and should be "public" or "private" (default)
  43. :password set password authentication for video
  44. :commentable pass true value in order to allow comments
  45. :downloadale pass true to grant video downloading
  46. =end
  47. def upload(params = {})
  48. get_upload_page
  49. if upload_file(params)
  50. link = video_link
  51. fill_details(params)
  52. if navigate_to_edit(link)
  53. fill_details(params)
  54. end
  55. return link
  56. end
  57. rescue
  58. false
  59. end
  60. private
  61. def make_agent
  62. @agent = Mechanize.new do |a|
  63. a.user_agent_alias = 'Mac Safari'
  64. a.max_history = 1
  65. end
  66. end
  67. def get_upload_page
  68. @agent.page.link_with(:href => %r{/upload}).click
  69. @agent.get @agent.page.at('//iframe[contains(@src, "upload")]')[:src]
  70. end
  71. def upload_file(params)
  72. form = upload_form
  73. return false unless form
  74. form.file_upload_with(:name => 'upload').file_name = params[:path]
  75. form.title = params[:title]
  76. form.tags = params[:keywords] if params[:keywords]
  77. category = form.field_with(:name => 'rubric')
  78. option = category.options.detect {|opt| opt.text =~ /#{params[:category] || 'nonexistent'}/ } || category.options[1]
  79. option.select
  80. form.submit
  81. @agent.get @agent.page.body.match(/window.parent.parent.location\s*=\s*["']([^'"]+)/)[1]
  82. end
  83. def navigate_to_edit(link)
  84. tries = 0
  85. loop do
  86. sleep 5
  87. @agent.get link
  88. if edit_link = @agent.page.link_with(:href => %r{/video/edit})
  89. edit_link.click
  90. return true
  91. else
  92. tries += 1
  93. if tries > 3
  94. return false
  95. else
  96. sleep rand(15)
  97. end
  98. end
  99. end
  100. end
  101. def fill_details(params)
  102. form = @agent.page.form_with(:id => 'video_edit')
  103. return unless form
  104. form.action = @agent.page.uri.to_s
  105. form.radiobutton_with(:name => 'visible', :value => (params[:privacy] == 'public' ? '0' : '1')).check
  106. form.description = params[:description]
  107. if params[:password]
  108. form.checkbox_with(:name => 'option').check
  109. form.password = params[:password]
  110. form.confirm_password = params[:password]
  111. end
  112. form.checkbox_with(:name => 'allow_download').send(params[:downloadale] ? :check : :uncheck)
  113. form.checkbox_with(:name => 'deny_comments').send(params[:commentable] ? :uncheck : :check)
  114. form.delete_field! 'allow_notify'
  115. #form.add_field! 'submit_mv_form.x', rand(30)
  116. #form.add_field! 'submit_mv_form.y', rand(30)
  117. form.submit
  118. end
  119. def upload_form
  120. @agent.page.form_with :id => 'video_upload'
  121. end
  122. def video_link
  123. if link = @agent.page.at('#videolinktext')
  124. link[:value]
  125. end
  126. end
  127. end
  128. end

comments powered by Disqus