· 7 years ago · Mar 21, 2018, 01:28 AM
1class Grabbers::Twitter < Grabbers::Base
2 def build_client(access_token, secret_key)
3 @api_client ||= Twitter::Client.new(oauth_token: access_token, oauth_token_secret: secret_key)
4 end
5
6 def grab_endpoint(endpoint, options={})
7 result = nil
8 endpoint_params = build_endpoint_params(endpoint)
9 begin
10 options = prepare_options(endpoint, endpoint_params, options)
11 result = self.send(endpoint_params['method'], options)
12 rescue Twitter::Error::Unauthorized => error
13 tokens_error(endpoint_params['method'], error)
14 rescue Twitter::Error::BadRequest => error
15 if error.to_s == 'Bad Authentication data'
16 tokens_error(endpoint_params['method'], error)
17 else
18 ErrorsService.log(error, title: "Error on Grab endpoint for #{service}", social_profile: social_profile,
19 data: {service: service, endpoint: endpoint, options: options, access_token: @access_token, secret_key: @secret_key})
20 common_error(endpoint_params['method'], error)
21 end
22 rescue Twitter::Error::TooManyRequests => error
23 rate_limit_error(endpoint_params['method'], error)
24 rescue Twitter::Error::ServerError => error
25 rate_limit_error(endpoint_params['method'], error, 10.minutes.to_i)
26 rescue JSON::ParserError => error
27 rate_limit_error(endpoint_params['method'], error, 15.seconds.to_i)
28 rescue Exception => error
29 common_error(endpoint_params['method'], error, options)
30 end
31
32 result
33 end
34
35 protected
36
37 def prepare_options(endpoint, endpoint_params, options)
38 result = {}
39 result[:count] = per_process_limit(options)
40 result[:max_id] = options[:until] if options[:until].present?
41 result[:since_id] = options[:since] if options[:since].present?
42
43 result
44 end
45
46 def service
47 'twitter'
48 end
49
50 def user(options)
51 [@api_client.user.attrs]
52 end
53
54 def friends(options)
55 begin
56 options[:count] = 200 #set manually for obtain maximum friends.
57 build_data_by_attributes @api_client.friends(options) # Will raised error if user has more than 3000 friends
58 rescue Exception => error
59 return []
60 end
61 end
62
63 def mentions_timeline(options)
64 build_data_by_attributes @api_client.mentions_timeline(options)
65 end
66
67 def е(options)
68 build_data_by_attributes @api_client.home_timeline(options)
69 end
70
71 def user_timeline(options)
72 build_data_by_attributes @api_client.user_timeline(options)
73 end
74
75 def build_data_by_attributes(data)
76 result = []
77 data.each do |resource|
78 result << resource.attrs
79 end
80
81 result
82 end
83
84 def endpoints_with_pagination
85 %w(home feed)
86 end
87
88end