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

some fixes PEP8 #12

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 49 additions & 27 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@
"""

import os
from flask import Flask, render_template, request, url_for, make_response, jsonify
from flask import Flask, render_template, request, url_for, make_response,\
jsonify
import plivo
import config
from utils import get_redis_connection, get_plivo_connection, tinyid

app = Flask(__name__)


@app.route("/")
def index():
"""
Expand All @@ -36,14 +38,15 @@ def conf_music():
"""
Renders the XML to be used for hold music in the conference.
This XML will be executed by Plivo when there is only 1
participant in the conference. The URL to this XML is
participant in the conference. The URL to this XML is
passed to the Conference XML under the parameter 'waitSound'.
"""

plivo_response = plivo.XML.Response()
plivo_response.addSpeak(config.CONFERENCE_WAIT_ANNOUNCEMENT)
plivo_response.addPlay(config.HOLD_MUSIC, loop=1)
response = make_response(render_template('response_template.xml', response=plivo_response))
response = make_response(render_template('response_template.xml',
response=plivo_response))
response.headers['content-type'] = 'text/xml'
return response

Expand All @@ -53,7 +56,7 @@ def conf(conference_name):
"""
Renders the XML to start a conference based on the conference name
it receives. It checks if the conference exists in our memory (to make
sure it hasn't expired).
sure it hasn't expired).

This URL is passed as the Answer URL to Plivo. When the call gets
answered, the call will be put into a conference.
Expand All @@ -65,7 +68,8 @@ def conf(conference_name):

if not room_exists:
plivo_response.addHangup(reason="invalid conference")
response = make_response(render_template('response_template.xml', response=plivo_response))
response = make_response(render_template('response_template.xml',
response=plivo_response))
response.headers['content-type'] = 'text/xml'
return response

Expand All @@ -74,32 +78,33 @@ def conf(conference_name):
wait_sound_url = url_for('conf_music', _external=True)

plivo_response.addConference(conference_name,
enterSound="beep:1",
exitSound="beep:2",
waitSound=wait_sound_url,
)
response = make_response(render_template('response_template.xml', response=plivo_response))
enterSound="beep:1",
exitSound="beep:2",
waitSound=wait_sound_url)
response = make_response(render_template('response_template.xml',
response=plivo_response))
response.headers['content-type'] = 'text/xml'
return response


@app.route('/<conference_name>/', methods=['GET'])
def conference(conference_name):
"""
Returns the HTML page for a particular conference name. The HTML page
Returns the HTML page for a particular conference name. The HTML page
uses the Plivo WebSDK to register to Plivo and make calls.
"""

if conference_exists(conference_name):
redis_conn = get_redis_connection()
endpoint_username = redis_conn.hget(conference_name, 'username')
endpoint_password = redis_conn.hget(conference_name, 'password')
inbound_did = redis_conn.hget(conference_name, 'inbound_did')

conference_url = url_for('conference', _external=True, conference_name=conference_name)
conference_url = url_for('conference', _external=True,
conference_name=conference_name)

data = {
'endpoint_username': endpoint_username,
'endpoint_username': endpoint_username,
'endpoint_password': endpoint_password,
'inbound_did': inbound_did,
'conference_name': conference_name,
Expand Down Expand Up @@ -128,10 +133,13 @@ def conference_api():
app_id = create_plivo_application(conference_name)
endpoint_username = create_plivo_endpoint(conference_name, app_id)
inbound_did = attach_inbound_did(app_id)
link_conference(conference_name, endpoint_username, conference_name, inbound_did)
link_conference(conference_name, endpoint_username, conference_name,
inbound_did)

conference_url = url_for('conference', _external=True, conference_name=conference_name)
return jsonify(conference_url = conference_url, conference_name = conference_name)
conference_url = url_for('conference', _external=True,
conference_name=conference_name)
return jsonify(conference_url=conference_url,
conference_name=conference_name)


@app.route('/api/v1/conference/<conference_name>/', methods=['POST'])
Expand All @@ -150,9 +158,12 @@ def conference_call_api(conference_name):
if conference_exists(conference_name):
to_number = request.form.get('to', None)
clid = request.form.get('clid', config.PLIVO_CALLER_ID)
answer_url = url_for('conf', _external=True, conference_name=conference_name)
answer_url = url_for('conf', _external=True,
conference_name=conference_name)
plivo_conn = get_plivo_connection()
status, _ = plivo_conn.make_call({'to': to_number, 'from': clid, 'answer_url': answer_url, 'answer_method': 'POST'})
status, _ = plivo_conn.make_call({'to': to_number, 'from': clid,
'answer_url': answer_url,
'answer_method': 'POST'})
if status == 201:
return jsonify(success=True, message='Call has been queued')
return jsonify(success=False, message='Call could not be made')
Expand Down Expand Up @@ -184,19 +195,25 @@ def create_plivo_endpoint(conference_name, app_id):
"""

plivo_conn = get_plivo_connection()
_, response = plivo_conn.create_endpoint({'username': conference_name, 'password': conference_name, 'alias': conference_name, 'app_id': app_id})
_, response = plivo_conn.create_endpoint({'username': conference_name,
'password': conference_name,
'alias': conference_name,
'app_id': app_id})
endpoint_username = response['username']
return endpoint_username


def link_conference(conference_name, endpoint_username, endpoint_password, inbound_did=None):
def link_conference(conference_name, endpoint_username, endpoint_password,
inbound_did=None):
"""
Store the conference name in memory along with
endpoint username and password associated with it.
"""

redis_conn = get_redis_connection()
redis_conn.hmset(conference_name, {'username': endpoint_username, 'password': endpoint_password, 'inbound_did': inbound_did})
redis_conn.hmset(conference_name, {'username': endpoint_username,
'password': endpoint_password,
'inbound_did': inbound_did})
if config.EXPIRE_CONFERENCE:
redis_conn.expire(conference_name, 24*60*60)

Expand All @@ -210,9 +227,12 @@ def create_plivo_application(conference_name):
Returns the id of the application created on Plivo.
"""

answer_url = url_for('conf', _external=True, conference_name=conference_name)
answer_url = url_for('conf', _external=True,
conference_name=conference_name)
plivo_conn = get_plivo_connection()
_, response = plivo_conn.create_application({'app_name': conference_name, 'answer_url': answer_url, 'answer_method': 'POST'})
_, response = plivo_conn.create_application({'app_name': conference_name,
'answer_url': answer_url,
'answer_method': 'POST'})
app_id = response['app_id']
return app_id

Expand All @@ -228,7 +248,11 @@ def attach_inbound_did(app_id):
status, response = plivo_conn.get_number_group({'country_iso': 'US'})
try:
group_id = response['objects'][0]['group_id']
status, response = plivo_conn.rent_from_number_group({'group_id': group_id, 'quantity': 1, 'app_id': app_id})
status, response = plivo_conn.rent_from_number_group({'group_id':
group_id,
'quantity': 1,
'app_id':
app_id})
number_rented = response['numbers'][0]['number']
return number_rented
except Exception as e:
Expand All @@ -238,5 +262,3 @@ def attach_inbound_did(app_id):
if __name__ == '__main__':
port = int(os.environ.get('PORT', 5000))
app.run(host='0.0.0.0', port=port)