forked from platformio/platformio-core
Improve PIO Home Misc RPC
This commit is contained in:
@ -22,8 +22,8 @@ from platformio.commands.home.rpc.handlers.os import OSRPC
|
|||||||
|
|
||||||
|
|
||||||
class MiscRPC(object):
|
class MiscRPC(object):
|
||||||
def load_latest_tweets(self, username):
|
def load_latest_tweets(self, data_url):
|
||||||
cache_key = "piohome_latest_tweets_" + str(username)
|
cache_key = data_url
|
||||||
cache_valid = "7d"
|
cache_valid = "7d"
|
||||||
with app.ContentCache() as cc:
|
with app.ContentCache() as cc:
|
||||||
cache_data = cc.get(cache_key)
|
cache_data = cc.get(cache_key)
|
||||||
@ -32,20 +32,17 @@ class MiscRPC(object):
|
|||||||
# automatically update cache in background every 12 hours
|
# automatically update cache in background every 12 hours
|
||||||
if cache_data["time"] < (time.time() - (3600 * 12)):
|
if cache_data["time"] < (time.time() - (3600 * 12)):
|
||||||
reactor.callLater(
|
reactor.callLater(
|
||||||
5, self._preload_latest_tweets, username, cache_key, cache_valid
|
5, self._preload_latest_tweets, data_url, cache_key, cache_valid
|
||||||
)
|
)
|
||||||
return cache_data["result"]
|
return cache_data["result"]
|
||||||
|
|
||||||
result = self._preload_latest_tweets(username, cache_key, cache_valid)
|
result = self._preload_latest_tweets(data_url, cache_key, cache_valid)
|
||||||
return result
|
return result
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
@defer.inlineCallbacks
|
@defer.inlineCallbacks
|
||||||
def _preload_latest_tweets(username, cache_key, cache_valid):
|
def _preload_latest_tweets(data_url, cache_key, cache_valid):
|
||||||
result = yield OSRPC.fetch_content(
|
result = json.loads((yield OSRPC.fetch_content(data_url)))
|
||||||
"https://api.platformio.org/tweets/" + username
|
|
||||||
)
|
|
||||||
result = json.loads(result)
|
|
||||||
with app.ContentCache() as cc:
|
with app.ContentCache() as cc:
|
||||||
cc.set(
|
cc.set(
|
||||||
cache_key,
|
cache_key,
|
||||||
|
Reference in New Issue
Block a user