Answers for "nomadcoder base_url = "http://hn.algolia.com/api/v1""

0

nomadcoder base_url = "http://hn.algolia.com/api/v1"

from flask import Flask, render_template, request, redirect
app = Flask("SuperScrapper")
import requests

import os
os.system("clear")

base_url = "http://hn.algolia.com/api/v1"

# This URL gets the newest stories.
newurl = f"{base_url}/search_by_date?tags=story"

# This URL gets the most popular stories
popularurl = f"{base_url}/search?tags=story"

DataBagenew = []
innew = []
DataBagePopular = []
inpopular = []
DataBagecom = []


def populardataget():
    popular = requests.get(popularurl)
    populardic = popular.json()
    for i in populardic['hits']:
        if i['num_comments'] != 0 and i['title'] != None:
            if i['objectID'] not in inpopular:
                inpopular.append(i['objectID'])
                DataBagePopular.append(i)
    return DataBagePopular


def newdataget():
    new = requests.get(newurl)
    populardic = new.json()
    for i in populardic['hits']:
        if i['title'] != None:
            if i['objectID'] not in innew:
                innew.append(i['objectID'])
                DataBagenew.append(i)
    return DataBagenew


@app.route("/")
def home():
    order_by = request.args.get('order_by')
    print(order_by)
    if order_by == "new":
        if DataBagenew:
            DataBage = DataBagenew
        else:
            DataBage = newdataget()
        return render_template("mvp.html", data=DataBage, order=order_by)
    elif order_by == "popular":
        if DataBagePopular:
            DataBage = DataBagePopular
        else:
            DataBage = populardataget()
        return render_template("mvp.html", data=DataBage, order=order_by)
    else:
        if DataBagePopular:
            DataBage = DataBagePopular
        else:
            DataBage = populardataget()
        return render_template("mvp.html", data=DataBage, order=order_by)


@app.route("/<number>")
def comment(number):
    com = requests.get(f"{base_url}/items/{number}")
    comdic = com.json()
    return render_template("mvpcom.html", data = comdic)

def startgame():
  app.run(host="0.0.0.0")
Posted by: Guest on September-13-2021

Code answers related to "nomadcoder base_url = "http://hn.algolia.com/api/v1""

Browse Popular Code Answers by Language