Last active
December 22, 2015 01:09
-
-
Save finnp/6394664 to your computer and use it in GitHub Desktop.
Python command line script for codeivate.com
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
import urllib | |
import sys | |
import datetime | |
import time | |
def color_text(text, color = "green"): | |
colors = { | |
"green": '\033[92m', | |
"blue": '\033[94m', | |
"yellow": '\033[93m', | |
"red": '\033[91m' | |
} | |
return colors[color] + text + '\033[0m' | |
def progress_bar(ratio): | |
maxi = 20 | |
left = int(maxi * ratio) | |
right = maxi - left | |
percentage = str(int(ratio * 100)) + "%" | |
return "[" + left*"#" + right*" " + "]" + percentage | |
def show_progress(lang = False, focus = False): | |
if lang: | |
raw_level = float ( profile['languages'][lang]['level']) | |
elif focus: | |
raw_level = float( profile['focus_level'] ) | |
else: | |
raw_level = float( profile['level'] ) | |
kind = lang if lang else "Focus" if focus else "General" | |
level = int( raw_level ) | |
level_progress = raw_level - level | |
print "Level " + color_text(str(level)) + "\tProgress " + color_text(progress_bar(level_progress)) + " - " + kind | |
def get_argument(endpoint, default = False): | |
if (endpoint in sys.argv): | |
parameter = sys.argv.index(endpoint) | |
if(len(sys.argv) > (parameter + 1)): | |
return sys.argv[parameter + 1] | |
return default | |
def restart_line(): | |
sys.stdout.write('\r') | |
sys.stdout.flush() | |
username = get_argument("-user", "finn") | |
while True: | |
# Load the json file | |
jsonr = urllib.urlopen("http://www.codeivate.com/users/{0}.json".format(username)); | |
profile = json.loads(jsonr.read()); | |
if "error" in profile: | |
for key, value in profile["error"].iteritems(): | |
print color_text("Error " + key + ": ","red") + value + "\a" | |
else: | |
if "-level" in sys.argv: | |
show_progress() | |
if "-focus" in sys.argv: | |
show_progress(False, True) | |
if "-streak" in sys.argv: | |
print "You are streaking right now" if profile['streaking_now'] else "Not on streak, yet. Continue coding! ", | |
print "Your max streak was " + str(profile['max_streak']) | |
if "-time" in sys.argv: | |
print "You are programming already " + str(datetime.timedelta(seconds=profile['time_spent'])) | |
langs = get_argument("-lang", "empty").split(',') | |
if langs[0] == "all": | |
for lang in profile['languages']: | |
show_progress(lang) | |
elif langs[0] == "now": | |
show_progress(profile['current_language']) | |
elif langs[0] != "empty": | |
for lang in langs: | |
show_progress(lang) | |
if("-refresh" in sys.argv): | |
time.sleep(int(get_argument("-refresh", 60))) | |
else: | |
break | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment