neo4apis-twitter 0.7.0 → 0.8.1

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: 7c8be4b5bfaae76a81157797e34dd4565a939ec2
4
- data.tar.gz: 10542478383cdcf161003cc746ec4e20414d5f2d
3
+ metadata.gz: 4af92daa4a378b1ced8de1deb45ca1a9b551e637
4
+ data.tar.gz: dd5db05d098883b4699674761ad2801232ecbc2c
5
5
  SHA512:
6
- metadata.gz: 503ba611328dc21c6fcfb03e84c1efb100899a356a83dcb44303003fd7cfc8857a859806c6ae48a0119965d99147328687f981c43a4e4e8893675dad5ac93b65
7
- data.tar.gz: cdf50c105718208e443888792dae59c50099f27f6b00f4ac11c07e9275f6673af63017198db9b826d07dac70e38cf44671488df2835c8d4a996e934f090536cb
6
+ metadata.gz: ecc8240fed4ca7038db29ebb7bda7e1848aa0a6e5b7ca365604c839bd56ff0b33fefa5d0ae491454938c8625c193999d843ef24234bd402f8ab57be60e930a21
7
+ data.tar.gz: 6004713148e4a3848b3f45c7f006648186a5872e7e4c2048869b7d0d490530e0d29d3cbe72dc152ffa2147258ca854f0817d953534660e10f92b4ab9d57ed6aa
@@ -0,0 +1,7 @@
1
+ ---
2
+ :consumer_key: iwmENsqBZNAeN07hsrGuGwKMa
3
+ :consumer_secret: ioVqcH3pxTrTq5or5cpXdj4WJpUO1ZNjMRr3tmhFg4xT1mOQXa
4
+ :access_token: 2776665793-QPDyINltYv3hGa9RjmMeAbxFSHXJ764n0ycOu1E
5
+ :access_token_secret: hlxs1wONx6lJ7GLLY0nXm630mv6NzPHOhDlirCvqetCip
6
+
7
+
@@ -4,7 +4,7 @@ require 'colorize'
4
4
 
5
5
  module Neo4Apis
6
6
  module CLI
7
- class Twitter < Thor
7
+ class Twitter < Base
8
8
  class_option :config_path, type: :string, default: 'config/twitter.yml'
9
9
 
10
10
  class_option :import_retweets, type: :boolean, default: false
@@ -27,18 +27,93 @@ module Neo4Apis
27
27
  desc "search QUERY COUNT", "Import tweets via a search query"
28
28
  def search(query, count)
29
29
  neo4apis_client.batch do
30
- twitter_client(false).search(query, options).take(count.to_i).each(&tweet_importer)
30
+ twitter_client.search(query, options).take(count.to_i).each(&tweet_importer)
31
+ end
32
+ end
33
+
34
+ desc "followers USERNAME DEPTH", "Imports followers (and optionally followers of followers if depth specified and with a value > 1"
35
+ def followers(user, depth = 1)
36
+ neo4apis_client.batch do
37
+ follower_importer(neo4apis_client, user, depth.to_i)
38
+ end
39
+ end
40
+
41
+ desc "friends USERNAME DEPTH", "Imports friends (and optionally friends of friends if depth specified and with a value > 1"
42
+ def friends(user, depth = 1)
43
+ neo4apis_client.batch do
44
+ friend_importer(neo4apis_client, user, depth.to_i)
31
45
  end
32
46
  end
33
47
 
34
48
  private
35
49
 
50
+ def follower_importer(neo4apis_client, user, depth)
51
+ friend_follower_importer(:followers, neo4apis_client, user, depth)
52
+ end
53
+
54
+ def friend_importer(neo4apis_client, user, depth)
55
+ friend_follower_importer(:friends, neo4apis_client, user, depth)
56
+ end
57
+
58
+ def friend_follower_importer(type, neo4apis_client, user, depth)
59
+ twitter_user = if user == ::Twitter::User
60
+ user
61
+ else
62
+ throttled_block { twitter_client.user(user) }
63
+ end
64
+ user_node = user_importer.call(twitter_user)
65
+
66
+ relationship = (type == :friends ? :friended : :follows)
67
+ get_friends_or_followers(type, twitter_user).each do |other_user|
68
+ other_user_node = user_importer.call(other_user)
69
+
70
+ neo4apis_client.add_relationship(relationship, other_user_node, user_node)
71
+
72
+ if depth > 1
73
+ friend_follower_importer(type, neo4apis_client, other_user, depth - 1)
74
+ end
75
+ end
76
+ end
77
+
78
+ def get_friends_or_followers(type, user)
79
+ [].tap do |user_list|
80
+ next_cursor = -1
81
+ while next_cursor != 0
82
+ result = throttled_block { twitter_client.send(type, user, cursor: next_cursor, count: 5000) }
83
+ throttled_block { user_list.concat(result.take(5000)) }
84
+ next_cursor = result.send(:next_cursor)
85
+ end
86
+ end
87
+ end
88
+
89
+ def throttled_block
90
+ yield
91
+ rescue ::Twitter::Error::TooManyRequests => error
92
+ reset_in = error.rate_limit.reset_in
93
+ puts "Rate limit exceeded. Sleeping for #{reset_in} seconds"
94
+ sleep reset_in
95
+ retry
96
+ end
97
+
36
98
  def tweet_importer
99
+ @tweet_importer ||= object_importer(::Twitter::Tweet, :Tweet)
100
+ end
101
+
102
+ def user_importer
103
+ @user_importer ||= object_importer(::Twitter::User, :User)
104
+ end
105
+
106
+ def object_importer(gem_class, label)
37
107
  Proc.new do |object|
38
108
  case object
39
- when ::Twitter::Tweet
40
- say "got tweet from @#{object.user.screen_name.colorize(:light_blue)}: #{object.text}"
41
- neo4apis_client.import :Tweet, object
109
+ when gem_class
110
+ case label
111
+ when :Tweet
112
+ say "got tweet from @#{object.user.screen_name.colorize(:light_blue)}: #{object.text}"
113
+ when :User
114
+ say "get user ##{object.id}"
115
+ end
116
+ neo4apis_client.import label, object
42
117
  when ::Twitter::Streaming::StallWarning
43
118
  say "FALLING BEHIND!".colorize(:red)
44
119
  end
@@ -48,10 +123,10 @@ module Neo4Apis
48
123
  NEO4APIS_CLIENT_CLASS = ::Neo4Apis::Twitter
49
124
 
50
125
  def neo4apis_client
51
- @neo4apis_client ||= NEO4APIS_CLIENT_CLASS.new(Neo4j::Session.open(:server_db, parent_options[:neo4j_url]), import_retweets: options[:import_retweets], import_hashtags: options[:import_hashtags], import_user_mentions: options[:import_user_mentions])
126
+ @neo4apis_client ||= NEO4APIS_CLIENT_CLASS.new(neo4j_session, import_retweets: options[:import_retweets], import_hashtags: options[:import_hashtags], import_user_mentions: options[:import_user_mentions])
52
127
  end
53
128
 
54
- def twitter_client(streaming)
129
+ def twitter_client(streaming = false)
55
130
  return @twitter_client if @twitter_client
56
131
 
57
132
  twitter_client_class = streaming ? ::Twitter::Streaming::Client : ::Twitter::REST::Client
@@ -3,7 +3,7 @@ require 'ostruct'
3
3
 
4
4
  module Neo4Apis
5
5
  class Twitter < Base
6
- prefix :Twitter
6
+ common_label :Twitter
7
7
 
8
8
  uuid :Tweet, :id
9
9
  uuid :User, :id
@@ -3,7 +3,7 @@ $:.unshift lib unless $:.include?(lib)
3
3
 
4
4
  Gem::Specification.new do |s|
5
5
  s.name = "neo4apis-twitter"
6
- s.version = '0.7.0'
6
+ s.version = '0.8.1'
7
7
  s.required_ruby_version = ">= 1.9.1"
8
8
 
9
9
  s.authors = "Brian Underwood"
@@ -18,7 +18,7 @@ A ruby gem using neo4apis to make importing twitter data to neo4j easy
18
18
  s.require_path = 'lib'
19
19
  s.files = Dir.glob("{bin,lib,config}/**/*") + %w(README.md Gemfile neo4apis-twitter.gemspec)
20
20
 
21
- s.add_dependency('neo4apis', '~> 0.4.0')
21
+ s.add_dependency('neo4apis', '>= 0.8.0')
22
22
  s.add_dependency('twitter', '~> 5.12.0')
23
23
 
24
24
  end
metadata CHANGED
@@ -1,29 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: neo4apis-twitter
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.0
4
+ version: 0.8.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brian Underwood
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-11-09 00:00:00.000000000 Z
11
+ date: 2015-03-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: neo4apis
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: 0.4.0
19
+ version: 0.8.0
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - "~>"
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: 0.4.0
26
+ version: 0.8.0
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: twitter
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -47,6 +47,7 @@ extra_rdoc_files: []
47
47
  files:
48
48
  - Gemfile
49
49
  - README.md
50
+ - config/twitter.yml
50
51
  - lib/neo4apis-twitter.rb
51
52
  - lib/neo4apis/cli/twitter.rb
52
53
  - lib/neo4apis/twitter.rb
@@ -71,7 +72,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
71
72
  version: '0'
72
73
  requirements: []
73
74
  rubyforge_project:
74
- rubygems_version: 2.2.2
75
+ rubygems_version: 2.4.5
75
76
  signing_key:
76
77
  specification_version: 4
77
78
  summary: An ruby gem to import twitter data to neo4j