totalshares 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b7eb9662e622a3409879daaed4c21e5968e447fb
4
+ data.tar.gz: ac6539ef0b9e9625bc3f40c1b5076a99afcb72ae
5
+ SHA512:
6
+ metadata.gz: ac6049c15d2a98e206ec4411bdb1fd98b3199b8725dbf1403b95192377ce677bf23b4aa59cf441be0bc4ae758520de904878451f3cfdefc9797f169bbdb92d2f
7
+ data.tar.gz: c74c4b480576988ce991bad4d1a8415d1fc1190db31f462da6737d5ea2eb63823f0722ad8939a55720abd19e8d8cac086423dfe76a1afa934d2e8724ae076d9c
@@ -0,0 +1,39 @@
1
+ lib = File.expand_path("../../lib/", __FILE__)
2
+ $LOAD_PATH << lib
3
+ require 'totalshares'
4
+
5
+
6
+
7
+ webpage = Webpage.new "http://www.thegeekstuff.com/2014/10/linux-kvm-create-guest-vm/"
8
+
9
+ webpage.gplus :v => true
10
+ webpage.twitter :v => true
11
+ webpage.facebook :v => true
12
+ webpage.pinterest :v => true
13
+ webpage.stumbledupon :v => true
14
+ webpage.linkedin :v => true
15
+ webpage.reddit :v => true
16
+ webpage.shares_selected(["facebook", "linkedin", "twitter", "gplus"], :v => true)
17
+ webpage.all :v => true
18
+
19
+ puts "G-plus #{webpage.gplus}"
20
+ puts "twitter #{webpage.twitter}"
21
+ puts "facebook #{webpage.facebook}"
22
+ puts "pinterest #{webpage.pinterest}"
23
+ puts "stumbleupon #{webpage.stumbledupon}"
24
+ puts "linkedin #{webpage.linkedin}"
25
+ puts "reddit #{webpage.reddit}"
26
+ puts webpage.shares_selected(["facebook", "linkedin", "twitter", "gplus"])
27
+ puts webpage.all
28
+
29
+
30
+
31
+
32
+ website = Website.new("www.thegeekstuff.com")
33
+
34
+ puts website.facebook :depth_limit => 1, :v => true
35
+ puts website.twitter :depth_limit => 1
36
+ puts website.gplus :depth_limit => 1
37
+ puts website.shares_selected(["facebook", "linkedin", "twitter", "gplus"], :depth_limit => 1, :v => true)
38
+ puts website.all :depth_limit => 1, :v => true
39
+ puts website.all
@@ -0,0 +1,14 @@
1
+ module Totalshares
2
+ SOCIAL_URLS = {
3
+ "twitter" => "http://cdn.api.twitter.com/1/urls/count.json?url=",
4
+ "facebook" => "http://graph.facebook.com/?id=",
5
+ "linkedin" => "http://www.linkedin.com/countserv/count/share?format=json&url=",
6
+ "reddit" => "http://www.reddit.com/api/info.json?url=",
7
+ "stumbledupon" => "http://www.stumbleupon.com/services/1.01/badge.getinfo?url=",
8
+ "pinterest" => "http://api.pinterest.com/v1/urls/count.json?url=",
9
+ "gplus" => "https://clients6.google.com/rpc?key=AIzaSyCKSbrvQasunBoV16zDH9R33D88CeLr9gQ"
10
+ }
11
+ end
12
+
13
+ require 'website'
14
+ require 'webpage'
data/lib/webpage.rb ADDED
@@ -0,0 +1,100 @@
1
+ require 'typhoeus'
2
+ require 'json'
3
+ require 'yaml'
4
+ require 'totalshares'
5
+ class Webpage
6
+ include Totalshares
7
+ def initialize(url)
8
+ @url = url
9
+ @url ="http://#{url}" unless url.start_with? "https://" or url.start_with? "http://"
10
+ end
11
+
12
+ def twitter(opts = {})
13
+ output = Typhoeus.get(SOCIAL_URLS["twitter"] + @url)
14
+ count = JSON.parse(output.response_body)["count"].to_i || 0
15
+ puts "#{@url}\n#{count}" if opts[:v]
16
+ count
17
+ end
18
+
19
+ def facebook(opts = {})
20
+ output = Typhoeus.get(SOCIAL_URLS["facebook"] + @url)
21
+ count = JSON.parse(output.response_body)["shares"].to_i || 0
22
+ puts "#{@url}\n#{count}" if opts[:v]
23
+ count
24
+ end
25
+
26
+ def linkedin(opts = {})
27
+ output = Typhoeus.get(SOCIAL_URLS["linkedin"] + @url)
28
+ count = JSON.parse(output.response_body)["count"].to_i || 0
29
+ puts "#{@url}\n#{count}" if opts[:v]
30
+ count
31
+ end
32
+
33
+ def reddit(opts = {})
34
+ output = Typhoeus.get(SOCIAL_URLS["reddit"] + @url)
35
+ count = JSON.parse(output.response_body)["data"]["children"].size.to_i || 0
36
+ puts "#{@url}\n#{count}" if opts[:v]
37
+ count
38
+ end
39
+
40
+ def stumbledupon(opts = {})
41
+ output = Typhoeus.get(SOCIAL_URLS["stumbledupon"] + @url)
42
+ count = JSON.parse(output.response_body)["result"]["views"].to_i || 0
43
+ puts "#{@url}\n#{count}" if opts[:v]
44
+ count
45
+ end
46
+
47
+ def pinterest(opts = {})
48
+ output = Typhoeus.get(SOCIAL_URLS["pinterest"] + @url)
49
+ if output.response_code.eql? 200
50
+ output = output.response_body
51
+ json_output = output[output.index("{"), output.rindex("}")- (output.index("{")-1)]
52
+ count = JSON.parse(json_output)["count"].to_i || 0
53
+ puts "#{@url}\n#{count}" if opts[:v]
54
+ count
55
+ else
56
+ nil
57
+ end
58
+ end
59
+
60
+ def gplus(opts = {})
61
+ body = [
62
+ {
63
+ method: "pos.plusones.get",
64
+ id: "p",
65
+ params: {
66
+ nolog: true,
67
+ id: @url,
68
+ source: "widget",
69
+ userId: "@viewer",
70
+ groupId: "@self"
71
+ },
72
+ jsonrpc: "2.0",
73
+ key: "p",
74
+ apiVersion: "v1"
75
+ }
76
+ ]
77
+ output = Typhoeus.post(SOCIAL_URLS["gplus"], body: JSON.dump(body), headers: { "content-type" => "application/json", "accept" => "application/json"})
78
+ count = JSON.parse(output.response_body)[0]["result"]["metadata"]["globalCounts"]["count"].to_i || 0
79
+ puts "#{@url}\n#{count}" if opts[:v]
80
+ count
81
+ end
82
+
83
+ def all(opts = {})
84
+ response = {}
85
+ SOCIAL_URLS.each do |key, value|
86
+ response[key] = self.send "#{key}"
87
+ end
88
+ puts "#{@url}\n#{response}" if opts[:v]
89
+ response
90
+ end
91
+
92
+ def shares_selected(social_networks,opts = {})
93
+ response = {}
94
+ social_networks.each do |sn|
95
+ response[sn] = self.send "#{sn}" unless SOCIAL_URLS[sn].nil? or SOCIAL_URLS[sn] == ""
96
+ end
97
+ puts "#{@url}\n#{response}" if opts[:v]
98
+ response
99
+ end
100
+ end
data/lib/website.rb ADDED
@@ -0,0 +1,92 @@
1
+ require 'webpage'
2
+ require 'anemone'
3
+ require 'totalshares'
4
+ class Website
5
+ include Totalshares
6
+ def initialize(url)
7
+ @url = url
8
+ @url ="http://#{url}" unless url.start_with? "https://" or url.start_with? "http://"
9
+ SOCIAL_URLS.each do |key, value|
10
+ instance_variable_set("@#{key}_count", 0)
11
+ self.class.send(:define_method, "#{key}") {|opts={}| self.shares(key, opts)}
12
+ self.class.send(:define_method, "all") {|opts={}| self.shares("all", opts)}
13
+ end
14
+ end
15
+
16
+ def shares_selected(social_networks, opts = {})
17
+ social_networks.each do |sn|
18
+ instance_variable_set("@#{sn}_count", 0)
19
+ end
20
+ output = {}
21
+ opts[:skip_query_strings] = true
22
+ Anemone.crawl(@url, opts) do | anemone |
23
+ anemone.on_every_page do |page|
24
+ webpage = Webpage.new(page.url.to_s)
25
+ count = webpage.send("shares_selected",social_networks)
26
+ print_flag=false
27
+ count.each do |key,value|
28
+ print_flag=true if value > 0
29
+ sum_of_count = instance_variable_get("@#{key}_count") + value
30
+ instance_variable_set("@#{key}_count", sum_of_count)
31
+ end
32
+ puts "#{page.url.to_s}\n#{count}" if print_flag and opts[:v]
33
+ output[page.url.to_s]=count if print_flag
34
+ end
35
+ end
36
+ total=0
37
+ social_networks.each do |sn|
38
+ variable_name = "@#{sn}_count"
39
+ puts "#{sn}: #{instance_variable_get(variable_name)}"
40
+ total += instance_variable_get(variable_name)
41
+ end
42
+ puts "Total Shares: #{total}" if opts[:v]
43
+ return output, total
44
+ end
45
+
46
+ protected
47
+ def shares(method_name, opts = {})
48
+ SOCIAL_URLS.each do |key, value|
49
+ instance_variable_set("@#{key}_count", 0)
50
+ end
51
+ output = {}
52
+ opts[:skip_query_strings] = true
53
+ Anemone.crawl(@url, opts) do | anemone |
54
+ anemone.on_every_page do |page|
55
+ webpage = Webpage.new(page.url.to_s)
56
+ count = webpage.send("#{method_name}")
57
+
58
+ if method_name.eql?("all")
59
+ print_flag=false
60
+ count.each do |key,value|
61
+ print_flag=true if value > 0
62
+ sum_of_count = instance_variable_get("@#{key}_count") + value
63
+ instance_variable_set("@#{key}_count", sum_of_count)
64
+ end
65
+ puts "#{page.url.to_s}\n#{count}" if print_flag and opts[:v]
66
+ output[page.url.to_s] = count if print_flag
67
+ else
68
+ puts "#{page.url.to_s}\n#{count}" if !(count.eql? 0) and opts[:v]
69
+ output[page.url.to_s] = count if !(count.eql? 0)
70
+
71
+ sum_of_count = instance_variable_get("@#{method_name}_count") + count
72
+ instance_variable_set("@#{method_name}_count", sum_of_count)
73
+ end
74
+
75
+ end
76
+ end
77
+
78
+ if method_name.eql?("all")
79
+ total=0
80
+ SOCIAL_URLS.each do |key, value|
81
+ variable_name = "@#{key}_count"
82
+ puts "#{key}: #{instance_variable_get(variable_name)}" if opts[:v]
83
+ total += instance_variable_get(variable_name)
84
+ end
85
+ puts "Total Shares: #{total}" if opts[:v]
86
+ return output, total
87
+ else
88
+ puts "#{method_name}: #{instance_variable_get("@#{method_name}_count")}" if opts[:v]
89
+ return output, instance_variable_get("@#{method_name}_count")
90
+ end
91
+ end
92
+ end
metadata ADDED
@@ -0,0 +1,89 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: totalshares
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Prashant Kumar
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-10-26 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: typhoeus
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.6'
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 0.6.9
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: '0.6'
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 0.6.9
33
+ - !ruby/object:Gem::Dependency
34
+ name: anemone
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '0.7'
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: 0.7.2
43
+ type: :runtime
44
+ prerelease: false
45
+ version_requirements: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - "~>"
48
+ - !ruby/object:Gem::Version
49
+ version: '0.7'
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: 0.7.2
53
+ description: Find out the total no. of shares of a website or webpage on all major
54
+ social networks.
55
+ email: prashant.kumar.312@gmail.com
56
+ executables: []
57
+ extensions: []
58
+ extra_rdoc_files: []
59
+ files:
60
+ - examples/example.rb
61
+ - lib/totalshares.rb
62
+ - lib/webpage.rb
63
+ - lib/website.rb
64
+ homepage: https://github.com/Prashant-kumar/totalshares
65
+ licenses:
66
+ - MIT
67
+ metadata: {}
68
+ post_install_message:
69
+ rdoc_options: []
70
+ require_paths:
71
+ - lib
72
+ required_ruby_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ required_rubygems_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ requirements: []
83
+ rubyforge_project:
84
+ rubygems_version: 2.2.1
85
+ signing_key:
86
+ specification_version: 4
87
+ summary: Find out the total no. of shares of a website or webpage.
88
+ test_files: []
89
+ has_rdoc: