mirror of
https://github.com/aljazceru/plugins.git
synced 2026-01-05 22:34:20 +01:00
currencyrate: feedback from Christian and m-schmook.
1. Use requests.packages.urllib3.util.retry for retries. 2. rename get_currencyrate parameters to match Source names. 3. Rename "currencyrate" to "currencyrates". 4. Remove unused packaging package from requirements.txt Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
committed by
Christian Decker
parent
f16b78996f
commit
2bee4a0fc2
@@ -3,9 +3,10 @@ from pyln.client import Plugin
|
|||||||
from collections import namedtuple
|
from collections import namedtuple
|
||||||
from pyln.client import Millisatoshi
|
from pyln.client import Millisatoshi
|
||||||
from cachetools import cached, TTLCache
|
from cachetools import cached, TTLCache
|
||||||
|
from requests.adapters import HTTPAdapter
|
||||||
|
from requests.packages.urllib3.util.retry import Retry
|
||||||
import requests
|
import requests
|
||||||
import statistics
|
import statistics
|
||||||
import time
|
|
||||||
|
|
||||||
plugin = Plugin()
|
plugin = Plugin()
|
||||||
|
|
||||||
@@ -41,24 +42,40 @@ sources = [
|
|||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
def get_currencyrate(plugin, currency, req_template, response_members):
|
# Stolen from https://www.peterbe.com/plog/best-practice-with-retries-with-requests
|
||||||
|
def requests_retry_session(
|
||||||
|
retries=3,
|
||||||
|
backoff_factor=0.3,
|
||||||
|
status_forcelist=(500, 502, 504),
|
||||||
|
session=None,
|
||||||
|
):
|
||||||
|
session = session or requests.Session()
|
||||||
|
retry = Retry(
|
||||||
|
total=retries,
|
||||||
|
read=retries,
|
||||||
|
connect=retries,
|
||||||
|
backoff_factor=backoff_factor,
|
||||||
|
status_forcelist=status_forcelist,
|
||||||
|
)
|
||||||
|
adapter = HTTPAdapter(max_retries=retry)
|
||||||
|
session.mount('http://', adapter)
|
||||||
|
session.mount('https://', adapter)
|
||||||
|
return session
|
||||||
|
|
||||||
|
|
||||||
|
def get_currencyrate(plugin, currency, urlformat, replymembers):
|
||||||
# NOTE: Bitstamp has a DNS/Proxy issues that can return 404
|
# NOTE: Bitstamp has a DNS/Proxy issues that can return 404
|
||||||
# Workaround: retry up to 5 times with a delay
|
# Workaround: retry up to 5 times with a delay
|
||||||
currency_lc = currency.lower()
|
currency_lc = currency.lower()
|
||||||
url = req_template.format(currency_lc=currency_lc, currency=currency)
|
url = urlformat.format(currency_lc=currency_lc, currency=currency)
|
||||||
for _ in range(5):
|
r = requests_retry_session(retries=5, status_forcelist=(404)).get(url, proxies=plugin.proxies)
|
||||||
r = requests.get(url, proxies=plugin.proxies)
|
|
||||||
if r.status_code != 200:
|
|
||||||
time.sleep(1)
|
|
||||||
continue
|
|
||||||
break
|
|
||||||
|
|
||||||
if r.status_code != 200:
|
if r.status_code != 200:
|
||||||
plugin.log(level='info', message='{}: bad response {}'.format(url, r.status_code))
|
plugin.log(level='info', message='{}: bad response {}'.format(url, r.status_code))
|
||||||
return None
|
return None
|
||||||
|
|
||||||
json = r.json()
|
json = r.json()
|
||||||
for m in response_members:
|
for m in replymembers:
|
||||||
expanded = m.format(currency_lc=currency_lc, currency=currency)
|
expanded = m.format(currency_lc=currency_lc, currency=currency)
|
||||||
if expanded not in json:
|
if expanded not in json:
|
||||||
plugin.log(level='debug', message='{}: {} not in {}'.format(url, expanded, json))
|
plugin.log(level='debug', message='{}: {} not in {}'.format(url, expanded, json))
|
||||||
@@ -85,7 +102,7 @@ def set_proxies(plugin):
|
|||||||
plugin.proxies = None
|
plugin.proxies = None
|
||||||
|
|
||||||
|
|
||||||
# Don't grab these more than once per hour.
|
# Don't grab these more than once per hour.
|
||||||
@cached(cache=TTLCache(maxsize=1024, ttl=3600))
|
@cached(cache=TTLCache(maxsize=1024, ttl=3600))
|
||||||
def get_rates(plugin, currency):
|
def get_rates(plugin, currency):
|
||||||
rates = {}
|
rates = {}
|
||||||
@@ -97,8 +114,8 @@ def get_rates(plugin, currency):
|
|||||||
return rates
|
return rates
|
||||||
|
|
||||||
|
|
||||||
@plugin.method("currencyrate")
|
@plugin.method("currencyrates")
|
||||||
def currencyrate(plugin, currency):
|
def currencyrates(plugin, currency):
|
||||||
"""Gets currency from given APIs."""
|
"""Gets currency from given APIs."""
|
||||||
|
|
||||||
return get_rates(plugin, currency.upper())
|
return get_rates(plugin, currency.upper())
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
pyln-client>=0.7.3
|
pyln-client>=0.7.3
|
||||||
requests>=2.10.0
|
requests>=2.10.0
|
||||||
requests[socks]>=2.10.0
|
requests[socks]>=2.10.0
|
||||||
packaging>=14.1
|
|
||||||
cachetools
|
cachetools
|
||||||
|
|||||||
Reference in New Issue
Block a user