Skip to content

Instantly share code, notes, and snippets.

@Xodarap
Created June 30, 2014 14:33
Show Gist options
  • Save Xodarap/9b1771046123c3359564 to your computer and use it in GitHub Desktop.
Save Xodarap/9b1771046123c3359564 to your computer and use it in GitHub Desktop.
Scripts to synchronize Todoist and habitRPG
# Simple script to synchronize Todoist and habitRPG
# only marks complete items in Todoist as complete in habitRPG,
# doesn't synchronize non-complete to do items
#
# Note: this script uses a temporary file to store the most recent sequence number from
# Todoist. Therefore, it must be stored in a spot where it has write access to a new file,
# and if you move it you'll either need to comment out the body of the loop below or else
# move the file.
#
# If this is the first time you're running the script, you might want to comment out the
# body of the loop below so that it doesn't synchronize everything in your history.
#
# (Commenting out the loop body and running the script will just update the sequence number
# and not actually synchronize anything)
require 'json'
require 'net/http'
require 'net/https'
# Put your keys here
habit_user =
habit_token =
todoist_token =
hr_hd = {
"Content-Type"=>"application/json",
'x-api-user'=> habit_user,
'x-api-key' => habit_token
}
if !File.file?("todo.txt")
File.open("todo.txt", "w") do |aFile|
aFile.puts("0")
end
end
aFile = File.open("todo.txt", "r")
seq_no = aFile.readline
aFile.close
url = URI.parse('https://api.todoist.com/TodoistSync/v5.3/get')
post_args1 = {'api_token'=>todoist_token, 'seq_no'=>seq_no}
resp, data = Net::HTTP.post_form(url, post_args1)
jd = JSON.parse(resp.body)
jd['Items'].each do |it|
# Checked means that you marked it as complete in Todoist
if it['checked'] == 1
# Adding a task is a two-stage process:
# 1. First, add it as a to do item
# 2. Next, Mark that item as complete
#
# if you just immediately mark it as complete you don't get XP
hr_args = {
'type' => 'todo',
'completed' => false,
'text' => it['content']
}
url = URI.parse('https://habitrpg.com:443/api/v2/user/tasks')
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Post.new(url.request_uri, hr_hd)
request.body = hr_args.to_json
response = http.request(request)
hr_j = JSON.parse(response.body)
hr_id = hr_j['id']
url = URI.parse("https://habitrpg.com:443/api/v2/user/tasks/#{hr_id}/up")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Post.new(url.request_uri, hr_hd)
response = http.request(request)
puts "Added #{it['content']}"
end
end
File.open("todo.txt", "w") do |aFile|
aFile.puts(jd['seq_no'])
end
@nermanthomas
Copy link

Hi!
When I run the script I receive an "Unexpected Identifier" error and points to the first dash in my habit_user ID. Thanks for the help and sorry for the bother

@ooker777
Copy link

Hi, I'm totally noob here. How can I run this script? Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment