import os

if not os.environ['FLASK_ENV'] == 'production':
    from dotenv import load_dotenv, find_dotenv
    load_dotenv(find_dotenv(), override=True)

from flask import Flask, jsonify
from flask_cors import CORS

######################### FLASK APP
app = Flask(__name__)
CORS(app)

print("Value of WEB_CONCURRENCY is %s" % os.environ['WEB_CONCURRENCY'])
if int(os.environ['WEB_CONCURRENCY']) > 1:
    # sleep for a random interval to avoid all workers hitting
    # the database multiple times on bootup
    from random import randint
    from time import sleep
    interval = randint(4,20)
    print("Sleep for %d seconds to avoid too many concurrent db hits" % interval)
    sleep(interval)
import db


######################### ACTIONS
@app.route("/pbc")
def index():
    """
    Return organizational entities like Landesliste Hessen and the hessian election districts
    as well as the candidates in them
    """
    groupings = []
    candidates_by_district = db.get_candidates_by_district()
    for group in candidates_by_district:
        print(group)
        groupings.append(group)
    return jsonify({'candidates_by_district': groupings})


def get_full_name(name):
    if not name:
        return None
    full_name = ''
    if name['titles']:
        full_name = full_name + name['titles']
    if name['forename']:
        full_name = full_name + ' ' + name['forename']
    if name['surname']:
        full_name = full_name + ' ' + name['surname']
    if name['affix']:
        full_name = full_name + ' ' + name['affix']
    return full_name


@app.route("/pbc/user/<slug>")
def candidate_info(slug=None):
    """
    """
    if slug is None:
        return "Candidate not provided"

    candidate = db.get_candidate(slug)
    if candidate is None:
        return "Candidate not found"

    full_name = get_full_name(candidate['name'])

    twitter_user = db.getUser(candidate['twitter_handle'])
    if twitter_user is None:
       return "Twitter user for candidate lost in the dark forest - make a donation to us to find this user."

    followers = {"numFollowers": twitter_user["twitter"]["followers_count"]}
    follower_stats = db.getFollowerStats(candidate['twitter_handle'])
    followers.update(follower_stats)

    json_output = {
        "content": "MEMBER", 
        "member": {
            "name": full_name,
            "party": candidate["election"]["party"],
            "twitter_handle": candidate['twitter_handle'],
            "facts" : candidate["facts"],
            "links" : candidate["links"],
            "photos" : candidate["photos"]
        },
        "wordCluster": twitter_user.get("word_frequencies") or {'topics': []},
        "followers": followers,
        "retweets": {
              "numRetweets": 12,
              "numHumans": 11,
              "numBots": 1
        },
        "retweeters": {
              "numRetweeters": 22,
              "numHumans": 9,
              "numBots": 13
        },
        "election": candidate['election'],
        "botness": twitter_user["botness"] if "botness" in twitter_user else {}
    }

    return jsonify(json_output)


if __name__ == "__main__":
    port = os.environ.get('PORT') or 6755
    app.run(host="0.0.0.0", port=port)

@@ -2575,32 +2575,137 @@
 os%22%5D%0A        %7D,%0A
+        %22twitter%22: %7B%0A            %22profile_url%22: twitter_user%5B%22twitter%22%5D%5B%22profile_image_url%22%5D,%0A        %7D,%0A
         %22wordClu

Add twitter profile pic to politician json