Skip to content

Instantly share code, notes, and snippets.

@camillebaldock
Last active August 29, 2015 14:27
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 camillebaldock/a29450249721e1686cb8 to your computer and use it in GitHub Desktop.
Save camillebaldock/a29450249721e1686cb8 to your computer and use it in GitHub Desktop.
Dashing Project Euler

Description

A Dashing widget for displaying the number of unsolved Project Euler problems for an account.

See a live demo here.

Usage

Due to the fact that Project Euler does not, at current time of writing, have a public API, I wrote some code to scrape the data off their webpage. I decided to run the script on a personal machine at home and push the data to my Dashing application.

Setup the following environment variables:

  • PROJECT_EULER_USERNAME
  • PROJECT_EULER_PASSWORD
  • AUTH_TOKEN: the auth token of your Dashing application

Dependencies

mechanize

Add it to your Dashing application's Gemfile:

gem 'mechanize'

and run bundle install.

require 'uri'
require 'net/http'
require 'json'
require 'mechanize'
mechanize = Mechanize.new
page = mechanize.get('https://projecteuler.net/sign_in')
form = page.forms.first
form['username']=ENV['PROJECT_EULER_USERNAME']
form['password']=ENV['PROJECT_EULER_PASSWORD']
button = form.buttons.first
page = form.submit(button)
progress = page.link_with(text: 'Progress')
page = progress.click
solved_string = page.at('#progress_bar_section > h3').text
matches = /Solved (\d+) out of (\d+) problems/.match(solved_string)
euler_left = matches[2].to_i-matches[1].to_i
auth_token=ENV["AUTH_TOKEN"]
json_headers = {"Content-Type" => "application/json",
"Accept" => "application/json"}
params = {'auth_token' => auth_token, 'current' => euler_left}
uri = URI.parse('http://dashboard.camillebaldock.com/widgets/euler')
http = Net::HTTP.new(uri.host, uri.port)
response = http.post(uri.path, params.to_json, json_headers)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment