-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgogstash.rb
143 lines (128 loc) · 4.07 KB
/
gogstash.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# GogStash - A gog.com downloader written in Ruby
# Copyright (C) 2017 Lloyd Dilley
# http://www.dilley.me/
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
require 'io/console' # for password input with no echo
VERSION = '0.1a'
LOGIN_PAGE = 'https://#.gog.com/#'
def log_init
begin
directory_name = 'logs'
Dir.mkdir(directory_name) unless File.exists?(directory_name) && File.writable?(directory_name)
rescue => exception
puts "Unable to create #{directory_name} directory: #{exception}"
exit!
end
end
def log_write(severity, text)
# Below actually returns an array, so we check for emptiness and not nil/true/false
Dir.mkdir('logs') if Dir['logs'].empty?
case severity
when 0
level = 'DBUG'
when 1
level = 'AUTH'
when 2
level = 'INFO'
when 3
level = 'WARN'
when 4
level = 'CRIT'
else
level = 'DBUG'
end
log_file = File.open('logs/gogstash.log', 'a')
log_file.puts "#{Time.now.asctime} #{level}: #{text}"
log_file.close
rescue => exception
puts "Unable to write log file: #{exception}"
end
# Send output to both stdout and log
def duplex_write(severity, text)
puts text
log_write(severity, text)
end
# Enforce proper Ruby version
def version_check
if RUBY_VERSION < '1.9'
duplex_write(4, 'GogStash requires Ruby >=1.9!')
exit!
end
end
# Check if Mechanize is available
def dependency_check
begin
gem('mechanize', '>=2.7.5')
require 'mechanize'
rescue Gem::LoadError
duplex_write(4, 'Mechanize gem not found!')
exit!
end
end
def web_login
begin
cookie_file = 'cookie.yml'
mechanize = Mechanize.new
#mechanize.verify_mode = OpenSSL::SSL::VERIFY_NONE # for testing with invalid/self-signed certificates
if File.exists?(cookie_file)
mechanize.cookie_jar.load cookie_file
else
email_address = nil
password = nil
while email_address.nil? || email_address.empty? do
print 'Enter your gog.com e-mail address: '
email_address = gets.chomp.strip
puts 'Invalid e-mail address!' if email_address.nil? || email_address.empty?
end
while password.nil? || password.empty? do
print 'Enter your gog.com password: '
password = STDIN.noecho(&:gets).chomp.strip
puts '' # fixes \r\n problem after using noecho
puts 'Invalid password!' if password.nil? || password.empty?
end
login_page = mechanize.get LOGIN_PAGE
web_form = login_page.forms.first
if web_form.nil?
duplex_write(4, "Unable to log into #{LOGIN_PAGE}: No form?")
exit!
end
web_form.field_with(id: 'login_username').value = email_address
web_form.field_with(id: 'login_password').value = password
web_form.submit
mechanize.cookie_jar.save_as cookie_file
end
# Temporary debug stuff
#results = mechanize.get("https://www.gog.com/account")
results = mechanize.get("https://www.gog.com/account/wishlist")
log_write(1, results.content)
# End temporary debug stuff
rescue => exception
duplex_write(4, "Unable to log into #{LOGIN_PAGE}: #{exception}")
exit!
end
end
# main()
puts "GogStash #{VERSION}"
puts 'Initializing logging...'
log_init
log_write(2, "GogStash #{VERSION}")
duplex_write(2, 'Checking Ruby version...')
version_check
duplex_write(2, 'Checking for dependencies...')
dependency_check
duplex_write(2, "Logging in to #{LOGIN_PAGE}...")
web_login
# ToDo: Allow downloading of films and games after authentication.