Skip to content

Commit

Permalink
daemon works with logging
Browse files Browse the repository at this point in the history
  • Loading branch information
hako committed Jan 27, 2017
1 parent 7cd81cc commit 14242a2
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 24 deletions.
13 changes: 8 additions & 5 deletions src/blackboard-dl/client.cr
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ module BlackBoard::Dl
self
end

# Gets a list of colleges on the Blackboard system.
# Gets a list of available colleges on the Blackboard system.
def self.search_colleges(q : String)
client = HTTP::Client.new URI.parse(SEARCH_HOST)
param_data = {
Expand Down Expand Up @@ -79,7 +79,7 @@ module BlackBoard::Dl
colleges
end

# Signs the user into Blackboard.
# Signs the student into Blackboard.
def login
client = HTTP::Client.new URI.parse(@host)
data = {"username" => @username, "password" => @password}
Expand All @@ -92,7 +92,10 @@ module BlackBoard::Dl
# Save relevant cookies...
COOKIES["s_session_id"] = res.cookies["s_session_id"].value
COOKIES["session_id"] = res.cookies["session_id"].value
COOKIES["web_client_cache_guid"] = res.cookies["web_client_cache_guid"].value

if res.cookies.has_key?("web_client_cache_guid")
COOKIES["web_client_cache_guid"] = res.cookies["web_client_cache_guid"].value
end
end
return status
end
Expand Down Expand Up @@ -125,6 +128,8 @@ module BlackBoard::Dl

# Send 'Cookie' header.
HEADERS["Cookie"] = ("web_client_cache_guid=#{COOKIES["web_client_cache_guid"]}; session_id=#{COOKIES["session_id"]}; s_session_id=#{COOKIES["s_session_id"]}")

# Get course data and start downloading attachments.
res = client.post(COURSE + "&course_id=" + course_id, headers: HEADERS)
response = XML.parse(res.body.to_s).first_element_child.as(XML::Node)
status = response["status"]
Expand Down Expand Up @@ -188,7 +193,6 @@ module BlackBoard::Dl
end

# Workshops

begin
parsed_data.children[1].children[7].children.each do |child|
child.children.each do |c|
Expand Down Expand Up @@ -262,7 +266,6 @@ module BlackBoard::Dl
#
# 2nd Location. (Should be the attachment)
location3 = URI.parse(location2.headers["Location"].to_s)

client3 = HTTP::Client.new location3
attachment = client3.get(location3.path.to_s, headers: HEADERS)
client3.close
Expand Down
71 changes: 52 additions & 19 deletions src/cli.cr
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@ module BlackBoard::Dl
)
end

# Selected college.
# Selected college & current dir (for daemon)
@@college = {} of String => String

# Runs the blackboard-dl commandline utility.
def self.run
banner = "Blackboard course downloader " + "v#{VERSION}".colorize.green.to_s + " - [c] 2017 Wesley Hill"
bb_username = Nil
bb_password = Nil
daemon = false
Expand All @@ -37,7 +38,7 @@ module BlackBoard::Dl
parser.on("-h", "--help", "Show this help") { puts parser; exit(0) }
parser.on("-v", "--version", "Show program version") { puts BlackBoard::Dl::VERSION }
end
puts "Blackboard course downloader " + "v#{VERSION}".colorize.green.to_s + " - [c] 2017 Wesley Hill"
puts banner
load_selected_college

# Only search if there is no selected college.
Expand All @@ -54,6 +55,7 @@ module BlackBoard::Dl

puts "\n#{@@college["name"]}".colorize.green.mode(:bold).to_s + " #{secure_type}"
puts "#{@@college["host"]}".colorize.green

# Present a prompt to the student to enter in their course details instead of using the argument parser.
if (bb_username && bb_password) == Nil
puts "-Login--------------------------".colorize.green.mode(:dim)
Expand All @@ -66,31 +68,53 @@ module BlackBoard::Dl
bb_password = password
end

# Download as normal or as a daemon.
if daemon == true
# Daemonize process.
puts "Running in the background on pid: #{Process.pid}".colorize.magenta.mode(:bold).to_s
Daemonize.daemonize
now = Time.now
diff = now.at_end_of_hour - now
until now == now.at_end_of_hour
puts "Checking for an update in #{diff.minutes} minute(s)"
sleep diff.duration
download(@@college["url"], bb_username, bb_password)
now = Time.now
end
download_as_daemon(bb_username, bb_password)
else
download(@@college["url"], bb_username, bb_password)
end
end

