use wakatime-cli to fetch today coding time

This commit is contained in:
Alan Hamlett 2019-05-10 09:43:30 -07:00
parent db00024455
commit f7b3924a30

View file

@ -26,7 +26,6 @@ import threading
import traceback import traceback
import urllib import urllib
import webbrowser import webbrowser
from contextlib import closing
from subprocess import STDOUT, PIPE from subprocess import STDOUT, PIPE
from zipfile import ZipFile from zipfile import ZipFile
try: try:
@ -41,11 +40,6 @@ try:
except ImportError: except ImportError:
import queue # py3 import queue # py3
try:
import urllib2
except ImportError:
import urllib.request as urllib2
is_py2 = (sys.version_info[0] == 2) is_py2 = (sys.version_info[0] == 2)
is_py3 = (sys.version_info[0] == 3) is_py3 = (sys.version_info[0] == 3)
@ -134,7 +128,6 @@ PYTHON_LOCATION = None
HEARTBEATS = queue.Queue() HEARTBEATS = queue.Queue()
HEARTBEAT_FREQUENCY = 2 # minutes between logging heartbeat when editing same file HEARTBEAT_FREQUENCY = 2 # minutes between logging heartbeat when editing same file
SEND_BUFFER_SECONDS = 30 # seconds between sending buffered heartbeats to API SEND_BUFFER_SECONDS = 30 # seconds between sending buffered heartbeats to API
API_SUMMARIES_URL = 'https://api.wakatime.com/api/v1/users/current/summaries?start=today&end=today&api_key={}'
# Log Levels # Log Levels
@ -228,19 +221,49 @@ class FetchStatusBarCodingTime(threading.Thread):
def __init__(self): def __init__(self):
threading.Thread.__init__(self) threading.Thread.__init__(self)
self.debug = SETTINGS.get('debug')
self.api_key = SETTINGS.get('api_key', '') self.api_key = SETTINGS.get('api_key', '')
self.proxy = SETTINGS.get('proxy')
self.python_binary = SETTINGS.get('python_binary')
def run(self): def run(self):
if not self.api_key: if not self.api_key:
log(DEBUG, 'Missing WakaTime API key.') log(DEBUG, 'Missing WakaTime API key.')
return return
url = API_SUMMARIES_URL.format(self.api_key) python = self.python_binary
if not python or not python.strip():
python = python_binary()
if not python:
log(DEBUG, 'Missing Python.')
return
ua = 'sublime/%d sublime-wakatime/%s' % (ST_VERSION, __version__)
cmd = [
python,
API_CLIENT,
'--today',
'--key', str(bytes.decode(self.api_key.encode('utf8'))),
'--plugin', ua,
]
if self.debug:
cmd.append('--verbose')
if self.proxy:
cmd.extend(['--proxy', self.proxy])
log(DEBUG, ' '.join(obfuscate_apikey(cmd)))
try: try:
with closing(urllib2.urlopen(url)) as response: process = Popen(cmd, stdout=PIPE, stderr=STDOUT)
grand_total_text = json.loads(u(response.read()))['data'][0]['grand_total']['text'] output, err = process.communicate()
msg = 'Today: {0}'.format(grand_total_text) output = u(output)
retcode = process.poll()
if not retcode and output:
msg = 'Today: {output}'.format(output=output)
update_status_bar(msg=msg) update_status_bar(msg=msg)
else:
log(DEBUG, 'wakatime-core today exited with status: {0}'.format(retcode))
if output:
log(DEBUG, u('wakatime-core today output: {0}').format(output))
except: except:
pass pass