Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

接口错误 #1

Open
nerozhang opened this issue Apr 12, 2018 · 1 comment
Open

接口错误 #1

nerozhang opened this issue Apr 12, 2018 · 1 comment

Comments

@nerozhang
Copy link

数据接口被拒绝了,本地测试获取不到数据的

@jarrekk
Copy link
Owner

jarrekk commented Apr 13, 2018

后端API没有开源,贴上Python flask关于hack news API的proxy接口:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# author: Kun Jia
# date: 18/6/17
# email: [email protected]
import html
import requests
from flask import jsonify
from . import api_1_0
from flask import current_app
from pymongo import MongoClient
from app.tasks import test_add


@api_1_0.route('/celery')
def celery_test():
    c = test_add.delay(1, 2)
    return 'ok'


@api_1_0.route('/hn/cache/<stype>')
def get_cache(stype):
    app = current_app._get_current_object()
    client = MongoClient(app.config['MONGODB_SETTINGS']['host'], app.config['MONGODB_SETTINGS']['port'])
    db = client.hacker_news
    data = db.cache.find_one({'stype': stype})
    client.close()
    return jsonify(status='success', data=data)


@api_1_0.route('/hn/list/<items>', methods=['GET'])
def get_list(items):
    try:
        items = eval(items)
    except:
        return jsonify(status='error', data={'message': 'items error'}), 400
    # items = items.split(',')
    print(items)
    data = []
    for item in items:
        try:
            r = requests.get('https://hacker-news.firebaseio.com/v0/item/' + str(item) + '.json')
        except Exception as e:
            print(e)
            return jsonify(status='error', data={'message': 'request error'}), 400
        else:
            result = r.json()
            if result.get('text', None):
                result['text'] = html.unescape(result['text'])
            data.append(result)
    return jsonify(status='success', data=data)


@api_1_0.route('/hn/<sub_url>', methods=['GET'])
def hacker_news(sub_url):
    sub_url = '/'.join(sub_url.split('.')) + '.json'
    try:
        r = requests.get('https://hacker-news.firebaseio.com/' + sub_url)
    except:
        return jsonify(status='error', data={'message': 'request error'}), 400
    else:
        if 'item' in sub_url:
            if r.json().get('text', None):
                data = r.json()
                data['text'] = html.unescape(data['text'])
                return jsonify(status='success', data=data)
        return jsonify(status='success', data=r.json())

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants