Source

toolbox / site.py

Full commit
#!/usr/bin/env python
#-*- coding: utf-8 -*-

import sys
import os
import logging
import apesmit
import csv
import cherrypy
import StringIO
import cgi
import tempfile
import smxml_template
from string import Template

try:
    import json
except ImportError:
    import simplejson as json
from jinja2 import Environment, FileSystemLoader
from urllib import urlopen, quote
import codecs

def urlencode(string):
    return quote(string.encode('utf-8'))

env = Environment(loader=FileSystemLoader('templates'))
env.filters['urlencode'] = urlencode

def get_suggestion(source, query):
    if source == 'google':
        url = "http://clients1.google.it/complete/search?hl=it&q=%s&cp=2&hjson=t" % query
    if source == 'youtube':
        url = "http://suggestqueries.google.com/complete/search?hl=it&client=youtube&hjson=t&ds=yt&q=%s&cp=1" % query
    if source == 'gnews':
        url = "http://news.google.it/complete/search?hl=it&gl=it&ds=n&nolabels=t&hjson=t&q=%s" % query
    try:
        response = urlopen(url).read()
    except IOError, e:
        return None
    print url
    result = json.loads(response)
    return result

class Usuggest(object):
    """
    Ubersuggest!
    """
    MEDIA_URL="http://www.thinkpragmatic.net/media/ubersuggest/"
    template = env.get_template('index.html')
    
    def index(self, query=None):
        errors = []
        if query is None:
            out = self.template.render(query=query, media=self.MEDIA_URL, errors=None)
        elif query == '':
            errors.append("Please enter at least one character")
            out = self.template.render(errors=errors, query=None, media=self.MEDIA_URL)
        else:
            query = quote(query)
            gnews = get_suggestion('gnews', query)
            google = get_suggestion('google', query)
            youtube = get_suggestion('youtube', query)
            if gnews is None:
                errors.append("Huston, we have a problem with Google News")
            if google is None:
                errors.append("Huston, we have a problem with Google")
            if youtube is None:
                errors.append("Huston, we have a problem with Youtube")
            if len(errors) == 0: errors = None
            out = self.template.render(gnews=gnews, google=google,
                    youtube=youtube, query=query, errors=errors, media=self.MEDIA_URL)
        return out

    index.exposed = True

class Smxml(object):

    MEDIA_URL="http://www.thinkpragmatic.net/media/smxml/"
    
    def index(self):
        template_dict = dict(url=self.MEDIA_URL)
        return Template(smxml_template.index).safe_substitute(template_dict)
    index.exposed = True
    
    def upload(self, myFile):
        data = myFile.file.readlines()
        rows = csv.reader(data)

        sitemap = apesmit.Sitemap()
        urls_number = 0
        for row in rows:
            changefreq = priority = lastmod = None
            if row[0] != 'URL' and row[0] != '':
                url = row[0]
                if row[1] != '': changefreq = row[1]
                if row[2] != '': priority = row[2]
                if row[3] != '': lastmod = row[3]
                sitemap.add(url, changefreq=changefreq, priority=priority, lastmod=lastmod)
                urls_number += 1
        
        output = StringIO.StringIO()
        sitemap.write(output)
        template_dict = dict(url=self.MEDIA_URL, urls_number=urls_number, xml_sitemap=cgi.escape(output.getvalue()))

        out = Template(smxml_template.sitemap_display)
        
        return out.safe_substitute(template_dict)
    upload.exposed = True
    
    def download(self, file_content):
        f = tempfile.NamedTemporaryFile(prefix='sitemap_', suffix='.xml')
        f.file.write(file_content)
        f.file.close()
        path = f.name
        return static.serve_file(path, "application/x-download",
                                 "attachment", os.path.basename(path))
    download.exposed = True

class Tools(object):
    smxml = Smxml()
    usuggest = Usuggest()

    def index(self):
        raise cherrypy.HTTPRedirect("http://www.thinkpragmatic.net/toolbox/", 301)
    index.exposed = True
    
cherrypy.config.update({
    #'environment': 'production',
    'log.screen': True,
    'server.socket_host': '127.0.0.1',
    'server.socket_port':33293,
    'tools.encode.on':True,
    'tools.encode.encoding':'utf8',
    'tools.gzip.on' : True,
    'error_page.404':'template/404.html  })

cherrypy.quickstart(Tools(), '/toolbox')