Created
December 24, 2018 01:39
-
-
Save dtak1114/68c4cc0824228922f7769b6389f8f01c to your computer and use it in GitHub Desktop.
Amazonギフト券売買価格監視用puthonista widget
This file contains hidden or 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
#!python3 | |
import appex, ui | |
import webbrowser | |
import requests | |
from datetime import datetime as dt | |
import time | |
NOTIF_RATE = 90.5 | |
ON_CLICK_URI = 'safari-https://amaten.com/exhibitions/amazon' | |
def get_stats(): | |
headers = {'X-Requested-With': 'XMLHttpRequest'} | |
res = requests.get( | |
'https://amaten.com/api/gifts?order=&type=amazon&limit=20&last_id=1939408', | |
headers=headers) | |
result = res.json() | |
lowest = sorted(result['gifts'], key=lambda e: e['rate'])[0] | |
rate = float(lowest.get('rate')) | |
price = int(lowest.get('price')) | |
cnt = result['all_gift_count'] | |
return (rate, price, cnt) | |
v = ui.View(frame=(0, 0, 320, 100), name='amatenWatcher') | |
label = ui.Label(frame=(0, 10, 320, 50), flex='wh',font=('<System>', 18), alignment=ui.ALIGN_CENTER) | |
v.add_subview(label) | |
def button_tapped(sender): | |
webbrowser.open(ON_CLICK_URI) | |
button = ui.Button(title='open in safari', font=('<System>', 16), flex='rwh', action=button_tapped,alignment=ui.ALIGN_CENTER) | |
button.frame = (0, 50, 320, 50) | |
v.add_subview(button) | |
def fetch(l): | |
ts = dt.now().strftime('(%H:%M%:%S)') | |
rate, price, cnt = get_stats() | |
l.text = ts + ': ' + str(rate) + '%' + ( | |
': (%dyen)' % price) + '(all: %d)' % cnt | |
if rate <= NOTIF_RATE: | |
l.text_color = 'red' | |
def main(): | |
appex.set_widget_view(v) | |
fetch(label) | |
while appex.is_running_extension(): | |
time.sleep(4) | |
fetch(label) | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment