funky 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +11 -0
- data/.rspec +2 -0
- data/.travis.yml +5 -0
- data/CHANGELOG.md +11 -0
- data/CODE_OF_CONDUCT.md +49 -0
- data/Gemfile +5 -0
- data/LICENSE.txt +21 -0
- data/README.md +112 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/funky.gemspec +28 -0
- data/lib/funky/configuration.rb +10 -0
- data/lib/funky/connections/api.rb +50 -0
- data/lib/funky/connections/base.rb +22 -0
- data/lib/funky/errors.rb +4 -0
- data/lib/funky/html/page.rb +33 -0
- data/lib/funky/html/parser.rb +41 -0
- data/lib/funky/version.rb +3 -0
- data/lib/funky/video.rb +133 -0
- data/lib/funky.rb +17 -0
- metadata +153 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 5cca92d30f4575bcf0dcccd62ace840b68b56b62
|
4
|
+
data.tar.gz: d1a848502da15831b6f50a2a4077db0596f5c332
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: a2306ed173e47b236cbd4267ad95e23d3cf6a8fb7c345f65cd6cc2eba2d53752f9720dedad95048466c3e5701f16adb80c49211d302cbd662b492a1f855797b2
|
7
|
+
data.tar.gz: 877b2d66b634a9924b79550d4483fd86dda43ce8e9c35116306df4a472501fad97d0633c98ec10473b74196bd276b2ea2f473c618db3b47f8ea0b50ed42833c4
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CHANGELOG.md
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
# Changelog
|
2
|
+
|
3
|
+
All notable changes to this project will be documented in this file.
|
4
|
+
|
5
|
+
For more information about changelogs, check
|
6
|
+
[Keep a Changelog](http://keepachangelog.com) and
|
7
|
+
[Vandamme](http://tech-angels.github.io/vandamme).
|
8
|
+
|
9
|
+
## 0.1.0 - 2016/05/26
|
10
|
+
|
11
|
+
* [FEATURE] Added support for Facebook video data. Currently we can get the video id, length, description, created_time, picture, view_count, share_count, comment_count, and like_count.
|
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
# Contributor Code of Conduct
|
2
|
+
|
3
|
+
As contributors and maintainers of this project, and in the interest of
|
4
|
+
fostering an open and welcoming community, we pledge to respect all people who
|
5
|
+
contribute through reporting issues, posting feature requests, updating
|
6
|
+
documentation, submitting pull requests or patches, and other activities.
|
7
|
+
|
8
|
+
We are committed to making participation in this project a harassment-free
|
9
|
+
experience for everyone, regardless of level of experience, gender, gender
|
10
|
+
identity and expression, sexual orientation, disability, personal appearance,
|
11
|
+
body size, race, ethnicity, age, religion, or nationality.
|
12
|
+
|
13
|
+
Examples of unacceptable behavior by participants include:
|
14
|
+
|
15
|
+
* The use of sexualized language or imagery
|
16
|
+
* Personal attacks
|
17
|
+
* Trolling or insulting/derogatory comments
|
18
|
+
* Public or private harassment
|
19
|
+
* Publishing other's private information, such as physical or electronic
|
20
|
+
addresses, without explicit permission
|
21
|
+
* Other unethical or unprofessional conduct
|
22
|
+
|
23
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
24
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
25
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
26
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
27
|
+
threatening, offensive, or harmful.
|
28
|
+
|
29
|
+
By adopting this Code of Conduct, project maintainers commit themselves to
|
30
|
+
fairly and consistently applying these principles to every aspect of managing
|
31
|
+
this project. Project maintainers who do not follow or enforce the Code of
|
32
|
+
Conduct may be permanently removed from the project team.
|
33
|
+
|
34
|
+
This code of conduct applies both within project spaces and in public spaces
|
35
|
+
when an individual is representing the project or its community.
|
36
|
+
|
37
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
38
|
+
reported by contacting a project maintainer at philip.nguyen@fullscreen.net. All
|
39
|
+
complaints will be reviewed and investigated and will result in a response that
|
40
|
+
is deemed necessary and appropriate to the circumstances. Maintainers are
|
41
|
+
obligated to maintain confidentiality with regard to the reporter of an
|
42
|
+
incident.
|
43
|
+
|
44
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage],
|
45
|
+
version 1.3.0, available at
|
46
|
+
[http://contributor-covenant.org/version/1/3/0/][version]
|
47
|
+
|
48
|
+
[homepage]: http://contributor-covenant.org
|
49
|
+
[version]: http://contributor-covenant.org/version/1/3/0/
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Fullscreen, Inc
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,112 @@
|
|
1
|
+
[![Build Status](https://travis-ci.org/Fullscreen/funky.svg?branch=master)](https://travis-ci.org/Fullscreen/funky)
|
2
|
+
[![Coverage Status](https://coveralls.io/repos/github/Fullscreen/funky/badge.svg?branch=master)](https://coveralls.io/github/Fullscreen/funky?branch=master)
|
3
|
+
|
4
|
+
# Funky
|
5
|
+
|
6
|
+
Funky is a Ruby library to fetch data about videos posted on Facebook, such as their title, description, number of views, comments, shares, and likes.
|
7
|
+
|
8
|
+
## How it works
|
9
|
+
|
10
|
+
Funky can get *public* Facebook video data whether the Graphi API requires insight permission or not. For example, even though the number of shares and views are shown publicly on the web page, the Graph API will not return those results unless the user has insight permission for that video. Using Funky, you can obtain the number of shares and views without insight permissions.
|
11
|
+
|
12
|
+
Under the hood, Funky hits Facebook's APIs on some cases, while other cases it will scrape Facebook's HTML to get the data. It's kind of... funky.
|
13
|
+
|
14
|
+
## Usage
|
15
|
+
|
16
|
+
This is still a very early version, and it currently can only retrieve certain Facebook video data.
|
17
|
+
|
18
|
+
### Configuring Funky
|
19
|
+
|
20
|
+
Funky will require an App ID and an App Secret which you can obtain after registering as a developer on [Facebook for developers](https://developers.facebook.com/).
|
21
|
+
|
22
|
+
There are two ways to configure Funky with your App ID and App Secret:
|
23
|
+
|
24
|
+
1. By default, Funky will look for the environment variables called `FB_APP_ID` and `FB_APP_SECRET`. You can put those keys in your `.bash_profile` and Funky will work.
|
25
|
+
|
26
|
+
```
|
27
|
+
export FB_APP_ID="YourAppID"
|
28
|
+
export FB_APP_SECRET="YourAppSecret"
|
29
|
+
```
|
30
|
+
|
31
|
+
2. If you're using this in Rails, you can choose to create an initializer instead and configure the App ID and App Secret like so:
|
32
|
+
|
33
|
+
```ruby
|
34
|
+
Funky.configure do |config|
|
35
|
+
config.app_id = 'YourAppID'
|
36
|
+
config.app_secret = 'YourAppSecret'
|
37
|
+
end
|
38
|
+
```
|
39
|
+
Or with environment variables (which is safer) like so:
|
40
|
+
|
41
|
+
```ruby
|
42
|
+
Funky.configure do |config|
|
43
|
+
config.app_id = ENV['FB_APP_ID']
|
44
|
+
config.app_secret = ENV['FB_APP_SECRET']
|
45
|
+
end
|
46
|
+
```
|
47
|
+
|
48
|
+
### Use #where clause to get an array of videos
|
49
|
+
|
50
|
+
```ruby
|
51
|
+
ids = ['10154439119663508', '10153834590672139']
|
52
|
+
videos = Funky::Video.where(id: ids)
|
53
|
+
videos.first.id # => '10154439119663508'
|
54
|
+
videos.first.created_time # => #<DateTime: 2015-12-17T06:29:48+00:00>
|
55
|
+
videos.first.description # => "Hugh Jackman coaches Great Britain's..."
|
56
|
+
videos.first.length # => 147.05
|
57
|
+
videos.first.picture # => "https://scontent.xx.fbcdn.net/v/..."
|
58
|
+
|
59
|
+
```
|
60
|
+
|
61
|
+
If a non-existing video ID is passed into the where clause, it is ignored. Other video IDs will still be retrieved.
|
62
|
+
|
63
|
+
```ruby
|
64
|
+
ids = ['10154439119663508', '10153834590672139', 'doesnotexist']
|
65
|
+
videos = Funky::Video.where(id: ids)
|
66
|
+
videos.count # => 2
|
67
|
+
videos.first.id # => '10154439119663508'
|
68
|
+
videos.last.id # => '10153834590672139'
|
69
|
+
```
|
70
|
+
|
71
|
+
### Use #find to get a single video
|
72
|
+
|
73
|
+
```ruby
|
74
|
+
video = Funky::Video.find('10154439119663508')
|
75
|
+
video.id # => '10154439119663508'
|
76
|
+
video.like_count # => 1169
|
77
|
+
video.comment_count # => 65
|
78
|
+
video.share_count # => 348
|
79
|
+
video.view_count # => 10121
|
80
|
+
```
|
81
|
+
|
82
|
+
If a non-existing video ID is passed into #find, Funky::ContentNotFound will be raised.
|
83
|
+
|
84
|
+
```ruby
|
85
|
+
Funky::Video.find('doesnotexist') # => raises Funky::ContentNotFound
|
86
|
+
```
|
87
|
+
|
88
|
+
### Connection error
|
89
|
+
|
90
|
+
Should there be a case where Funky is unable to connect to Facebook, `Funky::ConnectionError` will be raised.
|
91
|
+
|
92
|
+
```ruby
|
93
|
+
# Given funky is unable to establish a connection to Facebook
|
94
|
+
Funky::Video.find('10154439119663508') # => raises Funky::ConnectionError
|
95
|
+
Funky::Video.where(id: '10154439119663508') # => raises Funky::ConnectionError
|
96
|
+
```
|
97
|
+
|
98
|
+
## Development
|
99
|
+
|
100
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
101
|
+
|
102
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
103
|
+
|
104
|
+
## Contributing
|
105
|
+
|
106
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/fullscreen/funky. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
107
|
+
|
108
|
+
|
109
|
+
## License
|
110
|
+
|
111
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
112
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "funky"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
data/funky.gemspec
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'funky/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "funky"
|
8
|
+
spec.version = Funky::VERSION
|
9
|
+
spec.authors = ["Philip Nguyen"]
|
10
|
+
spec.email = ["philip.nguyen@fullscreen.net"]
|
11
|
+
|
12
|
+
spec.summary = "Funky is a Ruby library to fetch video data."
|
13
|
+
spec.description = "Funky is a Ruby library to fetch data about videos posted on Facebook, such as their title, description, number of views, comments, shares, and likes. Funky can obtain those public data regardless of whether you have insight permission. Funky is fully tested."
|
14
|
+
spec.homepage = "https://github.com/Fullscreen/funky"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
18
|
+
spec.bindir = "exe"
|
19
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
|
+
spec.require_paths = ["lib"]
|
21
|
+
|
22
|
+
spec.add_development_dependency "bundler", "~> 1.11"
|
23
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
24
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
25
|
+
spec.add_development_dependency "simplecov", "0.11.2"
|
26
|
+
spec.add_development_dependency "yard", "0.8.7.6"
|
27
|
+
spec.add_development_dependency 'coveralls', "0.8.13"
|
28
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
require 'funky/connections/base'
|
2
|
+
require 'json'
|
3
|
+
|
4
|
+
module Funky
|
5
|
+
# @api private
|
6
|
+
module Connection
|
7
|
+
class API < Base
|
8
|
+
def self.request(id:, fields:)
|
9
|
+
uri = URI::HTTPS.build host: host,
|
10
|
+
path: "/v2.6/#{id}",
|
11
|
+
query: "access_token=#{app_id}%7C#{app_secret}&fields=#{fields}"
|
12
|
+
response_for(get_http_request(uri), uri)
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.batch_request(ids:, fields:)
|
16
|
+
uri = URI::HTTPS.build host: host,
|
17
|
+
path: "/",
|
18
|
+
query: "include_headers=false&access_token=#{app_id}%7C#{app_secret}"
|
19
|
+
batch = create_batch_for ids, fields
|
20
|
+
http_request = post_http_request uri
|
21
|
+
http_request.set_form_data batch: batch.to_json
|
22
|
+
response_for(http_request, uri)
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
|
27
|
+
def self.host
|
28
|
+
'graph.facebook.com'
|
29
|
+
end
|
30
|
+
|
31
|
+
def self.app_id
|
32
|
+
Funky.configuration.app_id
|
33
|
+
end
|
34
|
+
|
35
|
+
def self.app_secret
|
36
|
+
Funky.configuration.app_secret
|
37
|
+
end
|
38
|
+
|
39
|
+
def self.post_http_request(uri)
|
40
|
+
Net::HTTP::Post.new uri
|
41
|
+
end
|
42
|
+
|
43
|
+
def self.create_batch_for(ids, fields)
|
44
|
+
ids.map do |id|
|
45
|
+
{"method":"GET", "relative_url": "/v2.6/#{id}?fields=#{fields}"}
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module Funky
|
2
|
+
# @api private
|
3
|
+
module Connection
|
4
|
+
class Base
|
5
|
+
|
6
|
+
private
|
7
|
+
|
8
|
+
def self.get_http_request(uri)
|
9
|
+
Net::HTTP::Get.new(uri.request_uri)
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.response_for(http_request, uri)
|
13
|
+
response = Net::HTTP.start(uri.host, 443, use_ssl: true) do |http|
|
14
|
+
http.request http_request
|
15
|
+
end
|
16
|
+
response
|
17
|
+
rescue SocketError => e
|
18
|
+
raise ConnectionError, e.message
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
data/lib/funky/errors.rb
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
module Funky
|
2
|
+
# @api private
|
3
|
+
module HTML
|
4
|
+
class Page
|
5
|
+
def get(video_id:)
|
6
|
+
body = response_for(video_id).body
|
7
|
+
if body.include? '<meta name="description"'
|
8
|
+
body
|
9
|
+
else
|
10
|
+
raise ContentNotFound, 'Please double check the ID and try again.'
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
private
|
15
|
+
|
16
|
+
def response_for(video_id)
|
17
|
+
uri = uri_for video_id
|
18
|
+
request = Net::HTTP::Get.new(uri.request_uri)
|
19
|
+
Net::HTTP.start(uri.host, 443, use_ssl: true) do |http|
|
20
|
+
http.request request
|
21
|
+
end
|
22
|
+
rescue SocketError => e
|
23
|
+
raise ConnectionError, e.message
|
24
|
+
end
|
25
|
+
|
26
|
+
def uri_for(video_id)
|
27
|
+
URI::HTTPS.build host: 'www.facebook.com',
|
28
|
+
path: '/video.php',
|
29
|
+
query: "v=#{video_id}"
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
module Funky
|
2
|
+
# @api private
|
3
|
+
module HTML
|
4
|
+
class Parser
|
5
|
+
def parse(html:)
|
6
|
+
{
|
7
|
+
view_count: extract_views_from(html),
|
8
|
+
share_count: extract_shares_from(html),
|
9
|
+
like_count: extract_likes_from(html),
|
10
|
+
comment_count: extract_comments_from(html)
|
11
|
+
}
|
12
|
+
end
|
13
|
+
|
14
|
+
def extract_shares_from(html)
|
15
|
+
html.match(/"sharecount":(.*?),/)
|
16
|
+
matched_count $1
|
17
|
+
end
|
18
|
+
|
19
|
+
def extract_views_from(html)
|
20
|
+
html.match(/<div><\/div><span class="fcg">(.*) Views<\/span>/)
|
21
|
+
matched_count $1
|
22
|
+
end
|
23
|
+
|
24
|
+
def extract_likes_from(html)
|
25
|
+
html.match /"likecount":(.*?),/
|
26
|
+
matched_count $1
|
27
|
+
end
|
28
|
+
|
29
|
+
def extract_comments_from(html)
|
30
|
+
html.match /"commentcount":(.*?),/
|
31
|
+
matched_count $1
|
32
|
+
end
|
33
|
+
|
34
|
+
private
|
35
|
+
|
36
|
+
def matched_count(matched)
|
37
|
+
matched ? matched.delete(',').to_i : nil
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
data/lib/funky/video.rb
ADDED
@@ -0,0 +1,133 @@
|
|
1
|
+
require 'funky/html/page'
|
2
|
+
require 'funky/html/parser'
|
3
|
+
|
4
|
+
module Funky
|
5
|
+
class Video
|
6
|
+
attr_reader :data
|
7
|
+
attr_accessor :counters
|
8
|
+
|
9
|
+
@@html_page = HTML::Page.new
|
10
|
+
@@html_parser = HTML::Parser.new
|
11
|
+
|
12
|
+
def initialize(data)
|
13
|
+
@data = data
|
14
|
+
end
|
15
|
+
|
16
|
+
# @return [String] the video ID.
|
17
|
+
def id
|
18
|
+
data[:id]
|
19
|
+
end
|
20
|
+
|
21
|
+
# @return [DateTime] the created time of the video.
|
22
|
+
def created_time
|
23
|
+
datetime = data[:created_time]
|
24
|
+
DateTime.parse datetime if datetime
|
25
|
+
end
|
26
|
+
|
27
|
+
# @return [String] the description of the video.
|
28
|
+
def description
|
29
|
+
data[:description]
|
30
|
+
end
|
31
|
+
|
32
|
+
# @return [Float] the length (duration) of the video.
|
33
|
+
def length
|
34
|
+
data[:length]
|
35
|
+
end
|
36
|
+
|
37
|
+
# @return [String] the picture URL of the video.
|
38
|
+
def picture
|
39
|
+
data[:picture]
|
40
|
+
end
|
41
|
+
|
42
|
+
# @return [Integer] the total number of likes for the video.
|
43
|
+
def like_count
|
44
|
+
data[:like_count]
|
45
|
+
end
|
46
|
+
|
47
|
+
# @return [Integer] the total number of comments for the video.
|
48
|
+
def comment_count
|
49
|
+
data[:comment_count]
|
50
|
+
end
|
51
|
+
|
52
|
+
# @return [Integer] the total number of shares for the video.
|
53
|
+
def share_count
|
54
|
+
data[:share_count]
|
55
|
+
end
|
56
|
+
|
57
|
+
# @return [Integer] the total number of views for the video.
|
58
|
+
def view_count
|
59
|
+
data[:view_count]
|
60
|
+
end
|
61
|
+
|
62
|
+
# Fetches the data from Facebook's APIs and instantiates the data
|
63
|
+
# into an Array of Funky::Video objects. It can accept one video ID or an
|
64
|
+
# array of multiple video IDs.
|
65
|
+
#
|
66
|
+
# @example Getting one video
|
67
|
+
# id = '10153834590672139'
|
68
|
+
# Funky::Video.where(id: id) # => [#<Funky::Video>]
|
69
|
+
# @example Getting multiple videos
|
70
|
+
# ids = ['10154439119663508', '10153834590672139']
|
71
|
+
# Funky::Video.where(id: ids) # => [#<Funky::Video>, #<Funky::Video>]
|
72
|
+
#
|
73
|
+
# @return [Array<Funky::Video>] multiple instances of Funky::Video objects
|
74
|
+
# containing data obtained by Facebook's APIs.
|
75
|
+
def self.where(id:)
|
76
|
+
return nil unless id
|
77
|
+
instantiate_collection(fetch_and_parse_data Array(id))
|
78
|
+
end
|
79
|
+
|
80
|
+
# Fetches the data from Facebook's HTML and instantiates the data
|
81
|
+
# into a single Funky::Video object. It can accept one only video ID.
|
82
|
+
#
|
83
|
+
# @example Getting a video
|
84
|
+
# Funky::Video.find('10153834590672139') # => #<Funky::Video>
|
85
|
+
#
|
86
|
+
# @return [Funky::Video] the data scraped from Facebook's HTML
|
87
|
+
# and encapsulated into a Funky::Video object.
|
88
|
+
def self.find(video_id)
|
89
|
+
counters = @@html_parser.parse html: @@html_page.get(video_id: video_id)
|
90
|
+
new counters.merge(id: video_id)
|
91
|
+
end
|
92
|
+
|
93
|
+
private
|
94
|
+
|
95
|
+
def self.fetch_and_parse_data(ids)
|
96
|
+
if ids.is_a?(Array) && ids.size > 1
|
97
|
+
response = Connection::API.batch_request(ids: ids, fields: fields)
|
98
|
+
else
|
99
|
+
id = ids.is_a?(Array) ? ids.first : ids
|
100
|
+
response = Connection::API.request(id: id, fields: fields)
|
101
|
+
end
|
102
|
+
parse response
|
103
|
+
end
|
104
|
+
|
105
|
+
def self.parse(response)
|
106
|
+
if response.code == '200'
|
107
|
+
body = JSON.parse response.body, symbolize_names: true
|
108
|
+
if body.is_a? Array
|
109
|
+
body.select{|item| item[:code] == 200}.collect do |item|
|
110
|
+
JSON.parse(item[:body], symbolize_names: true)
|
111
|
+
end.compact
|
112
|
+
else
|
113
|
+
[body]
|
114
|
+
end
|
115
|
+
else
|
116
|
+
raise ContentNotFound, 'Please check IDs'
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
def self.instantiate_collection(items)
|
121
|
+
items.collect { |item| new item }
|
122
|
+
end
|
123
|
+
|
124
|
+
def self.fields
|
125
|
+
[
|
126
|
+
'created_time',
|
127
|
+
'description',
|
128
|
+
'length',
|
129
|
+
'picture'
|
130
|
+
]
|
131
|
+
end
|
132
|
+
end
|
133
|
+
end
|
data/lib/funky.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'date'
|
2
|
+
require "funky/version"
|
3
|
+
require 'funky/errors'
|
4
|
+
require 'net/http'
|
5
|
+
require 'funky/connections/api'
|
6
|
+
require 'funky/configuration'
|
7
|
+
require "funky/video"
|
8
|
+
|
9
|
+
module Funky
|
10
|
+
def self.configuration
|
11
|
+
@configuration ||= Configuration.new
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.configure
|
15
|
+
yield configuration if block_given?
|
16
|
+
end
|
17
|
+
end
|
metadata
ADDED
@@ -0,0 +1,153 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: funky
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Philip Nguyen
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-05-26 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.11'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.11'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: simplecov
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - '='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 0.11.2
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - '='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 0.11.2
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: yard
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - '='
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: 0.8.7.6
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - '='
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: 0.8.7.6
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: coveralls
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - '='
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: 0.8.13
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - '='
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: 0.8.13
|
97
|
+
description: Funky is a Ruby library to fetch data about videos posted on Facebook,
|
98
|
+
such as their title, description, number of views, comments, shares, and likes.
|
99
|
+
Funky can obtain those public data regardless of whether you have insight permission.
|
100
|
+
Funky is fully tested.
|
101
|
+
email:
|
102
|
+
- philip.nguyen@fullscreen.net
|
103
|
+
executables: []
|
104
|
+
extensions: []
|
105
|
+
extra_rdoc_files: []
|
106
|
+
files:
|
107
|
+
- ".gitignore"
|
108
|
+
- ".rspec"
|
109
|
+
- ".travis.yml"
|
110
|
+
- CHANGELOG.md
|
111
|
+
- CODE_OF_CONDUCT.md
|
112
|
+
- Gemfile
|
113
|
+
- LICENSE.txt
|
114
|
+
- README.md
|
115
|
+
- Rakefile
|
116
|
+
- bin/console
|
117
|
+
- bin/setup
|
118
|
+
- funky.gemspec
|
119
|
+
- lib/funky.rb
|
120
|
+
- lib/funky/configuration.rb
|
121
|
+
- lib/funky/connections/api.rb
|
122
|
+
- lib/funky/connections/base.rb
|
123
|
+
- lib/funky/errors.rb
|
124
|
+
- lib/funky/html/page.rb
|
125
|
+
- lib/funky/html/parser.rb
|
126
|
+
- lib/funky/version.rb
|
127
|
+
- lib/funky/video.rb
|
128
|
+
homepage: https://github.com/Fullscreen/funky
|
129
|
+
licenses:
|
130
|
+
- MIT
|
131
|
+
metadata: {}
|
132
|
+
post_install_message:
|
133
|
+
rdoc_options: []
|
134
|
+
require_paths:
|
135
|
+
- lib
|
136
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
137
|
+
requirements:
|
138
|
+
- - ">="
|
139
|
+
- !ruby/object:Gem::Version
|
140
|
+
version: '0'
|
141
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
142
|
+
requirements:
|
143
|
+
- - ">="
|
144
|
+
- !ruby/object:Gem::Version
|
145
|
+
version: '0'
|
146
|
+
requirements: []
|
147
|
+
rubyforge_project:
|
148
|
+
rubygems_version: 2.5.1
|
149
|
+
signing_key:
|
150
|
+
specification_version: 4
|
151
|
+
summary: Funky is a Ruby library to fetch video data.
|
152
|
+
test_files: []
|
153
|
+
has_rdoc:
|