Created
February 16, 2018 06:17
-
-
Save tsukumonasu/ee128b73ac1cd17e53825a21149baa7f to your computer and use it in GitHub Desktop.
ponoka
This file contains 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
# coding=utf-8 | |
import json | |
import io | |
import pycurl | |
import random | |
from datetime import datetime | |
import datetime | |
import traceback | |
def lambda_handler(event, context): | |
try: | |
return {'text': getMessage(event['text'])} | |
except: | |
return {'text': traceback.format_exc()} | |
def getMessage(text): | |
if text.find('占い') > -1: return getKehai() | |
coin_name = getName(text) | |
if coin_name == '': return 'まだ知らないコインがあるなんて...' | |
curl = pycurl.Curl() | |
curl.setopt(pycurl.URL,'https://min-api.cryptocompare.com/data/price?fsym=' + coin_name + '&tsyms=JPY' ) | |
b = io.BytesIO() | |
curl.setopt(pycurl.WRITEFUNCTION, b.write) | |
curl.perform() | |
price = b.getvalue().replace('{"JPY":','').replace('}','') | |
return ('今は' + price + '円よ' + "\n" + | |
getOneHourage(coin_name, 1, float(price)) + | |
getOneHourage(coin_name, 2, float(price)) + | |
getOneHourage(coin_name, 3, float(price)) + | |
getOneHourage(coin_name, 4, float(price)) + | |
getOneHourage(coin_name, 5, float(price)) + | |
getOneHourage(coin_name, 6, float(price)) + | |
getOneHourage(coin_name, 7, float(price))) | |
def getKehai(): | |
ram = random.random() | |
if ram > 0.9: | |
return '買いだわ、今すぐ売プッシュよ' | |
if ram > 0.6: | |
return '買いだわ' | |
if ram > 0.3: | |
return '様子見がいいと思うわ' | |
return '売り気配よ' | |
def getOneHourage(coin_name, hours, now_price): | |
target_time = int((datetime.datetime.now() - datetime.timedelta(days=hours)).strftime('%s')) | |
print target_time | |
curl = pycurl.Curl() | |
curl.setopt(pycurl.URL,'https://min-api.cryptocompare.com/data/pricehistorical?fsym=' + coin_name + '&tsyms=JPY&ts=' + str(target_time)) | |
b = io.BytesIO() | |
curl.setopt(pycurl.WRITEFUNCTION, b.write) | |
curl.perform() | |
price = float(b.getvalue().split(':')[2].replace('}}','')) | |
return str(hours) + '日前は、' + str(price) + '円('+ str(hours) + '日前比' + str(int(now_price / price * 100)) +'%)よ\n' | |
def getName(text): | |
if text.find('リップル') >-1: return 'XRP' | |
if text.find('イーサリアム') > -1: return 'ETH' | |
if text.find('ビットコイン') > -1: return 'BTC' | |
if text.find('モナ') > -1:return 'MONA' | |
if text.find('Bitcoin') > -1: return 'BTC' | |
if text.find('Litecoin') > -1: return 'LTC' | |
if text.find('DigitalCash') > -1: return 'DASH' | |
if text.find('Monero') > -1: return 'XMR' | |
return '' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment