repl.it
@andersonbosa/

LynxApp-bot

Python

No description

fork
loading
Files
  • main.py
  • static
  • templates
  • __init__.py
main.py
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
65
66
67
68
69
70
71
72
73
74
75
76
	
import requests
import json
import os
from flask import Flask, redirect, url_for, render_template, session
from collections import namedtuple

app = Flask(__name__, static_folder='./', root_path='/home/runner')

# key example
app.secret_key = b'_5#y2L"F4Q8z\n\xec]/'

###############################################################################
# Actions

def request_data(entrypoint):
    print("# Crawling data from API endpoint:\n# {}".format(entrypoint))
    start_api_url = entrypoint
    res = requests.get(url=start_api_url)
    page_data = res.json()  # Get all json data
    # Another way to access the data
    data_query = json.loads(
        res.text, object_hook=lambda d: namedtuple('X', d.keys())(*d.values()))
    return page_data


###############################################################################
# Routes

@app.route('/')
def index():
    # forbidden access to index products redirecting to first products page
    return redirect('/products/0/')


@app.route('/newsletter/')
def newsletter():
    return render_template('pages/newsletter.html',
    from_server="Seu amigo com certeza vai adorar!")


@app.route('/products/')
def redir():
    # forbidden access to products redirecting to first products page
    return redirect('/products/0/')


@app.route('/products/<string:page>/')
def products(page):
    # API endpoint 
    api_base_url = 'https://frontend-intern-challenge-api.iurykrieger.now.sh/products?page='

    # requested page number
    this_page = int('%s' % page)

    # set url to next page
    next_page = '/products/'+str((this_page + 1))

    # request data in API
    page_data = request_data(
        str(api_base_url + '{}'.format(this_page)))

    return render_template('index.html',
                           next_page=next_page,  # next url page to be the accessed from this page
                           data=page_data,  # products json data
                           items_perpage=len(page_data['products']), # control products per page
                           form_page = "/newsletter" # url to a post formularie
                           )


###############################################################################
# Starting

if __name__ == '__main__':
    app.run(host='0.0.0.0', port='3000')