social_net 0.2.14 → 0.2.16

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 47e0336b763fff5d86cc513d217b9edf209d5842
4
- data.tar.gz: 0ebe4d1772c16e296b327a7837c0e3b68b4bd54f
3
+ metadata.gz: 9af92a897ea599c53f2c4d5daf3d7db4b9da5ccb
4
+ data.tar.gz: 788410dc09c02b2122c5f859bf60156e92cae025
5
5
  SHA512:
6
- metadata.gz: 7416f3d78c1f3fc716a5eebc7b208ee91549a4a724dff8a3eec83ab988307fb0994956e3c6ec319f3d2225f4fbebbb8dc5db75a5482a762a44553640af7b7511
7
- data.tar.gz: 46dd1de90ae89aade9364d969a96fbd1ae23bac7efc3717d0421dab19aff79f2549223f28f94e4bed9095d38a31f4d21b244b18cbafcdd18ce279d1a81a5d260
6
+ metadata.gz: fb6637e9108fe032dc54eea19ed5fa6574672943d0a86be25288026860682dcd659493394cad072022b9169f28543a2f0c9bfa26a63bd561e8a62773f5508f04
7
+ data.tar.gz: f9a10d8a6a99325229e442bc076939deb53a7a64423e57a956654de2bff3ce82a958295370ef80ce9535214cdfdf63ec60756b6f5f2c401fc65605e52adb4ac7
data/CHANGELOG.md CHANGED
@@ -58,3 +58,11 @@ For more information about changelogs, check
58
58
  ## 0.2.14 - 2018-06-05
59
59
 
60
60
  * [BUGFIX] Fix attribute readers bug
61
+
62
+ ## 0.2.15 - 2018-06-15
63
+
64
+ * [FEATURE] Add `Facebook::Video` model and support for `find_by_username_and_video_id`.
65
+
66
+ ## 0.2.16 - 2018-06-21
67
+
68
+ * [FEATURE] Add `Facebook::User` support for `find_video`.
@@ -0,0 +1,72 @@
1
+ require 'social_net/facebook/errors/response_error'
2
+ require 'social_net/facebook/errors/unknown_video'
3
+ require 'active_support'
4
+ require 'active_support/core_ext'
5
+ require 'nokogiri'
6
+
7
+ module SocialNet
8
+ module Facebook
9
+ module Api
10
+ class ScrapeRequest
11
+ def initialize(attrs = {})
12
+ @host = 'www.facebook.com'
13
+ @username = attrs[:username]
14
+ @video_id = attrs[:video_id]
15
+ @path = attrs.fetch :path, "/#{attrs[:username]}/videos/#{attrs[:video_id]}/"
16
+ @method = attrs.fetch :method, :get
17
+ end
18
+
19
+ def run
20
+ print "#{as_curl}\n"
21
+ case response = run_http_request
22
+ when Net::HTTPOK
23
+ data_string = Nokogiri::HTML response.body
24
+ parse_video_data data_string
25
+ else
26
+ raise Errors::ResponseError, response
27
+ end
28
+ end
29
+
30
+ private
31
+ def run_http_request
32
+ Net::HTTP.start(uri.host, uri.port, use_ssl: true) do |http|
33
+ http.request http_request
34
+ end
35
+ end
36
+
37
+ def http_request
38
+ http_class = "Net::HTTP::#{@method.capitalize}".constantize
39
+ @http_request ||= http_class.new(uri.request_uri)
40
+ end
41
+
42
+ def uri
43
+ @uri ||= URI::HTTPS.build host: @host, path: @path
44
+ end
45
+
46
+ def parse_video_data(data)
47
+ m = data.content.match(/hd_src:"([^\\"]*)"/)
48
+
49
+ raise Errors::UnknownVideo unless m
50
+ {}.tap do |video|
51
+ video['id'] = @video_id
52
+ video['video_url'] = data.at("meta[property='og:url']")['content']
53
+ video['link'] = m[1]
54
+ video['caption'] = data.at("meta[property='og:description']")['content']
55
+ video['thumbnail_url'] = data.at("meta[property='og:image']")['content']
56
+ end
57
+ end
58
+
59
+ def as_curl
60
+ 'curl'.tap do |curl|
61
+ curl << " -X #{http_request.method}"
62
+ http_request.each_header do |name, value|
63
+ curl << %Q{ -H "#{name}: #{value}"}
64
+ end
65
+ curl << %Q{ -d '#{http_request.body}'} if http_request.body
66
+ curl << %Q{ "#{@uri.to_s}"}
67
+ end
68
+ end
69
+ end
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,11 @@
1
+ module SocialNet
2
+ module Facebook
3
+ module Errors
4
+ class UnknownVideo < StandardError
5
+ def message
6
+ 'Unknown video'
7
+ end
8
+ end
9
+ end
10
+ end
11
+ end
@@ -1,2 +1,3 @@
1
1
  require 'social_net/facebook/errors/response_error'
2
2
  require 'social_net/facebook/errors/unknown_user'
3
+ require 'social_net/facebook/errors/unknown_video'
@@ -1,16 +1,19 @@
1
1
  require 'social_net/facebook/api/request'
2
+ require 'social_net/facebook/api/scrape_request'
2
3
  require 'social_net/facebook/errors'
4
+ require 'social_net/facebook/models/video'
3
5
 
4
6
  module SocialNet
5
7
  module Facebook
6
8
  module Models
7
9
  class User
8
- attr_reader :id, :email, :gender, :first_name, :last_name, :access_token
10
+ attr_reader :id, :email, :gender, :user_name, :first_name, :last_name, :access_token
9
11
 
10
12
  def initialize(attrs = {})
11
13
  @id = attrs['id']
12
14
  @email = attrs['email']
13
15
  @gender = attrs['gender']
16
+ @user_name = attrs[:user_name]
14
17
  @first_name = attrs['first_name']
15
18
  @last_name = attrs['last_name']
16
19
  @access_token = attrs['access_token']
@@ -22,6 +25,17 @@ module SocialNet
22
25
  page_json['data'].map { |h| h.slice("name", "id") } if page_json['data'].any?
23
26
  end
24
27
 
28
+ def find_video(id)
29
+ request = Api::ScrapeRequest.new video_id: id, username: @user_name
30
+ video = request.run
31
+ Models::Video.new video
32
+ rescue Errors::ResponseError => error
33
+ case error.response
34
+ when Net::HTTPBadRequest then raise Errors::UnknownVideo
35
+ when Net::HTTPNotFound then raise Errors::UnknownVideo
36
+ end
37
+ end
38
+
25
39
  # Returns the existing Facebook user matching the provided attributes or
26
40
  # nil when the user is not found.
27
41
  #
@@ -0,0 +1,60 @@
1
+ require 'social_net/facebook/api/request'
2
+ require 'social_net/facebook/errors'
3
+
4
+ module SocialNet
5
+ module Facebook
6
+ module Models
7
+ class Video
8
+ attr_reader :id, :link, :video_url, :thumbnail_url, :caption
9
+
10
+ def initialize(attrs={})
11
+ @id = attrs['id']
12
+ @link = attrs['link']
13
+ @video_url = attrs['video_url']
14
+ @thumbnail_url = attrs['thumbnail_url']
15
+ @caption = attrs['caption'] if attrs['caption']
16
+ end
17
+
18
+ # Returns the existing Facebook video matching the provided attributes or
19
+ # nil when the video is not found.
20
+ #
21
+ # @return [SocialNet::Facebook::Models::Video] when the video is found.
22
+ # @return [nil] when the video is not found.
23
+ # @param [Hash] params the attributes to find a video by.
24
+ # @option params [String] :media_id The Facebook video's media id.
25
+ def self.find_by(params = {})
26
+ find_by! params
27
+ rescue Errors::UnknownVideo
28
+ nil
29
+ end
30
+
31
+ # Returns the existing Facebook video matching the provided attributes or
32
+ # raises an error when the video is not found.
33
+ #
34
+ # @return [SocialNet::Facebook::Models::Video] when the video is found.
35
+ # @return [nil] when the video is not found.
36
+ # @param [Hash] params the attributes to find a video by.
37
+ # @option params [String] :media_id The Facebook video's media id.
38
+ # @raise [SocialNet::Errors::UnknownVideo] if the video is not found.
39
+ def self.find_by!(params = {})
40
+ if params[:video_id] && params[:username]
41
+ find_by_username_and_video_id! params[:username], params[:video_id]
42
+ end
43
+ end
44
+
45
+ private
46
+
47
+ def self.find_by_username_and_video_id!(username, video_id)
48
+ request = Api::ScrapeRequest.new username: username, video_id: video_id
49
+ video = request.run
50
+ new video
51
+ rescue Errors::ResponseError => error
52
+ case error.response
53
+ when Net::HTTPBadRequest then raise Errors::UnknownVideo
54
+ when Net::HTTPNotFound then raise Errors::UnknownVideo
55
+ end
56
+ end
57
+ end
58
+ end
59
+ end
60
+ end
@@ -1,2 +1,3 @@
1
1
  require 'social_net/facebook/models/page'
2
2
  require 'social_net/facebook/models/user'
3
+ require 'social_net/facebook/models/video'
@@ -1,3 +1,3 @@
1
1
  module SocialNet
2
- VERSION = "0.2.14"
2
+ VERSION = "0.2.16"
3
3
  end
@@ -1,8 +1,11 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe SocialNet::Facebook::User do
3
+ describe SocialNet::Facebook::User, :vcr do
4
4
  let(:unknown_username) { 'qeqwe09qlkmhkjh' }
5
5
  let(:existing_username) { '1453280268327112' }
6
+ let(:existing_handle) { 'collabcreators' }
7
+ let(:existing_video) { '1596466537097289' }
8
+ let(:unknown_video) { '1596466537097288' }
6
9
 
7
10
  describe '.find_by' do
8
11
  subject(:user) { SocialNet::Facebook::User.find_by username: username }
@@ -41,4 +44,29 @@ describe SocialNet::Facebook::User do
41
44
  it { expect{user}.to raise_error SocialNet::Facebook::UnknownUser }
42
45
  end
43
46
  end
47
+
48
+ describe '.find_video' do
49
+ subject(:facebook_video) do
50
+ user = SocialNet::Facebook::User.new user_name: username
51
+ user.find_video video
52
+ end
53
+
54
+ context 'given an existing video id' do
55
+ let(:username) { existing_handle }
56
+ let(:video) { existing_video }
57
+
58
+ it 'returns an object representing a video' do
59
+ expect(facebook_video.id).to eq '1596466537097289'
60
+ expect(facebook_video.video_url).to eq 'https://www.facebook.com/collabcreators/videos/1596466537097289/'
61
+ expect(facebook_video.link).to be_present
62
+ end
63
+ end
64
+
65
+ context 'given a nonexistant video video_id' do
66
+ let(:username) { existing_handle }
67
+ let(:video) { unknown_video }
68
+
69
+ it { expect{facebook_video}.to raise_error SocialNet::Facebook::UnknownVideo }
70
+ end
71
+ end
44
72
  end
@@ -0,0 +1,28 @@
1
+ require 'spec_helper'
2
+ require 'social_net/facebook'
3
+
4
+ describe SocialNet::Facebook::Video, :vcr do
5
+ let(:existing_video_id) { '1596466537097289' }
6
+ let(:unknown_video_id) { '1596466537097288' }
7
+ let(:username){ 'collabcreators' }
8
+ describe '.find_by! id' do
9
+ subject(:video) do
10
+ SocialNet::Facebook::Video.find_by!(username: username, video_id: video_id)
11
+ end
12
+
13
+ context 'given an existing public video\'s video id' do
14
+ let(:video_id) { existing_video_id }
15
+
16
+ it 'returns an object representing that video' do
17
+ expect(video.id).to eq video_id
18
+ expect(video.video_url).to eq 'https://www.facebook.com/collabcreators/videos/1596466537097289/'
19
+ expect(video.link).to be_present
20
+ end
21
+ end
22
+
23
+ context 'given a nonexistant video video_id' do
24
+ let(:video_id) { unknown_video_id }
25
+ it { expect{video}.to raise_error SocialNet::Facebook::UnknownVideo }
26
+ end
27
+ end
28
+ end