Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save phdoerfler/753a351472db2a6ac4e37190270a9036 to your computer and use it in GitHub Desktop.
Save phdoerfler/753a351472db2a6ac4e37190270a9036 to your computer and use it in GitHub Desktop.
File to get the current total number of sessions in Focusmate
require 'json'
require 'pry'
require 'uri'
require 'net/http'
class FocusMateNumSessions
# Step 1. Create a file somewhere in your filesystem.
# Call it fm_token_file.txt. Insert the path to the file into the line below
# Make sure to look at the reference .txt file and just paste
# that over.
# e.g. File path shown below
# @@fm_token_file="/Users/seyonvas/Desktop/Web_scripts/fm_token_file.txt"
@@fm_token_file="insert_file_path"
def initialize
is_valid, token = check_if_existing_token_valid
if is_valid
get_num_sessions(token)
else
new_token = get_token_from_login
get_num_sessions(new_token)
update_token_file(new_token)
end
end
def update_token_file(token)
curr_time=Time.now.to_f.to_i
File.open(@@fm_token_file,'w') do |f|
f.puts(curr_time)
f.puts(token)
end
end
def check_if_existing_token_valid
f=File.open(@@fm_token_file,'r+')
prev_time, token = f.readlines.map(&:chomp)
if prev_time && token
f.close
curr_time = Time.now.to_f.to_i
is_valid = (curr_time - prev_time.to_i) < 3600
end
return is_valid, token
end
def get_token_from_login
# Step 2. Insert your Email/Password beow
email="insert_your_email_here"
pwd="insert_your_password_here"
url = URI("https://www.googleapis.com/identitytoolkit/v3/relyingparty/verifyPassword?key=AIzaSyBd7gm2iFZ8ksJRZfjKGf0j_UcLVFSQbWc")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Post.new(url)
request["authority"] = 'www.googleapis.com'
request["pragma"] = 'no-cache'
request["Cache-Control"] = 'no-cache'
request["x-client-version"] = 'Chrome/JsCore/7.14.1/FirebaseCore-web'
request["user-agent"] = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36'
request["content-type"] = 'application/json'
request["accept"] = '*/*'
request["origin"] = 'https://www.focusmate.com'
request["x-client-data"] = 'CK61yQEIi7bJAQiltskBCMG2yQEIqZ3KAQjQr8oBCLywygEI7bXKAQiOusoBCOfGygEYu7rKARiavsoB'
request["sec-fetch-site"] = 'cross-site'
request["sec-fetch-mode"] = 'cors'
request["sec-fetch-dest"] = 'empty'
request["referer"] = 'https://www.focusmate.com/login'
request["accept-language"] = 'en-US,en;q=0.9'
request["Postman-Token"] = 'efb193c9-70e8-d7d3-2596-c55e7a4bf09a'
request.body = "{\"email\":\"#{email}\",\"password\":\"#{pwd}\",\"returnSecureToken\":true}"
response = http.request(request)
json = JSON.parse(response.read_body)
id_token=json["idToken"]
refresh_token=json["refreshToken"]
return id_token
end
def get_num_sessions(token)
# Step 3: Navigate to your focusmate profile and put the text after "user/" below
# e.g. "seyon-v"
username="insert-username-here"
url = URI("https://focusmate-api.herokuapp.com/v1/user/#{username}")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Get.new(url)
request["Connection"] = 'keep-alive'
request["Pragma"] = 'no-cache'
request["Cache-Control"] = 'no-cache'
request["Accept"] = 'application/json, text/plain, */*'
request["Authorization"] = token
request["User-Agent"] = 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Mobile Safari/537.36'
request["Content-Type"] = 'application/json'
request["Origin"] = 'https://www.focusmate.com'
request["Sec-Fetch-Site"] = 'cross-site'
request["Sec-Fetch-Mode"] = 'cors'
request["Sec-Fetch-Dest"] = 'empty'
request["Accept-Language"] = 'en-US,en;q=0.9'
response = http.request(request)
body = response.read_body
json = JSON.parse(body)
num_sessions = json["compSessions"]
puts "Total Sessions Completed: #{num_sessions}"
end
end
f=FocusMateNumSessions.new()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment