Skip to content

Commit dc5d7c6

Browse files
committed
GitHub etag still counts against api quota when not authenticated, rate limit checking for updates client side
1 parent cda81ad commit dc5d7c6

File tree

1 file changed

+31
-36
lines changed

1 file changed

+31
-36
lines changed

scripts/install_cli.py

Lines changed: 31 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import ssl
1111
import subprocess
1212
import sys
13+
import time
1314
import traceback
1415
from subprocess import PIPE
1516
from zipfile import ZipFile
@@ -36,7 +37,7 @@ def getOsName():
3637

3738

3839
GITHUB_RELEASES_STABLE_URL = 'https://api.github.com/repos/wakatime/wakatime-cli/releases/latest'
39-
GITHUB_DOWNLOAD_PREFIX = 'https://github.com/wakatime/wakatime-cli/releases/download'
40+
GITHUB_DOWNLOAD_URL = 'https://github.com/wakatime/wakatime-cli/releases/latest/download'
4041
PLUGIN = 'vim'
4142

4243
is_py2 = (sys.version_info[0] == 2)
@@ -298,8 +299,27 @@ def isCliLatest():
298299
return True
299300

300301
log('Current wakatime-cli version is %s' % localVer)
301-
log('Checking for updates to wakatime-cli...')
302302

303+
configs, last_accessed = None, None
304+
try:
305+
configs = parseConfigFile(getConfigFile(True))
306+
if configs and configs.has_option('internal', 'cli_version_last_accessed'):
307+
last_accessed = configs.get('internal', 'cli_version_last_accessed')
308+
except:
309+
log(traceback.format_exc())
310+
311+
if last_accessed:
312+
try:
313+
last_accessed = int(float(last_accessed))
314+
except:
315+
last_accessed = None
316+
now = round(time.time())
317+
four_hours = 4 * 3600
318+
if last_accessed and last_accessed + four_hours > now:
319+
log('Skip checking for wakatime-cli updates because recently checked {0} seconds ago.'.format(now - last_accessed))
320+
return True
321+
322+
log('Checking for updates to wakatime-cli...')
303323
remoteVer = getLatestCliVersion()
304324

305325
if not remoteVer:
@@ -313,50 +333,31 @@ def isCliLatest():
313333
return False
314334

315335

316-
LATEST_CLI_VERSION = None
317-
318-
319336
def getLatestCliVersion():
320-
global LATEST_CLI_VERSION
321-
322-
if LATEST_CLI_VERSION:
323-
return LATEST_CLI_VERSION
324-
325-
configs, last_modified, last_version = None, None, None
326-
try:
327-
configs = parseConfigFile(getConfigFile(True))
328-
if configs:
329-
if configs.has_option('internal', 'cli_version'):
330-
last_version = configs.get('internal', 'cli_version')
331-
if last_version and configs.has_option('internal', 'cli_version_last_modified'):
332-
last_modified = configs.get('internal', 'cli_version_last_modified')
333-
except:
334-
log(traceback.format_exc())
335-
336337
try:
337-
headers, contents, code = request(GITHUB_RELEASES_STABLE_URL, last_modified=last_modified)
338+
headers, contents, code = request(GITHUB_RELEASES_STABLE_URL)
338339

339340
log('GitHub API Response {0}'.format(code))
340341

341-
if code == 304:
342-
LATEST_CLI_VERSION = last_version
343-
return last_version
344-
345342
data = json.loads(contents.decode('utf-8'))
346343

347344
ver = data['tag_name']
348345
log('Latest wakatime-cli version from GitHub: {0}'.format(ver))
349346

347+
try:
348+
configs = parseConfigFile(getConfigFile(True))
349+
except:
350+
log(traceback.format_exc())
350351
if configs:
351352
last_modified = headers.get('Last-Modified')
352353
if not configs.has_section('internal'):
353354
configs.add_section('internal')
354355
configs.set('internal', 'cli_version', str(u(ver)))
355356
configs.set('internal', 'cli_version_last_modified', str(u(last_modified)))
357+
configs.set('internal', 'cli_version_last_accessed', str(round(time.time())))
356358
with open(getConfigFile(True), 'w', encoding='utf-8') as fh:
357359
configs.write(fh)
358360

359-
LATEST_CLI_VERSION = ver
360361
return ver
361362
except:
362363
log(traceback.format_exc())
@@ -404,11 +405,8 @@ def cliDownloadUrl():
404405
if check not in validCombinations:
405406
reportMissingPlatformSupport(osname, arch)
406407

407-
version = getLatestCliVersion()
408-
409-
return '{prefix}/{version}/wakatime-cli-{osname}-{arch}.zip'.format(
410-
prefix=GITHUB_DOWNLOAD_PREFIX,
411-
version=version,
408+
return '{prefix}/wakatime-cli-{osname}-{arch}.zip'.format(
409+
prefix=GITHUB_DOWNLOAD_URL,
412410
osname=osname,
413411
arch=arch,
414412
)
@@ -423,17 +421,14 @@ def reportMissingPlatformSupport(osname, arch):
423421
request(url)
424422

425423

426-
def request(url, last_modified=None):
424+
def request(url):
427425
req = Request(url)
428426
req.add_header('User-Agent', 'github.com/wakatime/{plugin}-wakatime'.format(plugin=PLUGIN))
429427

430428
proxy = CONFIGS.get('settings', 'proxy') if CONFIGS.has_option('settings', 'proxy') else None
431429
if proxy:
432430
req.set_proxy(proxy, 'https')
433431

434-
if last_modified:
435-
req.add_header('If-Modified-Since', last_modified)
436-
437432
try:
438433
resp = urlopen(req)
439434
try:

0 commit comments

Comments
 (0)