Skip to content

Commit

Permalink
Unify frontend.py and username_api.py
Browse files Browse the repository at this point in the history
This commit merges `frontend.py` and `username_api.py` into
`username_checker.py`
This also adds `/api/` to API call in templates/status.html

Closes
manu-chroma#104
  • Loading branch information
suggoitanoshi committed Oct 30, 2018
1 parent d09333b commit 67d0dab
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 97 deletions.
73 changes: 0 additions & 73 deletions frontend.py

This file was deleted.

2 changes: 1 addition & 1 deletion templates/status.html
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
return await
$.ajax({
dataType: 'json',
url : `{{protocol_backend}}://{{host_backend}}:{{port_backend}}/check/${site}/{{username}}`,
url : `{{protocol_backend}}://{{host_backend}}:{{port_backend}}/api/check/${site}/{{username}}`,
})
.then((result) => {
var res = result;
Expand Down
122 changes: 99 additions & 23 deletions username_api.py → username_checker.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,33 @@
import logging
import os
import sys
import json
import re
import uuid

import requests as r
from dotenv import load_dotenv
from flask import Flask, request, render_template, jsonify
from flask.ext.cors import CORS, cross_origin
import yaml
import requests as r
from requests_toolbelt.utils import dump
from bs4 import BeautifulSoup
from flask import Flask, jsonify
from flask.ext.cors import CORS, cross_origin
from werkzeug.contrib.cache import SimpleCache

app = Flask(__name__)
app = Flask(__name__,
static_url_path='',
static_folder='static')

app.config['SEND_FILE_MAX_AGE_DEFAULT'] = 0
cors = CORS(app)
app.config['CORS_HEADERS'] = 'Content-Type'

load_dotenv('.env')
HOST_BACKEND = os.environ.get('HOST_BACKEND')
PORT_BACKEND = os.environ.get('PORT_BACKEND')
PORT_FRONTEND = os.environ.get('PORT_FRONTEND')
PROTOCOL_BACKEND = os.environ.get('PROTOCOL_BACKEND')

patterns = yaml.load(open('websites.yml'))

cache = SimpleCache()
Expand Down Expand Up @@ -41,7 +58,50 @@ def filter(self, record):

logger.addFilter(DebugFilter())

sites = ' '.join(list(patterns['username_patterns'].keys()))
logos = json.dumps(patterns['logos'])

## frontend
@app.route('/', methods=['GET'])
@cross_origin()
def my_form():
username = request.args.get('username', '')
if username:
return render_template('status.html',
username=username,
sites=sites,
logos=logos,
host_backend=HOST_BACKEND,
port_backend=PORT_BACKEND,
protocol_backend=PROTOCOL_BACKEND)
return render_template('form.html')


@app.route('/', methods=['POST'])
@cross_origin(origin='*')
def my_form_post():
username = request.form['text']
return render_template('status.html',
username=username,
sites=sites,
logos=logos,
host_backend=HOST_BACKEND,
port_backend=PORT_BACKEND,
protocol_backend=PROTOCOL_BACKEND)

## backend API endpoints
@app.route('/api/')
@cross_origin(origin='*')
def hello():
return 'Hello world! The app seems to be working!'


@app.route('/api/check/<website>/<username>', methods=['GET'])
@cross_origin(origin='*')
def check(website, username):
return jsonify(check_username(website, username))

## backend API functions
def get_profile_url(website, username):
return patterns['urls'].get(website, 'https://{w}.com/{u}').format(
w=website,
Expand All @@ -62,6 +122,11 @@ def get_avatar(website, username):

response = session.get(url)
if response.status_code == 404:
log_response(
'Status code of response for {}/{} is 404'.format(website, username),
response,
website,
username)
return None

if data == 'opengraph':
Expand All @@ -71,6 +136,12 @@ def get_avatar(website, username):
if item.has_attr('property') and
item.attrs['property'].lower() == 'og:image']
if not result or not result[0]:
log_response(
'There is no opengraph image found for {}/{}'.format(
website, username),
response,
website,
username)
return None
result = result[0]
elif 'html_selector' in data:
Expand All @@ -87,11 +158,21 @@ def get_avatar(website, username):
r'[\'\"]:(\s)?[\'\"](?P<link>[^\s]+)[\'\"]')
result = re.search(regex, response.text)
if not result:
log_response(
'There is no match found for {}/{}'.format(website, username),
response,
website,
username)
return None
result = result.group('link')
elif response.headers.get('content-type', '').startswith('image/'):
return url
else:
log_response(
'Unexpected result: {}/{}'.format(website, username),
response,
website,
username)
return None

# Fix relative links
Expand All @@ -100,7 +181,6 @@ def get_avatar(website, username):
result = base_url + result
return result


def get_status_code(website, username):
url = get_profile_url(website, username)

Expand All @@ -115,7 +195,6 @@ def get_status_code(website, username):
else:
return session.get(url).status_code


def check_username(website, username):
url = get_profile_url(website, username)

Expand Down Expand Up @@ -182,7 +261,6 @@ def check_username(website, username):
'usable': usable,
}


def check_usable(website):
"""
Check if the website is usable.
Expand Down Expand Up @@ -243,20 +321,18 @@ def check_format(website, username):
else:
return False

# API endpoints


@app.route('/')
@cross_origin(origin='*')
def hello():
return 'Hello world! The app seems to be working!'


@app.route('/check/<website>/<username>', methods=['GET'])
@cross_origin(origin='*')
def check(website, username):
return jsonify(check_username(website, username))


if __name__ == '__main__':
app.run(host='0.0.0.0', port=8521)
handler = logging.FileHandler('static/logs.log')
formatter = logging.Formatter('[%(levelname)s|%(filename)s:%(lineno)s] '
'%(asctime)s %(message)s')
handler.setLevel(logging.INFO)
handler.setFormatter(formatter)
app.logger.addHandler(handler)
if not (HOST_BACKEND and PORT_BACKEND):
app.logger.error('Both HOST_BACKEND and PORT_BACKEND'
' should be set before running')
sys.exit(1)
if not PORT_FRONTEND:
app.logger.error('PORT_FRONTEND is not set')
sys.exit(1)
app.run(host='0.0.0.0', port=int(PORT_FRONTEND))

0 comments on commit 67d0dab

Please sign in to comment.