github-calendar 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: 856e63c23cb5514fbc7977cbb20c65b0e4124c7f
4
+ data.tar.gz: 09737875de871692c12bceb518d5d9f7c8f86311
5
+ SHA512:
6
+ metadata.gz: 50c57e384c669352a51e2a69d6cdb23972135d7fc2ef7799a6efe9ba99c6f212a496479a693f09f4e4bbdc34cf5d3bc86b1bb2fda8fe986a59ecff52f95de455
7
+ data.tar.gz: 854848db35f10e0ead23a74e28b286fd78b10809abf77daae168d14ef81667d21c1ec97bc5bfb73289a05b7543f6c139636c2d8e853dde0b65e823df08eec131
data/CHANGELOG.md ADDED
@@ -0,0 +1,4 @@
1
+ # Changelog
2
+ ## Version 1
3
+ ### Version 1.0.0
4
+ * Initial release.
@@ -0,0 +1,166 @@
1
+ require 'open-uri'
2
+ require 'nokogiri'
3
+ require 'string-utility'
4
+ require_relative 'exceptions'
5
+
6
+ module GitHub
7
+ module Calendar
8
+ extend self
9
+ using StringUtility
10
+
11
+ # Gets the total number of contributions for the past year.
12
+ # @param user [String] The username to get this data for.
13
+ # @return [Int] An integer value of their total contributions this year.
14
+ def get_total_year(user)
15
+ source = get_page_source(user)
16
+ string = source.css('span.contrib-number')[0].text
17
+ string.to_i_separated
18
+ end
19
+
20
+ # Gets the longest contribution streak (in days) that the user has had.
21
+ # @param user [String] See #get_total_year
22
+ # @return [Int] The user's longest contribution streak in days.
23
+ def get_longest_streak(user)
24
+ source = get_page_source(user)
25
+ string = source.css('span.contrib-number')[1].text
26
+ string.to_i_separated
27
+ end
28
+
29
+ # Gets the current contribution streak (in days) that the user is in.
30
+ # @param user [String] See #get_total_year
31
+ # @return [Int] The user's current contribution streak in days.
32
+ def get_current_streak(user)
33
+ source = get_page_source(user)
34
+ string = source.css('span.contrib-number')[2].text
35
+ string.to_i_separated
36
+ end
37
+
38
+ # Gets the weekly contribution count for the past year.
39
+ # @param user [String] See #get_total_year
40
+ # @return [Hash] How many contributions they have done each week. Example:
41
+ # { 0: 0, 1: 8, etc. }
42
+ def get_weekly(user)
43
+ weeks = get_calendar(user)
44
+ ret = {}
45
+ count = 0
46
+ weeks[1..-1].each do |k|
47
+ data = 0
48
+ capture = k.to_s.scan(/data-count=\"(.*?)\"/)
49
+ capture[1..-1].each do |ints|
50
+ data += ints[0].to_i
51
+ end
52
+ ret[count] = data
53
+ count += 1
54
+ end
55
+ ret
56
+ end
57
+
58
+ # Gets the daily contribution count for the past year.
59
+ # @param user [String] See #get_total_year
60
+ # @return [Hash] How many contributions they have performed each day. See
61
+ # #get_weekly
62
+ def get_daily(user)
63
+ weeks = get_calendar(user)
64
+ ret = {}
65
+ count = 0
66
+ weeks[1..-1].each do |k|
67
+ capture = k.to_s.scan(/data-count=\"(.*?)\"/)
68
+ capture[1..-1].each do |i|
69
+ ret[count] = i[0].to_i
70
+ count += 1
71
+ end
72
+ end
73
+ ret
74
+ end
75
+
76
+ # Gets the monthly contribution count for the past year.
77
+ # @param user [String] See #get_total_year
78
+ # @return [Hash] How many contributions they have performed each month.
79
+ # Months are listed as their string integers, e.g., 01 is January and
80
+ # 12 is December.
81
+ def get_monthly(user)
82
+ weeks = get_calendar(user)
83
+ ret = {
84
+ '01' => 0,
85
+ '02' => 0,
86
+ '03' => 0,
87
+ '04' => 0,
88
+ '05' => 0,
89
+ '06' => 0,
90
+ '07' => 0,
91
+ '08' => 0,
92
+ '09' => 0,
93
+ '10' => 0,
94
+ '11' => 0,
95
+ '12' => 0
96
+ }
97
+ weeks[1..-1].each do |k|
98
+ date = k.to_s.scan(/data-date=\".*-(.*?)-/)
99
+ capture = k.to_s.scan(/data-count=\"(.*?)\"/)
100
+ capture[1..-1].each do |i|
101
+ ret[date[0][0]] += i[0].to_i
102
+ end
103
+ end
104
+ ret
105
+ end
106
+
107
+ # Gets the average weekly number of contributions by the user.
108
+ # @param user [String] See #get_total_year
109
+ # @return [Int] The average number of contributions.
110
+ def get_average_week(user)
111
+ weekly = get_weekly(user)
112
+ get_average(weekly)
113
+ end
114
+
115
+ # Gets the average daily number of contributions by the user.
116
+ # @param user [String] See #get_total_year
117
+ # @return [Int] See #get_average_week
118
+ def get_average_day(user)
119
+ daily = get_daily(user)
120
+ get_average(daily)
121
+ end
122
+
123
+ # Gets the average monthly number of contributions by the user.
124
+ # @param user [String] See #get_total_year
125
+ # @return [Int] See #get_average_week
126
+ def get_average_month(user)
127
+ monthly = get_monthly(user)
128
+ get_average(monthly)
129
+ end
130
+
131
+ private
132
+
133
+ # Gets the parsed HTML source for the user profile.
134
+ # @param user [String] See #get_total_year
135
+ # @return [Nokogiri::HTML::Document] The parsed HTML for the user page
136
+ # @raise [UserNotFoundException] If the user is not found.
137
+ def get_page_source(user)
138
+ begin
139
+ Nokogiri::HTML(open("https://github.com/#{user}"), &:noblanks)
140
+ rescue OpenURI::HTTPError
141
+ raise GitHub::Exceptions::UserNotFoundException.new(user)
142
+ end
143
+ end
144
+
145
+ # Gets the parsed calendar HTML source for the user profile.
146
+ # @param user [String] See #get_total_year
147
+ # @return [Nokogiri::XML::NodeSet] The NodeSet for all the g's in the
148
+ # calendar. Consider this as an array of all the weeks. In iteration,
149
+ # you will probably want to skip the first, as it is the total yearly.
150
+ # @return [Nil] See #get_total_year
151
+ def get_calendar(user)
152
+ source = get_page_source(user)
153
+ source.css('svg.js-calendar-graph-svg g')
154
+ end
155
+
156
+ # Gets an average for all the integer values in a hash.
157
+ # @param [Hash] The hash to get the average for.
158
+ # @return [Int] The average of the values.
159
+ def get_average(hash)
160
+ hash_max = hash.length
161
+ hash_total = 0
162
+ hash.each { |_, v| hash_total += v }
163
+ hash_total / hash_max
164
+ end
165
+ end
166
+ end
@@ -0,0 +1,10 @@
1
+ module GitHub
2
+ module Exceptions
3
+ class UserNotFoundException < StandardError
4
+ attr_reader :message
5
+ def initialize(user)
6
+ @message = "The user, #{user}, could not be found."
7
+ end
8
+ end
9
+ end
10
+ end
metadata ADDED
@@ -0,0 +1,75 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: github-calendar
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Eli Foster
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-11-18 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: nokogiri
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 1.6.6.2
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 1.6.6.2
27
+ - !ruby/object:Gem::Dependency
28
+ name: string-utility
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 2.5.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 2.5.0
41
+ description: A library that allows for quick HTML parsing of GitHub user profile contribution
42
+ calendars. This project is part of the GitHub User Language Statistics project.
43
+ email: elifosterwy@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - CHANGELOG.md
49
+ - lib/github/calendar.rb
50
+ - lib/github/exceptions.rb
51
+ homepage: https://github.com/ghuls-apps/github-calendar-api
52
+ licenses: []
53
+ metadata: {}
54
+ post_install_message:
55
+ rdoc_options: []
56
+ require_paths:
57
+ - lib
58
+ required_ruby_version: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: 2.2.3
63
+ required_rubygems_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ requirements: []
69
+ rubyforge_project:
70
+ rubygems_version: 2.4.5.1
71
+ signing_key:
72
+ specification_version: 4
73
+ summary: Getting GitHub user profile calendar data through HTML parsing.
74
+ test_files: []
75
+ has_rdoc: