1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
| import os import json import random from django.http import JsonResponse
from backend import settings from utils.response import CommonResponseMixin, ReturnCode import thirdparty.juhe
constellations = ['白羊座', '金牛座', '双子座', '巨蟹座', '狮子座', '处女座', '天秤座', '天蝎座', '射手座', '摩羯座', '水瓶座', '双鱼座'] popular_stocks = [ { 'code': '000001', 'name': '平安银行', 'market': 'sz' }, { 'code': '000002', 'name': '万科A', 'market': 'sz' }, { 'code': '600036', 'name': '招商银行', 'market': 'sh' }, { 'code': '601398', 'name': '工商银行', 'market': 'sh' } ]
all_jokes = []
def stock(request): data = [] for stock in popular_stocks: result = thirdparty.juhe.stock(stock['market'], stock['code']) data.append(result) response = CommonResponseMixin.wrap_json_response(data=data, code=ReturnCode.SUCCESS) return JsonResponse(data=response, safe=False) pass
def constellation(request): data = [] for c in constellations: result = thirdparty.juhe.constellation(c) data.append(result) response = CommonResponseMixin.wrap_json_response(data=data, code=ReturnCode.SUCCESS) return JsonResponse(data=response, safe=False) pass
def joke(request): global all_jokes if not all_jokes: all_jokes = json.load(open(os.path.join(settings.BASE_DIR, 'jokes.json'), 'r')) limits = 10 sample_jokes = random.sample(all_jokes, limits) response = CommonResponseMixin.wrap_json_response(data=sample_jokes) return JsonResponse(data=response, safe=False)
|