-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.rb
78 lines (60 loc) · 2.25 KB
/
script.rb
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
76
77
78
# Usage: $ ruby script.rb [repositories|code|issues|users] [query_string]
require 'httparty'
require 'launchy'
require 'socket'
require 'awesome_print'
require_relative 'aux'
params = {}
params[:type] = ARGV[0]
params[:q] = ARGV[1]
if params[:type].nil? || params[:q].nil? || !%w[repositories code issues users].include?(params[:type])
puts "Usage: $ ruby script.rb [repositories|code|issues|users] [query_string] [stars|forks|updated] [asc|desc]"
puts "Try: ruby script.rb help"
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/#{params[:type]}?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)
file = "results-codesearch-[type-#{params[:type]}]-[#{params[:q]}][#{Time.now}].csv"
send("save_csv_head_#{params[:type]}".to_sym, file)
process_data(params[:type], response, token, params, file)
#Pagination
unless response.headers['link'].nil?
links = pagination(response.headers)
while !links['next'].nil? do
response = HTTParty.get(links['next'], :headers => headers)
links = pagination(response.headers)
verify_rate_limit(response.headers)
process_data(params[:type], response, token, params, file)
end
end