<s> import json <EOL> import datetime <EOL> import requests <EOL> from flask import request , render_template <EOL> from flask . ext . cors import cross_origin <EOL> try : <EOL> from urllib . parse import urlparse <EOL> except ImportError : <EOL> from urlparse import urlparse <EOL> from alerta . app import app <EOL> from alerta . alert import Alert <EOL> from alerta . app . utils import jsonify , jsonp <EOL> from alerta . app . metrics import Timer <EOL> LOG = app . logger <EOL> oembed_timer = Timer ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> @ app . route ( '<STR_LIT>' , defaults = { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> @ app . route ( '<STR_LIT>' , methods = [ '<STR_LIT>' , '<STR_LIT:GET>' ] ) <EOL> @ cross_origin ( ) <EOL> @ jsonp <EOL> def oembed ( format ) : <EOL> oembed_started = oembed_timer . start_timer ( ) <EOL> if format != '<STR_LIT>' : <EOL> oembed_timer . stop_timer ( oembed_started ) <EOL> return jsonify ( status = "<STR_LIT:error>" , message = "<STR_LIT>" % format ) , <NUM_LIT> <EOL> if '<STR_LIT:url>' not in request . args or '<STR_LIT>' not in request . args or '<STR_LIT>' not in request . args : <EOL> oembed_timer . stop_timer ( oembed_started ) <EOL> return jsonify ( status = "<STR_LIT:error>" , message = "<STR_LIT>" ) , <NUM_LIT> <EOL> try : <EOL> url = request . args [ '<STR_LIT:url>' ] <EOL> key = request . args . get ( '<STR_LIT>' ) <EOL> width = int ( request . args [ '<STR_LIT>' ] ) <EOL> height = int ( request . args [ '<STR_LIT>' ] ) <EOL> title = request . args . get ( '<STR_LIT:title>' , '<STR_LIT>' ) <EOL> except Exception as e : <EOL> oembed_timer . stop_timer ( oembed_started ) <EOL> return jsonify ( status = "<STR_LIT:error>" , message = str ( e ) ) , <NUM_LIT> <EOL> try : <EOL> o = urlparse ( url ) <EOL> except Exception as e : <EOL> oembed_timer . stop_timer ( oembed_started ) <EOL> return jsonify ( status = "<STR_LIT:error>" , message = str ( e ) ) , <NUM_LIT> <EOL> headers = dict ( ) <EOL> if key : <EOL> headers = { '<STR_LIT>' : '<STR_LIT>' + key } <EOL> response = requests . get ( url = url , headers = headers ) <EOL> try : <EOL> response . raise_for_status ( ) <EOL> except requests . HTTPError as e : <EOL> oembed_timer . stop_timer ( oembed_started ) <EOL> return jsonify ( status = "<STR_LIT:error>" , message = str ( e ) ) , response . status_code <EOL> if o . path . endswith ( '<STR_LIT>' ) : <EOL> counts = response . json ( ) . get ( '<STR_LIT>' , dict ( ) ) <EOL> max = '<STR_LIT>' <EOL> if counts . get ( '<STR_LIT>' , <NUM_LIT:0> ) > <NUM_LIT:0> : <EOL> max = '<STR_LIT>' <EOL> if counts . get ( '<STR_LIT>' , <NUM_LIT:0> ) > <NUM_LIT:0> : <EOL> max = '<STR_LIT>' <EOL> if counts . get ( '<STR_LIT>' , <NUM_LIT:0> ) > <NUM_LIT:0> : <EOL> max = '<STR_LIT>' <EOL> if counts . get ( '<STR_LIT>' , <NUM_LIT:0> ) > <NUM_LIT:0> : <EOL> max = '<STR_LIT>' <EOL> html = render_template ( <EOL> '<STR_LIT>' , <EOL> title = title , <EOL> width = width , <EOL> height = height , <EOL> max = max , <EOL> counts = counts <EOL> ) <EOL> oembed_timer . stop_timer ( oembed_started ) <EOL> return jsonify ( version = "<STR_LIT:1.0>" , type = "<STR_LIT>" , width = width , height = height , title = title , provider_name = "<STR_LIT>" , provider_url = request . url_root , html = html ) <EOL> else : <EOL> oembed_timer . stop_timer ( oembed_started ) <EOL> return jsonify ( status = "<STR_LIT:error>" , message = "<STR_LIT>" ) , <NUM_LIT> <EOL> @ app . route ( '<STR_LIT>' , methods = [ '<STR_LIT>' , '<STR_LIT:GET>' ] ) <EOL> def embed_js ( ) : <EOL> return app . send_static_file ( '<STR_LIT>' ) </s>
