fb_video_url_converter 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/CHANGELOG.rdoc +4 -0
- data/Gemfile +2 -0
- data/MIT-LICENSE +20 -0
- data/Manifest +14 -0
- data/README.md +112 -0
- data/Rakefile +13 -0
- data/fb_video_url_converter.gemspec +40 -0
- data/init.rb +1 -0
- data/lib/facebook_bot.rb +117 -0
- data/lib/facebook_video.rb +116 -0
- data/lib/fb_video_url_converter.rb +2 -0
- data/spec/facebook_bot_spec.rb +52 -0
- data/spec/facebook_video_spec.rb +70 -0
- data/spec/spec_helper.rb +44 -0
- data.tar.gz.sig +0 -0
- metadata +131 -0
- metadata.gz.sig +0 -0
data/CHANGELOG.rdoc
ADDED
data/Gemfile
ADDED
data/MIT-LICENSE
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
Copyright (c) 2010 Maciej Mensfeld
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
|
4
|
+
a copy of this software and associated documentation files (the
|
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
|
9
|
+
the following conditions:
|
|
10
|
+
|
|
11
|
+
The above copyright notice and this permission notice shall be
|
|
12
|
+
included in all copies or substantial portions of the Software.
|
|
13
|
+
|
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Manifest
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
CHANGELOG.rdoc
|
|
2
|
+
Gemfile
|
|
3
|
+
MIT-LICENSE
|
|
4
|
+
Manifest
|
|
5
|
+
README.md
|
|
6
|
+
Rakefile
|
|
7
|
+
fb_video_url_converter.gemspec
|
|
8
|
+
init.rb
|
|
9
|
+
lib/facebook_bot.rb
|
|
10
|
+
lib/facebook_video.rb
|
|
11
|
+
lib/fb_video_url_converter.rb
|
|
12
|
+
spec/facebook_bot_spec.rb
|
|
13
|
+
spec/facebook_video_spec.rb
|
|
14
|
+
spec/spec_helper.rb
|
data/README.md
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
# Facebook Video URL Converter
|
|
2
|
+
|
|
3
|
+
## Install
|
|
4
|
+
|
|
5
|
+
gem install fb_video_url_converter
|
|
6
|
+
|
|
7
|
+
and in your Gemfile:
|
|
8
|
+
|
|
9
|
+
gem 'fb_video_url_converter'
|
|
10
|
+
|
|
11
|
+
## About
|
|
12
|
+
|
|
13
|
+
Facebook Video URL Converter is intended as an easy alternative to
|
|
14
|
+
changing video hosting from Facebook to a different one.
|
|
15
|
+
|
|
16
|
+
When you upload a movie to a Facebook profile, it is available through a URL.
|
|
17
|
+
However, if You want to insert your movie into external player (like JW Player),
|
|
18
|
+
you will have a problem. Facebook recently found out, that lots of websites
|
|
19
|
+
use them as a free HD video hosting. Each 24h they switch authorization
|
|
20
|
+
parameters, so you cannot embed links directly.
|
|
21
|
+
|
|
22
|
+
You need to refresh URLs with Facebook. That is why I've developed this small
|
|
23
|
+
piece of code. It gets MP4 file URL and cache it for 30 minutes. When cache
|
|
24
|
+
expires it checks URL header response and if it fails it refresh cache.
|
|
25
|
+
|
|
26
|
+
Works for approximately 99% of time.
|
|
27
|
+
|
|
28
|
+
Works with Ruby 1.8.7, 1.8.7 EE, 1.9.2
|
|
29
|
+
|
|
30
|
+
## Requirements
|
|
31
|
+
|
|
32
|
+
Facebook Video URL Converter uses ActiveRecord to store cached data.
|
|
33
|
+
You need to create a migration containing:
|
|
34
|
+
|
|
35
|
+
class CreateFacebookVideos < ActiveRecord::Migration
|
|
36
|
+
def self.up
|
|
37
|
+
create_table :facebook_videos do |t|
|
|
38
|
+
t.column :video_id, :string
|
|
39
|
+
t.column :name, :string
|
|
40
|
+
t.column :url, :string, :default => nil
|
|
41
|
+
t.column :views, :integer, :default => 1
|
|
42
|
+
t.column :cached_at, :datetime
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
add_index :facebook_videos, :video_id
|
|
46
|
+
add_index :facebook_videos, :url
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def self.down
|
|
50
|
+
drop_table :facebook_videos
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
or without migrations:
|
|
55
|
+
|
|
56
|
+
unless FacebookVideo.table_exists?
|
|
57
|
+
ActiveRecord::Base.connection.create_table(:facebook_videos) do |t|
|
|
58
|
+
t.column :video_id, :string
|
|
59
|
+
t.column :name, :string
|
|
60
|
+
t.column :url, :string, :default => nil
|
|
61
|
+
t.column :views, :integer, :default => 1
|
|
62
|
+
t.column :cached_at, :datetime
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
## Example
|
|
67
|
+
|
|
68
|
+
url = 'http://www.facebook.com/home.php?#!/video/video.php?v=SOME_ID'
|
|
69
|
+
p FacebookVideo.video_url(url)
|
|
70
|
+
|
|
71
|
+
=> 'http://video.ak.fbcdn.net/cfs-ak-ash4/214...long_long_url'
|
|
72
|
+
|
|
73
|
+
p FacebookVideo.video_name(url)
|
|
74
|
+
|
|
75
|
+
=> 'video_name'
|
|
76
|
+
|
|
77
|
+
## Setup
|
|
78
|
+
|
|
79
|
+
When you create table required by Facebook Video URL Converter, you need to do
|
|
80
|
+
one more thing - you need to setup your Facebook account. To do so, create
|
|
81
|
+
initializer containing:
|
|
82
|
+
|
|
83
|
+
FacebookBot.email = 'insert_here_mail@facebook.com'
|
|
84
|
+
FacebookBot.password = 'pass'
|
|
85
|
+
# Cache storage - in seconds
|
|
86
|
+
FacebookVideo.cache = 60*10
|
|
87
|
+
|
|
88
|
+
Insert your own account data and you are ready to go :)
|
|
89
|
+
|
|
90
|
+
## Tests
|
|
91
|
+
|
|
92
|
+
If you want to run rspec tests - you will need to modify:
|
|
93
|
+
|
|
94
|
+
FacebookBot.email = 'test@test.pl'
|
|
95
|
+
FacebookBot.password = 'pass'
|
|
96
|
+
# Cache storage - in seconds
|
|
97
|
+
FacebookVideo.cache = 60*10
|
|
98
|
+
|
|
99
|
+
and set them according to your own profile.
|
|
100
|
+
|
|
101
|
+
## Note on Patches/Pull Requests
|
|
102
|
+
|
|
103
|
+
* Fork the project.
|
|
104
|
+
* Make your feature addition or bug fix.
|
|
105
|
+
* Add tests for it. This is important so I don't break it in a future version unintentionally.
|
|
106
|
+
* Commit, do not mess with Rakefile, version, or history.
|
|
107
|
+
(if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
|
|
108
|
+
* Send me a pull request. Bonus points for topic branches.
|
|
109
|
+
|
|
110
|
+
## Copyright
|
|
111
|
+
|
|
112
|
+
Copyright (c) 2011 Maciej Mensfeld. See LICENSE for details.
|
data/Rakefile
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
require 'rubygems'
|
|
2
|
+
require 'rake'
|
|
3
|
+
require 'echoe'
|
|
4
|
+
|
|
5
|
+
Echoe.new('fb_video_url_converter', '0.1.2') do |p|
|
|
6
|
+
p.description = "Facebook Video URL Converter is intended as an easy alternative to changing video hosting from Facebook to a different one."
|
|
7
|
+
p.url = "https://github.com/mensfeld/FB-Video-URL-Converter"
|
|
8
|
+
p.author = "Maciej Mensfeld"
|
|
9
|
+
p.email = "maciej@mensfeld.pl"
|
|
10
|
+
p.ignore_pattern = ["tmp/*", "script/*"]
|
|
11
|
+
p.development_dependencies = ["rspec >=2.0.0", "active_record", 'mechanize']
|
|
12
|
+
end
|
|
13
|
+
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
|
2
|
+
|
|
3
|
+
Gem::Specification.new do |s|
|
|
4
|
+
s.name = %q{fb_video_url_converter}
|
|
5
|
+
s.version = "0.1.2"
|
|
6
|
+
|
|
7
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
|
|
8
|
+
s.authors = ["Maciej Mensfeld"]
|
|
9
|
+
s.cert_chain = ["/home/mencio/.cert_keys/gem-public_cert.pem"]
|
|
10
|
+
s.date = %q{2011-05-03}
|
|
11
|
+
s.description = %q{Facebook Video URL Converter is intended as an easy alternative to changing video hosting from Facebook to a different one.}
|
|
12
|
+
s.email = %q{maciej@mensfeld.pl}
|
|
13
|
+
s.extra_rdoc_files = ["CHANGELOG.rdoc", "README.md", "lib/facebook_bot.rb", "lib/facebook_video.rb", "lib/fb_video_url_converter.rb"]
|
|
14
|
+
s.files = ["CHANGELOG.rdoc", "Gemfile", "MIT-LICENSE", "Manifest", "README.md", "Rakefile", "fb_video_url_converter.gemspec", "init.rb", "lib/facebook_bot.rb", "lib/facebook_video.rb", "lib/fb_video_url_converter.rb", "spec/facebook_bot_spec.rb", "spec/facebook_video_spec.rb", "spec/spec_helper.rb"]
|
|
15
|
+
s.homepage = %q{https://github.com/mensfeld/FB-Video-URL-Converter}
|
|
16
|
+
s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Fb_video_url_converter", "--main", "README.md"]
|
|
17
|
+
s.require_paths = ["lib"]
|
|
18
|
+
s.rubyforge_project = %q{fb_video_url_converter}
|
|
19
|
+
s.rubygems_version = %q{1.5.2}
|
|
20
|
+
s.signing_key = %q{/home/mencio/.cert_keys/gem-private_key.pem}
|
|
21
|
+
s.summary = %q{Facebook Video URL Converter is intended as an easy alternative to changing video hosting from Facebook to a different one.}
|
|
22
|
+
|
|
23
|
+
if s.respond_to? :specification_version then
|
|
24
|
+
s.specification_version = 3
|
|
25
|
+
|
|
26
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
|
27
|
+
s.add_development_dependency(%q<rspec>, [">= 2.0.0"])
|
|
28
|
+
s.add_development_dependency(%q<active_record>, [">= 0"])
|
|
29
|
+
s.add_development_dependency(%q<mechanize>, [">= 0"])
|
|
30
|
+
else
|
|
31
|
+
s.add_dependency(%q<rspec>, [">= 2.0.0"])
|
|
32
|
+
s.add_dependency(%q<active_record>, [">= 0"])
|
|
33
|
+
s.add_dependency(%q<mechanize>, [">= 0"])
|
|
34
|
+
end
|
|
35
|
+
else
|
|
36
|
+
s.add_dependency(%q<rspec>, [">= 2.0.0"])
|
|
37
|
+
s.add_dependency(%q<active_record>, [">= 0"])
|
|
38
|
+
s.add_dependency(%q<mechanize>, [">= 0"])
|
|
39
|
+
end
|
|
40
|
+
end
|
data/init.rb
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
require 'fb_video_url_converter'
|
data/lib/facebook_bot.rb
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
require 'singleton'
|
|
2
|
+
require 'rubygems'
|
|
3
|
+
require 'mechanize'
|
|
4
|
+
include WWW
|
|
5
|
+
require 'uri'
|
|
6
|
+
require 'cgi'
|
|
7
|
+
require 'time'
|
|
8
|
+
|
|
9
|
+
class Mechanize::Page
|
|
10
|
+
def form_id(formId)
|
|
11
|
+
formcontents = (self/:form).find { |elem| elem['id'] == formId }
|
|
12
|
+
if formcontents then return Mechanize::Form.new(formcontents) end
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
class FacebookBot
|
|
17
|
+
|
|
18
|
+
# Probably wrong login || password
|
|
19
|
+
class LoginFailed < StandardError; end
|
|
20
|
+
|
|
21
|
+
class << self
|
|
22
|
+
attr_accessor :email
|
|
23
|
+
attr_accessor :password
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
FB_URL = "http://www.facebook.com/"
|
|
27
|
+
USER_AGENT = 'Windows IE 7'
|
|
28
|
+
VIDEO_ERROR = 'video_id_error'
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def initialize
|
|
32
|
+
@video_page = nil
|
|
33
|
+
@agent = Mechanize.new
|
|
34
|
+
@agent.user_agent_alias = USER_AGENT
|
|
35
|
+
|
|
36
|
+
begin
|
|
37
|
+
@@root = File.join(Rails.root, 'tmp')
|
|
38
|
+
rescue
|
|
39
|
+
@@root = File.expand_path(File.dirname(__FILE__))
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
@cookies = File.join(@@root, "cookie_#{self.class.email}.yml")
|
|
43
|
+
|
|
44
|
+
begin
|
|
45
|
+
@agent.cookie_jar.load(@cookies)
|
|
46
|
+
rescue
|
|
47
|
+
end if (File.file?(@cookies) && File.size(@cookies) > 10)
|
|
48
|
+
|
|
49
|
+
self.login
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def login
|
|
53
|
+
page = @agent.get(FB_URL)
|
|
54
|
+
|
|
55
|
+
if (loginf = page.form_id("login_form"))
|
|
56
|
+
loginf.set_fields(:email => self.class.email, :pass => self.class.password)
|
|
57
|
+
page = @agent.submit(loginf, loginf.buttons.first)
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
@agent.cookie_jar.save_as(@cookies)
|
|
61
|
+
page = @agent.get(FB_URL)
|
|
62
|
+
body = page.root.to_html
|
|
63
|
+
|
|
64
|
+
begin
|
|
65
|
+
# This is a UID given to each Facebook user.
|
|
66
|
+
@uid = %r{\\"user\\":(\d+),\\"hide\\"}.match(body)[1]
|
|
67
|
+
rescue
|
|
68
|
+
@uid = nil
|
|
69
|
+
end
|
|
70
|
+
# This is a token we need to submit forms.
|
|
71
|
+
begin
|
|
72
|
+
@post_form_id = %r{<input type="hidden" id="post_form_id" name="post_form_id" value="([^"]+)}.match(body)[1]
|
|
73
|
+
rescue
|
|
74
|
+
raise self.class::LoginFailed, 'Incorrect login or password'
|
|
75
|
+
end
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
def video_url(id)
|
|
79
|
+
begin
|
|
80
|
+
load_video_page(id)
|
|
81
|
+
get_url(@video_page)
|
|
82
|
+
rescue
|
|
83
|
+
VIDEO_ERROR
|
|
84
|
+
end
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
def video_name(id)
|
|
88
|
+
load_video_page(id)
|
|
89
|
+
get_name(@video_page)
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
private
|
|
93
|
+
|
|
94
|
+
def load_video_page(id)
|
|
95
|
+
if @video_page.nil?
|
|
96
|
+
pa = @agent.get("#{FB_URL}video/video.php?v=#{id}")
|
|
97
|
+
@video_page = pa.body
|
|
98
|
+
end
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
def get_url(url)
|
|
102
|
+
url = url.scan(/addVariable\(\"highqual_src\",\s\"http.+\"\)/ix).first
|
|
103
|
+
url = url.split(')').first.gsub('\u00253A', ':')
|
|
104
|
+
url = url.gsub('\u00252F', '/')
|
|
105
|
+
url = url.gsub('\u00253F', '?')
|
|
106
|
+
url = url.gsub('\u00253D', '=')
|
|
107
|
+
url = url.gsub('\u002526', '&')
|
|
108
|
+
url = "http://#{url.split('http://')[1]}".split('"').first
|
|
109
|
+
CGI.unescapeHTML(url)
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
def get_name(site)
|
|
113
|
+
name = site.scan(/datawrap\">(.+)<\/h3>/ix).first
|
|
114
|
+
name ? name.first : VIDEO_ERROR
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
end
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
# Copyright © 2011 Maciej Mensfeld, released under the MIT license
|
|
2
|
+
|
|
3
|
+
require 'net/http'
|
|
4
|
+
require 'uri'
|
|
5
|
+
require 'active_record'
|
|
6
|
+
|
|
7
|
+
# Small extension to determine if string is a number
|
|
8
|
+
class String
|
|
9
|
+
def is_number?
|
|
10
|
+
self.match(/\A[+-]?\d+?(\.\d+)?\Z/) == nil ? false : true
|
|
11
|
+
end
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
class FacebookVideo < ActiveRecord::Base
|
|
15
|
+
|
|
16
|
+
class << self
|
|
17
|
+
attr_accessor :cache
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
VIDEO_ERROR_MSG = 'video_id_error'
|
|
21
|
+
FB_ERROR_MSG = 'fb_account_error'
|
|
22
|
+
|
|
23
|
+
before_create :set_cached_at_time
|
|
24
|
+
|
|
25
|
+
validates_uniqueness_of :video_id
|
|
26
|
+
validate :working?
|
|
27
|
+
|
|
28
|
+
def self.video_url(v_id)
|
|
29
|
+
v = self.video_data(v_id)
|
|
30
|
+
case v
|
|
31
|
+
when 'video_error' then VIDEO_ERROR_MSG
|
|
32
|
+
when 'fb_error' then FB_ERROR_MSG
|
|
33
|
+
else v.url
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def self.video_name(v_id)
|
|
38
|
+
v = self.video_data(v_id)
|
|
39
|
+
case v
|
|
40
|
+
when 'video_error' then VIDEO_ERROR_MSG
|
|
41
|
+
when 'fb_error' then FB_ERROR_MSG
|
|
42
|
+
else v.name
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def url=(new_url)
|
|
47
|
+
self.errors.clear
|
|
48
|
+
self.cached_at = Time.now
|
|
49
|
+
super new_url
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def working?
|
|
53
|
+
v = false
|
|
54
|
+
if self.cached_at >= Time.now - self.class.cache && self.url.length > 10
|
|
55
|
+
v = true
|
|
56
|
+
else
|
|
57
|
+
v = url_working?
|
|
58
|
+
self.cached_at = Time.now
|
|
59
|
+
end
|
|
60
|
+
self.errors.add(:cached_at, 'Cache ulegl przedawnieniu') unless v
|
|
61
|
+
v
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def url_working?
|
|
65
|
+
begin
|
|
66
|
+
host = URI.parse(self.url).host
|
|
67
|
+
http = Net::HTTP.new(host)
|
|
68
|
+
headers = http.head(self.url)
|
|
69
|
+
rescue
|
|
70
|
+
return true
|
|
71
|
+
end
|
|
72
|
+
if headers.code == "200"
|
|
73
|
+
true
|
|
74
|
+
else
|
|
75
|
+
false
|
|
76
|
+
end
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
private
|
|
80
|
+
|
|
81
|
+
def self.video_data(v_id)
|
|
82
|
+
unless v_id.to_s.is_number?
|
|
83
|
+
v_id = v_id.scan(/[0-9]+/).first
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
return 'video_error' if v_id.nil? || v_id.length < 12
|
|
87
|
+
|
|
88
|
+
video = self.find_by_video_id(v_id)
|
|
89
|
+
unless video && video.valid?
|
|
90
|
+
begin
|
|
91
|
+
fb = FacebookBot.new
|
|
92
|
+
rescue
|
|
93
|
+
return 'fb_error'
|
|
94
|
+
end
|
|
95
|
+
url = fb.video_url(v_id)
|
|
96
|
+
name = fb.video_name(v_id)
|
|
97
|
+
if video
|
|
98
|
+
video.url = url
|
|
99
|
+
video.name = name
|
|
100
|
+
else
|
|
101
|
+
video = self.new(
|
|
102
|
+
:video_id => v_id,
|
|
103
|
+
:url => url,
|
|
104
|
+
:name => name)
|
|
105
|
+
end
|
|
106
|
+
end
|
|
107
|
+
video.views+=1
|
|
108
|
+
video.save
|
|
109
|
+
video
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
def set_cached_at_time
|
|
113
|
+
self.cached_at = Time.now
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
end
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
require 'spec_helper'
|
|
2
|
+
|
|
3
|
+
ROOT = File.expand_path(File.dirname(__FILE__))
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
def remove_cookie
|
|
7
|
+
c_p = File.join(ROOT, '..', 'lib', "cookie_#{FacebookBot.email}.yml")
|
|
8
|
+
FileUtils.rm( c_p ) if File.file?( c_p )
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
describe FacebookBot do
|
|
12
|
+
subject { FacebookBot }
|
|
13
|
+
# Remove cookie files
|
|
14
|
+
after(:all){ remove_cookie }
|
|
15
|
+
|
|
16
|
+
context "when we are not logged in" do
|
|
17
|
+
context "and login is correct" do
|
|
18
|
+
it "should log us in" do
|
|
19
|
+
subject.new
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
context "and login or password is incorrect" do
|
|
24
|
+
it "should throw failed exception" do
|
|
25
|
+
l = subject.email
|
|
26
|
+
subject.email = 'incorrect@incorrect.pl'
|
|
27
|
+
lambda { subject.new }.should raise_error(subject::LoginFailed, 'Incorrect login or password')
|
|
28
|
+
remove_cookie
|
|
29
|
+
subject.email = l
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
context "when we are logged in" do
|
|
35
|
+
it "should obtain valid url of a valid movie" do
|
|
36
|
+
fb = subject.new
|
|
37
|
+
fb.video_url('111449252268656').should_not eql 'video_id_error'
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
it "should obtain valid name of a valid movie" do
|
|
41
|
+
fb = subject.new
|
|
42
|
+
fb.video_name('111449252268656').should eql 'Naruto Shippuuden #203 Part2 [HD]'
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
it "should obtain valid error msg of a invalid movie" do
|
|
46
|
+
fb = subject.new
|
|
47
|
+
fb.video_url('1aaaaa').should eql 'video_id_error'
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
end
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
require 'spec_helper'
|
|
2
|
+
|
|
3
|
+
ROOT = File.expand_path(File.dirname(__FILE__))
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
def remove_cookie
|
|
7
|
+
c_p = File.join(ROOT, '..', 'lib', "cookie_#{FacebookBot.email}.yml")
|
|
8
|
+
FileUtils.rm( c_p ) if File.file?( c_p )
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
describe FacebookVideo do
|
|
12
|
+
subject { FacebookVideo }
|
|
13
|
+
after(:all){ remove_cookie }
|
|
14
|
+
|
|
15
|
+
context "when we obtain valid video for the first time" do
|
|
16
|
+
it "should fetch it and return url" do
|
|
17
|
+
FacebookVideo.video_url('111449252268656').should_not eql 'video_id_error'
|
|
18
|
+
url = 'http://www.facebook.com/video/video.php?v=119040278176220&comments'
|
|
19
|
+
FacebookVideo.video_url(url).should_not eql 'video_id_error'
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
it "should fetch it and return name" do
|
|
23
|
+
FacebookVideo.video_name('111449252268656').should eql 'Naruto Shippuuden #203 Part2 [HD]'
|
|
24
|
+
url = 'http://www.facebook.com/video/video.php?v=111449252268656&comments'
|
|
25
|
+
FacebookVideo.video_name(url).should eql 'Naruto Shippuuden #203 Part2 [HD]'
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
context "when we obtain video for n times" do
|
|
30
|
+
it "should increase views counter" do
|
|
31
|
+
FacebookVideo.video_name('111449252268656')
|
|
32
|
+
v = FacebookVideo.find_by_video_id('111449252268656').views
|
|
33
|
+
10.times do
|
|
34
|
+
FacebookVideo.video_name('111449252268656')
|
|
35
|
+
end
|
|
36
|
+
FacebookVideo.find_by_video_id('111449252268656').views.should == v+10
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
context "when video is too old" do
|
|
41
|
+
it "should rerequest it and refresh cache time" do
|
|
42
|
+
t = Time.now
|
|
43
|
+
FacebookVideo.video_name('111449252268656')
|
|
44
|
+
v = FacebookVideo.find_by_video_id('111449252268656')
|
|
45
|
+
v.cached_at = Time.at(946702800)
|
|
46
|
+
v.save!
|
|
47
|
+
FacebookVideo.video_name('111449252268656')
|
|
48
|
+
v.reload
|
|
49
|
+
v.cached_at.should > t
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
context "when we obtain invalid video" do
|
|
54
|
+
it "should return warning" do
|
|
55
|
+
FacebookVideo.video_name('').should eql 'video_id_error'
|
|
56
|
+
FacebookVideo.video_url('').should eql 'video_id_error'
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
context "when we cannot login to Facebook to obtain video" do
|
|
61
|
+
it "should return incorrect video ID info" do
|
|
62
|
+
l = FacebookBot.email
|
|
63
|
+
FacebookBot.email = 'aaa@aaa.pl'
|
|
64
|
+
FacebookVideo.video_url('120641554682759').should eql 'fb_account_error'
|
|
65
|
+
remove_cookie
|
|
66
|
+
FacebookBot.email = l
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
end
|
data/spec/spec_helper.rb
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
|
2
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
|
3
|
+
|
|
4
|
+
require 'singleton'
|
|
5
|
+
|
|
6
|
+
class <<Singleton
|
|
7
|
+
def included_with_reset(klass)
|
|
8
|
+
included_without_reset(klass)
|
|
9
|
+
class <<klass
|
|
10
|
+
def reset_instance
|
|
11
|
+
Singleton.send :__init__, self
|
|
12
|
+
self
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
alias_method :included_without_reset, :included
|
|
17
|
+
alias_method :included, :included_with_reset
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
require 'rubygems'
|
|
21
|
+
require 'sqlite3'
|
|
22
|
+
require 'fb_video_url_converter'
|
|
23
|
+
require 'active_record'
|
|
24
|
+
require 'fileutils'
|
|
25
|
+
|
|
26
|
+
FacebookBot.email = 'email'
|
|
27
|
+
FacebookBot.password = 'password'
|
|
28
|
+
FacebookVideo.cache = 60*10
|
|
29
|
+
|
|
30
|
+
ActiveRecord::Base.establish_connection(
|
|
31
|
+
:adapter => "sqlite3",
|
|
32
|
+
:database => ":memory:"
|
|
33
|
+
)
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
unless FacebookVideo.table_exists?
|
|
37
|
+
ActiveRecord::Base.connection.create_table(:facebook_videos) do |t|
|
|
38
|
+
t.column :video_id, :string
|
|
39
|
+
t.column :name, :string
|
|
40
|
+
t.column :url, :string, :default => nil
|
|
41
|
+
t.column :views, :integer, :default => 1
|
|
42
|
+
t.column :cached_at, :datetime
|
|
43
|
+
end
|
|
44
|
+
end
|
data.tar.gz.sig
ADDED
|
Binary file
|
metadata
ADDED
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: fb_video_url_converter
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
prerelease:
|
|
5
|
+
version: 0.1.2
|
|
6
|
+
platform: ruby
|
|
7
|
+
authors:
|
|
8
|
+
- Maciej Mensfeld
|
|
9
|
+
autorequire:
|
|
10
|
+
bindir: bin
|
|
11
|
+
cert_chain:
|
|
12
|
+
- |
|
|
13
|
+
-----BEGIN CERTIFICATE-----
|
|
14
|
+
MIIDMjCCAhqgAwIBAgIBADANBgkqhkiG9w0BAQUFADA/MQ8wDQYDVQQDDAZtYWNp
|
|
15
|
+
ZWoxGDAWBgoJkiaJk/IsZAEZFghtZW5zZmVsZDESMBAGCgmSJomT8ixkARkWAnBs
|
|
16
|
+
MB4XDTExMDQwOTA5NDcyMloXDTEyMDQwODA5NDcyMlowPzEPMA0GA1UEAwwGbWFj
|
|
17
|
+
aWVqMRgwFgYKCZImiZPyLGQBGRYIbWVuc2ZlbGQxEjAQBgoJkiaJk/IsZAEZFgJw
|
|
18
|
+
bDCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAL0+nG3V4/exIeiJ0IN+
|
|
19
|
+
wVfq8Utcu4Qpo+58EIVMIu3FiK+8w6MBvatZnUrRu12pqWLw9xrUkCiYeRErD+jF
|
|
20
|
+
AmdggIM/tu9CcjvURXH7VeTzOVA+pnV+eJWMD61o8HljFVcb/nyEYYVKErtr9/O4
|
|
21
|
+
QrIGv5lnszq1PMj2sBMy2gOP1YnzawncMLmkpp/T5SU4JZ5gAktGMRVz8RxmZzF5
|
|
22
|
+
6NVqFLbuqSRSU5U//WJvZVJt8dycCGgQzBM4Vi3nkOWyjIF0BANf1TqnlU2u6s8d
|
|
23
|
+
UK1AoDZfg5feef5e8eqoomHebX1opNGM/SOQhu3LRgax4rJfnl6VS3I2wighohsf
|
|
24
|
+
AgcCAwEAAaM5MDcwCQYDVR0TBAIwADAdBgNVHQ4EFgQUGlrWBqxVieAPk7NEzBDp
|
|
25
|
+
kM+iAMMwCwYDVR0PBAQDAgSwMA0GCSqGSIb3DQEBBQUAA4IBAQAJMoyBaJs8boiz
|
|
26
|
+
lFpbw6MWjk+7ZhqoHpFrWEV4nzb5GzyHZ7GU/pa1fSEQR0SCs+LnTLQbAYNQyUTT
|
|
27
|
+
O+UsTuA7xzI//v6cSodv3Q9NbfoDlou74xv1NXorWoosQFMpVWrXv+c/1RqU3cq4
|
|
28
|
+
WUr+rRiveEXG4tXOwkrpX8KH8xVp2vQZcGw3AXPqhzfqDGzpHd6ws3lk+8HoSrSo
|
|
29
|
+
2L68tDoxraF2Z2toAg9vfFw1+mOeDk1xVIPVcBy3tJxstHfHGHlQuMiRiDQX2b2D
|
|
30
|
+
YYU8UWVt2841IwB5Dgl4O+atXhe9ZTBO0W32pl4Bq5CP9lhQRT1KL7sxfznJlF7Y
|
|
31
|
+
BH3YFsdk
|
|
32
|
+
-----END CERTIFICATE-----
|
|
33
|
+
|
|
34
|
+
date: 2011-05-03 00:00:00 +02:00
|
|
35
|
+
default_executable:
|
|
36
|
+
dependencies:
|
|
37
|
+
- !ruby/object:Gem::Dependency
|
|
38
|
+
name: rspec
|
|
39
|
+
prerelease: false
|
|
40
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
|
41
|
+
none: false
|
|
42
|
+
requirements:
|
|
43
|
+
- - ">="
|
|
44
|
+
- !ruby/object:Gem::Version
|
|
45
|
+
version: 2.0.0
|
|
46
|
+
type: :development
|
|
47
|
+
version_requirements: *id001
|
|
48
|
+
- !ruby/object:Gem::Dependency
|
|
49
|
+
name: active_record
|
|
50
|
+
prerelease: false
|
|
51
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
|
52
|
+
none: false
|
|
53
|
+
requirements:
|
|
54
|
+
- - ">="
|
|
55
|
+
- !ruby/object:Gem::Version
|
|
56
|
+
version: "0"
|
|
57
|
+
type: :development
|
|
58
|
+
version_requirements: *id002
|
|
59
|
+
- !ruby/object:Gem::Dependency
|
|
60
|
+
name: mechanize
|
|
61
|
+
prerelease: false
|
|
62
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
|
63
|
+
none: false
|
|
64
|
+
requirements:
|
|
65
|
+
- - ">="
|
|
66
|
+
- !ruby/object:Gem::Version
|
|
67
|
+
version: "0"
|
|
68
|
+
type: :development
|
|
69
|
+
version_requirements: *id003
|
|
70
|
+
description: Facebook Video URL Converter is intended as an easy alternative to changing video hosting from Facebook to a different one.
|
|
71
|
+
email: maciej@mensfeld.pl
|
|
72
|
+
executables: []
|
|
73
|
+
|
|
74
|
+
extensions: []
|
|
75
|
+
|
|
76
|
+
extra_rdoc_files:
|
|
77
|
+
- CHANGELOG.rdoc
|
|
78
|
+
- README.md
|
|
79
|
+
- lib/facebook_bot.rb
|
|
80
|
+
- lib/facebook_video.rb
|
|
81
|
+
- lib/fb_video_url_converter.rb
|
|
82
|
+
files:
|
|
83
|
+
- CHANGELOG.rdoc
|
|
84
|
+
- Gemfile
|
|
85
|
+
- MIT-LICENSE
|
|
86
|
+
- Manifest
|
|
87
|
+
- README.md
|
|
88
|
+
- Rakefile
|
|
89
|
+
- fb_video_url_converter.gemspec
|
|
90
|
+
- init.rb
|
|
91
|
+
- lib/facebook_bot.rb
|
|
92
|
+
- lib/facebook_video.rb
|
|
93
|
+
- lib/fb_video_url_converter.rb
|
|
94
|
+
- spec/facebook_bot_spec.rb
|
|
95
|
+
- spec/facebook_video_spec.rb
|
|
96
|
+
- spec/spec_helper.rb
|
|
97
|
+
has_rdoc: true
|
|
98
|
+
homepage: https://github.com/mensfeld/FB-Video-URL-Converter
|
|
99
|
+
licenses: []
|
|
100
|
+
|
|
101
|
+
post_install_message:
|
|
102
|
+
rdoc_options:
|
|
103
|
+
- --line-numbers
|
|
104
|
+
- --inline-source
|
|
105
|
+
- --title
|
|
106
|
+
- Fb_video_url_converter
|
|
107
|
+
- --main
|
|
108
|
+
- README.md
|
|
109
|
+
require_paths:
|
|
110
|
+
- lib
|
|
111
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
112
|
+
none: false
|
|
113
|
+
requirements:
|
|
114
|
+
- - ">="
|
|
115
|
+
- !ruby/object:Gem::Version
|
|
116
|
+
version: "0"
|
|
117
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
118
|
+
none: false
|
|
119
|
+
requirements:
|
|
120
|
+
- - ">="
|
|
121
|
+
- !ruby/object:Gem::Version
|
|
122
|
+
version: "1.2"
|
|
123
|
+
requirements: []
|
|
124
|
+
|
|
125
|
+
rubyforge_project: fb_video_url_converter
|
|
126
|
+
rubygems_version: 1.5.2
|
|
127
|
+
signing_key:
|
|
128
|
+
specification_version: 3
|
|
129
|
+
summary: Facebook Video URL Converter is intended as an easy alternative to changing video hosting from Facebook to a different one.
|
|
130
|
+
test_files: []
|
|
131
|
+
|
metadata.gz.sig
ADDED
|
Binary file
|