-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodesearch.rb
More file actions
77 lines (58 loc) · 1.99 KB
/
codesearch.rb
File metadata and controls
77 lines (58 loc) · 1.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# Usage: $ ruby script.rb [repositories|code|issues|users] [query_string]
require 'httparty'
require 'launchy'
require 'socket'
require 'awesome_print'
require_relative 'Helper'
params = {}
params[:q] = ARGV[0]
if params[:q].nil?
puts "Usage: $ ruby script.rb [query_string]"
exit
end
#START OAuth
client_id = 'd74fcf012a3e382c7fed'
client_secret = '653999ffa39d81e9ae4b0138d4685045a625d967'
#open authorize page
Launchy.open ('https://github.com/login/oauth/authorize?client_id=' + client_id)
input = nil
#start server to get value of code
puts 'Starting up server...'
server = TCPServer.new(2000)
loop do
t = Thread.new(server.accept) do |session|
puts "[log] Connection from #{session.peeraddr[2]} at #{session.peeraddr[3]}"
puts "[log] Got input from client"
input = session.gets
session.puts "Permissions granted!\nClosing connection.\nBye :)"
puts "[log] Closing connection"
session.close
end
t.join()
break if !t.status
end
code = input.split("?code=")[1].split(" ")[0]
#get token
token = HTTParty.post('https://github.com/login/oauth/access_token',
:query => {:client_id => client_id, :client_secret => client_secret, :code => code})
#END OAuth
params[:q] = params[:q].gsub(/\s/, '+')
url = "https://api.github.com/search/code?q=#{params[:q]}"
url += "&" + token
url += "&per_page=100"
headers = { 'Accept' => 'application/vnd.github.preview.text-match+json', 'User-Agent' => 'coopera-codesearch' }
puts "URL: #{url}"
response = HTTParty.get(url, :headers => headers)
csv_file = "#{Time.now.strftime("[%Y-%m-%d %H:%M:%S]")}[#{params[:q]}].csv"
helper = Helper.new csv_file
helper.save_users(response, token)
#Pagination
unless response.headers['link'].nil?
links = helper.pagination(response.headers)
while !links['next'].nil? do
response = HTTParty.get(links['next'], :headers => headers)
links = helper.pagination(response.headers)
helper.verify_rate_limit(response.headers)
helper.save_users(response, token)
end
end