Created
May 26, 2014 18:17
-
-
Save j0lvera/9e7318f024f8b3958b0f to your computer and use it in GitHub Desktop.
seoapp
This file contains 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
#!/usr/bin/env python | |
# bottle.py boilerplate project | |
from bottle import route, run, template, static_file, request, response | |
from bs4 import BeautifulSoup | |
import requests, simplejson as json, webbrowser, os, lxml, re | |
# the decorator | |
def enable_cors(fn): | |
def _enable_cors(*args, **kwargs): | |
# set CORS headers | |
response.headers['Access-Control-Allow-Origin'] = '*' | |
response.headers['Access-Control-Allow-Methods'] = 'GET, POST, PUT, OPTIONS' | |
response.headers['Access-Control-Allow-Headers'] = 'Origin, Accept, Content-Type, X-Requested-With, X-CSRF-Token' | |
if request.method != 'OPTIONS': | |
# actual request; reply with the actual response | |
return fn(*args, **kwargs) | |
return _enable_cors | |
# Methods | |
def get_get(name, default=""): | |
"""grab param from request""" | |
return request.GET.get(name, default).strip() | |
def get_data(url): | |
"""method to make a request and return response""" | |
r = requests.get(url) | |
return r.text | |
def get_html(url): | |
"""grab the html from a website""" | |
page = requests.get(url) | |
return page.content | |
## Social | |
def get_gplus_profile_picture(id): | |
"""get the picture from the google plus profile""" | |
api_key = "AIzaSyAvAReWehjj1JzbGOwo9Ac6bNDRZ6gIo0U" | |
end_point = str.encode('https://www.googleapis.com/plus/v1/people/' + id + '?key=' + api_key) | |
print end_point | |
return get_data(end_point) | |
def get_facebook(url): | |
"""likes, comments and shares count""" | |
facebook_url = 'https://api.facebook.com/method/fql.query?query=select total_count,like_count,comment_count,share_count,click_count from link_stat where url="' + url + '"&format=json' | |
return get_data(facebook_url) | |
def get_twitter(url): | |
"""tweets count""" | |
twitter_url = 'http://urls.api.twitter.com/1/urls/count.json?url=' + url | |
return get_data(twitter_url) | |
def get_google_plus(url): | |
"""+1's count""" | |
google_url = 'https://plusone.google.com/_/+1/fastbutton?url=' + url | |
html = get_data(google_url) | |
soup = BeautifulSoup(html) | |
tag = soup.find_all(id="aggregateCount")[0] | |
count = tag.string.extract() | |
return count | |
def get_linkedin(url): | |
"""mentions count""" | |
linkedin_url = 'http://www.linkedin.com/countserv/count/share?url=' + url + '&format=json' | |
return get_data(linkedin_url) | |
def get_pinterest(url): | |
"""pinterest share count""" | |
pinterest_url = 'http://api.pinterest.com/v1/urls/count.json?url=' + url | |
return get_data(pinterest_url) | |
def get_stumbleupon(url): | |
"""views count""" | |
stumbleupon_url = 'http://www.stumbleupon.com/services/1.01/badge.getinfo?url=' + url + '&format=jsonp' | |
return get_data(stumbleupon_url) | |
def get_delicious(url): | |
"""bookmarked count""" | |
delicious_url = 'http://feeds.delicious.com/v2/json/urlinfo/data?url=' + url | |
return get_data(delicious_url) | |
def get_reddit(url): | |
"""mentions count""" | |
reddit_url = 'http://www.reddit.com/api/info.json?url=' + url | |
return get_data(reddit_url) | |
def get_social(url): | |
"""return json formated all related with social media""" | |
# url = get_get('url') | |
facebook = json.loads(get_facebook(url))[0] | |
twitter = json.loads(get_twitter(url)) | |
google_plus = get_google_plus(url) | |
linkedin = json.loads(get_linkedin(url)) | |
pinterest = get_pinterest(url) | |
stumbleupon = json.loads(get_stumbleupon(url)) | |
delicious = json.loads(get_delicious(url)) | |
reddit = json.loads(get_reddit(url)) | |
return { 'facebook' : facebook, 'twitter' : twitter, 'google_plus' : google_plus, 'linkedin' : linkedin , 'pinterest' : pinterest, 'stumbleupon' : stumbleupon, 'delicious' : delicious, 'reddit' : reddit } | |
## SEO | |
def get_seo(url): | |
"""...""" | |
# url = get_get('url') | |
html = get_html(url) | |
soup = BeautifulSoup(html) | |
header = str(soup.header) | |
title = str(soup.title) | |
body = str(soup.body) | |
footer = str(soup.footer) | |
html = soup.prettify() | |
# get text from body without script tag | |
no_scripts = soup.find_all('script') | |
[script.extract() for script in no_scripts] | |
body_text = soup.body.get_text() # strip = True quita los espacios y enter, checar si lo dejo o lo quito | |
all_links = str(soup.find_all('a')) | |
return { | |
'html' : soup.prettify() | |
} | |
@route('/') | |
def index(): | |
"""if user enter / return index.html""" | |
return static_file('index.html', root='./public/dev/') | |
@route('/<filename:path>') | |
def files(filename): | |
"""show static files""" | |
return static_file(filename, root='./public/dev/') | |
# @route('/social', method="GET") | |
# @route('/seo', method="GET") | |
@route('/url', method=["OPTIONS", "GET"]) | |
@enable_cors | |
def url(): | |
"""...""" | |
project = get_get('url') | |
social = get_social(project) | |
seo = get_seo(project) | |
return { 'social': social, 'seo': seo } | |
@route('/gplusp', method=['OPTIONS', 'GET']) | |
@enable_cors | |
def gplusp(): | |
"""return google+ profile data""" | |
print get_get('id') | |
gplus_id = get_get('id') | |
gplus_profile = get_gplus_profile_picture(gplus_id) | |
return { 'gplus_data': gplus_profile } | |
# run(host='localhost', port=8080, debug=True, reloader=True) | |
run(host='0.0.0.0', port=int(os.environ.get('PORT', 5000))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment