Created
December 6, 2016 18:07
-
-
Save soeffing/cf448fa6b54daf050f262042bf8d0676 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
from flask import Flask | |
from flask import request | |
from pymongo import MongoClient | |
client = MongoClient() | |
client = MongoClient('localhost', 27017) | |
db = client.serp_v2 | |
keywords_col = db.keywords | |
urls_col = db.urls | |
app = Flask(__name__) | |
@app.route('/callback', methods=['GET', 'POST']) | |
def callback(): | |
print request.args.get('callback_param') | |
keyword_param = request.args.get('callback_param') | |
print keyword_param | |
# find keyword in db | |
keyword = keywords_col.find_one({'term': keyword_param}) | |
print keyword | |
try: | |
vertifire_key = keyword['vertifire_key'] | |
except KeyError: | |
print 'no vertifire key' | |
print keyword_param | |
print keyword | |
return 'OK' | |
vertifire_res = requests.get("https://api.vertifire.com/v1/response/" + vertifire_key, | |
headers={'X-Vertifire-Token': 'TOKEN' } ) | |
#print vertifire_res | |
#print vertifire_res.text | |
j_res = vertifire_res.json() | |
for serp in j_res['response'][0]['results']['organic']: | |
# some serps do not have descriptions | |
if 'description' not in serp.keys(): | |
serp['description'] = '' | |
try: | |
description = serp['description'].encode('utf-8').strip().replace('"', "'") | |
title = serp['title'].encode('utf-8').strip().replace('"', "'") | |
new_url = { | |
'rank': serp['rank'], | |
'url': serp['url'], | |
'title': title, | |
'description': description, | |
'keyword_id': keyword['_id'] | |
} | |
urls_col.insert_one(new_url) | |
except: | |
print 'Error writing to db' | |
print serp['description'] | |
print serp['url'] | |
print serp['title'] | |
print keyword['_id'] | |
return 'Ok' | |
app.run(host='0.0.0.0', port=8300) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment