def tf_complete_job(job_id, tf_job_id):
	townflix.API_BEARER_TOKEN = _authenticate()

	# if is authenticated
	if townflix.API_BEARER_TOKEN is not None:

		post_data = {
			'job_id': str(tf_job_id),
			'origin_id': str(job_id)
		}

		auth_value = 'Bearer %s' % townflix.API_BEARER_TOKEN
		headers = {
			'Authorization': auth_value
		}

		endpoint = TOWNFLIX_API_ROOT % '/rest/v2/jobs/complete'
		result = execute_post(endpoint, post_data, headers, 'json')

		if result['status_code'] == 200:
			return True
		else:
			return None

	return None
def sort_photos(listing_id):
	headers = {
		'token': VALUEGAIA_TOKEN
	}

	endpoint = VALUEGAIA_API_ROOT % '/GaiaFlixOrderPhotos.ashx'

	post_data = {
		'id_imovel': listing_id
	}

	result = execute_post(endpoint, post_data, headers, 'urlencode', 'json')
	return result
def _authenticate():
	endpoint = TOWNFLIX_API_ROOT % '/oauth/token'
	post_data = {
		'client_id': API_CLIENT_ID,
		'client_secret': API_CLIENT_SECRET,
		'grant_type': 'client_credentials'
	}
	result = execute_post(endpoint, post_data)

	if result['status_code'] == 200:
		return result['body']['access_token']
	else:
		return None
def _create_job(post_data):
	auth_value = 'Bearer %s' % townflix.API_BEARER_TOKEN
	headers = {
		'Authorization': auth_value
	}

	endpoint = TOWNFLIX_API_ROOT % '/rest/v2/jobs/create'
	result = execute_post(endpoint, post_data, headers, 'json')

	if result['status_code'] == 200:
		return result['body']
	else:
		return None
def _reschedule_job(tf_job_id, post_data):
	auth_value = 'Bearer %s' % townflix.API_BEARER_TOKEN
	headers = {
		'Authorization': auth_value
	}

	endpoint = TOWNFLIX_API_ROOT % '/rest/v1/job/reschedule?id=' + str(tf_job_id)
	result = execute_post(endpoint, post_data, headers, 'urlencode', 'raw')

	if result['status_code'] == 200:
		return result['body']
	else:
		return None
def _get_token():
	params = {
		'login': '******',
		'password': '******'
	}

	resource = '/api/authentication/signin'
	endpoint = URL_ROOT % resource
	response = execute_post(endpoint, params)

	if response['status_code'] == 200:
		result = response['body']
		return result['token']
	else:
		return None
def insert_video(listing_id, townflix_code, thumbnail):
	headers = {
		'token': VALUEGAIA_TOKEN
	}

	endpoint = VALUEGAIA_API_ROOT % '/GaiaFlixVideo.ashx'

	post_data = {
		'id_imovel': listing_id,
		'codigo_town_flix': townflix_code,
		'thumbnail': thumbnail,
		'tela_cheia': 'True'
	}

	result = execute_post(endpoint, post_data, headers, 'urlencode', 'json')
	return result
def _cancel_job(job_id, tf_job_id):
	auth_value = 'Bearer %s' % townflix.API_BEARER_TOKEN
	headers = {
		'Authorization': auth_value
	}
	post_data = {
		'origin_id': str(job_id),
		'job_id': str(tf_job_id)
	}

	resource = '/rest/v2/jobs/cancel'
	endpoint = TOWNFLIX_API_ROOT % resource
	result = execute_post(endpoint, post_data, headers, 'json')

	if result['status_code'] == 200:
		return result['body']
	else:
		return None
def tf_redo_job(job_id, tf_job_id, medias):
	townflix.API_BEARER_TOKEN = _authenticate()

	# if is authenticated
	if townflix.API_BEARER_TOKEN is not None:

		tf_medias = []

		for p in medias['photos']:
			if 'comment' in p:
				tf_medias.append({
						'server_id': p['media_id'],
						'approved': False,
						'notes': p['comment']
					})

		for v in medias['videos']:
			if 'comment' in v:
				tf_medias.append({
						'server_id': v['media_id'],
						'approved': False,
						'notes': v['comment']
					})

		post_data = {
			'job_id': str(tf_job_id),
			'origin_id': str(job_id),
			'medias': tf_medias
		}

		auth_value = 'Bearer %s' % townflix.API_BEARER_TOKEN
		headers = {
			'Authorization': auth_value
		}

		endpoint = TOWNFLIX_API_ROOT % '/rest/v2/jobs/redo'
		result = execute_post(endpoint, post_data, headers, 'json')

		if result['status_code'] == 200:
			return True
		else:
			return None
	return None
def insert_photo(listing_id, original, caption, featured):
	headers = {
		'token': VALUEGAIA_TOKEN
	}

	endpoint = VALUEGAIA_API_ROOT % '/GaiaFlixPhoto.ashx'
	watermarked = original + '=s2000'
	thumbnail = original + '=s100'
	original = original + '=s2000'

	post_data = {
		'id_imovel': listing_id,
		'foto_grande': original,
		'foto_grande_marca_agua': watermarked,
		'foto_miniatura': thumbnail,
		'destaque': featured,
		'legenda': caption
	}

	result = execute_post(endpoint, post_data, headers, 'urlencode', 'json')
	return result
def test_send_lead():
    listing_id = request.args["listing_id"]

    post_data = {
        "property_owner": {"email": "*****@*****.**", "name": "Armando Miani", "phone": "19999385915"},
        "source_application": "GAIA",
        "agency": {
            "logomark": "http://www.casabrancanet.com.br/modelo/img/logo.png",
            "name": "Casa Branca",
            "website": "casabrancanet.net.br",
            "identifier": "CSBR",
        },
        "sender": {"email": "*****@*****.**", "name": "Corretor Casa Branca", "phone": "19987896541"},
        "listing": {
            "id": int(listing_id),
            "reference": "CA-0001",
            "title": "Casa em Campinas",
            "photo_url": "http://cdn1.valuegaia.com.br/watermark/agencies/3027/properties/66786302759AE6D3266C773AF635EC27FD485CE2BD3F22A68.jpg",
            "location": {
                "street": "Av. José de Souza Campos",
                "street_number": "1547",
                "city": "Campinas",
                "neighbourhood": "Cambuí",
                "state": "São Paulo",
                "external_id": 666,
            },
            "listing_type": "Casa",
            "description": ["this", "is the", "property", "description"],
        },
        "plan": "pack_valuegaia",
    }

    endpoint = "%s/leads" % API_ROOT_URL
    result = ch.execute_post(endpoint, post_data, {}, "json")

    if result["status_code"] == 201:
        return jsonify(data={"token": result["body"]["data"]["token"], "id": result["body"]["data"]["id"]})
    return jsonify(data=result["body"]), 500
def test_publish(job_id):
    post_data = {
        "photos": [
            {
                "url": "http://fotos-valuegaia.s3.amazonaws.com/gaiaflix/112/so4196/IMG_4828.JPG",
                "title": "Garagem",
                "media_id": "a112",
            },
            {
                "url": "http://fotos-valuegaia.s3.amazonaws.com/gaiaflix/112/so4196/IMG_4829.JPG",
                "title": "Garagem",
                "media_id": "a113",
            },
            {
                "url": "http://fotos-valuegaia.s3.amazonaws.com/gaiaflix/112/so4196/IMG_4831.JPG",
                "title": "Entrada",
                "media_id": "a114",
            },
            {
                "url": "http://fotos-valuegaia.s3.amazonaws.com/gaiaflix/112/so4196/IMG_4834.JPG",
                "title": "Sala",
                "media_id": "a115",
            },
            {
                "url": "http://fotos-valuegaia.s3.amazonaws.com/gaiaflix/112/so4196/IMG_4835.JPG",
                "title": "Sala",
                "media_id": "a116",
            },
            {
                "url": "http://fotos-valuegaia.s3.amazonaws.com/gaiaflix/112/so4196/IMG_4839.JPG",
                "title": "Sala",
                "media_id": "a117",
            },
            {
                "url": "http://fotos-valuegaia.s3.amazonaws.com/gaiaflix/112/so4196/IMG_4841.JPG",
                "title": "Toillete",
                "media_id": "a118",
            },
            {
                "url": "http://fotos-valuegaia.s3.amazonaws.com/gaiaflix/112/so4196/IMG_4845.JPG",
                "title": "Corredor",
                "media_id": "a119",
            },
            {
                "url": "http://fotos-valuegaia.s3.amazonaws.com/gaiaflix/112/so4196/IMG_4850.JPG",
                "title": "Cozinha",
                "media_id": "a121",
            },
            {
                "url": "http://fotos-valuegaia.s3.amazonaws.com/gaiaflix/112/so4196/IMG_4851.JPG",
                "title": "Cozinha",
                "media_id": "a122",
            },
            {
                "url": "http://fotos-valuegaia.s3.amazonaws.com/gaiaflix/112/so4196/IMG_4859.JPG",
                "title": "Quarto",
                "media_id": "a123",
            },
            {
                "url": "http://fotos-valuegaia.s3.amazonaws.com/gaiaflix/112/so4196/IMG_4860.JPG",
                "title": "Quarto",
                "media_id": "a124",
            },
            {
                "url": "http://fotos-valuegaia.s3.amazonaws.com/gaiaflix/112/so4196/IMG_4863.JPG",
                "title": "Corredor",
                "media_id": "a125",
            },
            {
                "url": "http://fotos-valuegaia.s3.amazonaws.com/gaiaflix/112/so4196/IMG_4867.JPG",
                "title": "Quarto",
                "media_id": "a126",
            },
            {
                "url": "http://fotos-valuegaia.s3.amazonaws.com/gaiaflix/112/so4196/IMG_4868.JPG",
                "title": "Quarto",
                "media_id": "a127",
            },
            {
                "url": "http://fotos-valuegaia.s3.amazonaws.com/gaiaflix/112/so4196/IMG_4873.JPG",
                "title": "Quarto",
                "media_id": "a128",
            },
            {
                "url": "http://fotos-valuegaia.s3.amazonaws.com/gaiaflix/112/so4196/IMG_4876.JPG",
                "title": "Banheiro",
                "media_id": "a129",
            },
            {
                "url": "http://fotos-valuegaia.s3.amazonaws.com/gaiaflix/112/so4196/IMG_4826.JPG",
                "title": "Fachada",
                "media_id": "a111",
            },
        ],
        "videos": [
            {
                "url": "http://www.townfilx.com/v/1_uwypaz22",
                "thumbnail": "http://www.kaltura.com/p/931471/thumbnail/width/180/height/180/type/2/bgcolor/000000/entry_id/1_6hn34cmw",
            }
        ],
    }

    endpoint = "%s/jobs/%s/publish" % (API_ROOT_URL, job_id)
    result = ch.execute_post(endpoint, post_data, {}, "json")

    success = "ok" if result["status_code"] == 201 else "erro"
    return jsonify(data=success), result["status_code"]