# Download course material as a daemon with a username & password.
def self.download_as_daemon(bb_username, bb_password)
# Daemonize process.
puts "Running daemon in the background on pid: #{Process.pid + 2}".colorize.magenta.mode(:bold).to_s
stdout_log = Dir.current + "/bbdl_output.log"
stderr_log = Dir.current + "/bbdl_errors.log"

puts "Daemon log output: #{stdout_log}".colorize.green.to_s
puts "Daemon error output: #{stderr_log}".colorize.red.to_s

Daemonize.daemonize(stdout: stdout_log, stderr: stderr_log, dir: Dir.current)

now = Time.now
puts ""
puts "-Blackboard course downloader daemon ---------------[#{now.to_s("%T")}]"
diff = now.at_end_of_hour - now

# Loop every n minutes.
until now == now.at_end_of_hour
puts "Checking for new course material in #{diff.minutes} minute(s)"
sleep diff.duration
puts "Checking new courses at #{Time.now.to_s("%T")}"
download(@@college["url"], bb_username, bb_password)
now = Time.now
diff = now.at_end_of_hour - now
end
end

# Download courses material with a college url, username & password.
def self.download(bb_url, bb_username, bb_password)
# Make sure we download to this directory.
print "\r[+] Logging in...".colorize.green.mode(:dim)
client = BlackBoard::Dl::Client.new(bb_url.to_s, bb_username.to_s, bb_password.to_s)
# Login.
begin
status = client.login
rescue
rescue error
puts "\r[x] Unable to login try again later...".colorize.red
puts "[!] Reason: #{error}".colorize.red
exit(1)
end
if status != "OK"
Expand All @@ -99,8 +123,7 @@ module BlackBoard::Dl
exit(1)
else
print "\n"
puts ("[+] Successfully logged into Blackboard as" + " %s!".colorize.green.mode(:bold).to_s) %
(bb_username.to_s)
puts ("[+] Successfully logged into Blackboard as" + " %s!".colorize.green.mode(:bold).to_s) % (bb_username.to_s)
end

# Fetch enrolled courses.
Expand All @@ -118,7 +141,12 @@ module BlackBoard::Dl
puts " [+] %s folder exists, continuing..." % name
end
# Start to download the attachment.
client.get_course_data(name, course["id"].to_s)
begin
client.get_course_data(name, course["id"].to_s)
rescue error
puts "[!] Reason: #{error}".colorize.red
exit(1)
end
puts ""
end
puts "[+] Finished downloading courses for #{bb_username}".colorize.green.mode(:bold).to_s
Expand Down Expand Up @@ -173,25 +201,30 @@ module BlackBoard::Dl
end
end

# Search for the students school.
# Search for the students college.
def self.search_college
# This is a prompt for students to search and select their college details. (assuming they use BlackBoard)
# Only exit if they have selected a course.
selected = false
until selected == true
query = Readline.readline("[!] Search for your college/university: ".colorize.green.to_s, add_history = true)
print "\rSearching...".colorize.cyan.mode(:bold)
# Search colleges.
colleges = BlackBoard::Dl::Client.search_colleges query.to_s

# Check empty college.
if colleges.empty?
print "\rNo Results found!".colorize.red
exit(1)
end

# Show college results.
print "\r#{colleges.size} result(s) for \"#{query}\"\n".colorize.green.mode(:bold)
colleges.each_with_index do |college, i|
puts "[#{i}]".colorize.yellow.to_s + " #{college["name"]}"
end
idx = Readline.readline("[?] What " + "number".colorize.yellow.to_s + " is your college/university?: ".colorize.green.to_s, add_history = true)

# Error handling...
begin
@@college = colleges.at(idx.to_s.to_i)
Expand All @@ -204,7 +237,7 @@ module BlackBoard::Dl
# Choice to save their college.
choice = check_y_n(Readline.readline("[?] Would you like to save your selected college?: ".colorize.green.to_s, add_history = true))

# Save only if they user pressed Y.
# Save only if the student pressed Y.
if choice == "Y"
self.save_selected_college
puts "Selected college " + "\"#{@@college["name"]}\"".colorize.green.to_s + " saved in " + "\"selected_college.json\"".colorize.green.to_s + "."
Expand Down

0 comments on commit 14242a2

Please sign in to comment.