#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright (C) 2010 Harang Camille <mammique@yooook.net>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

# Added by mammique 25/07/2002
# the GNU General Public License http://www.gnu.org/licenses/gpl.txt or COPYING file

import sys
from optparse import OptionParser

parser = OptionParser()
parser.add_option("-u", "--base-url", dest="url", default='http://seeks.fr/search',
                  help="Base URL of the target Seeks node, default: %default")
parser.add_option("-e", "--expansion", dest="expansion", default=1, type="int",
                  help="Expansion level, default: %default")
parser.add_option("-a", "--action", dest="action", default="expand",
                  help="Action expand|page|types|clusterize|similarity, default: %default")
parser.add_option("--engines", dest="engines",
                  help="Commas separated selection of search engines (e.g. bing,cuil)")
parser.add_option("-p", "--page", dest="page", default=1, type="int",
                  help="Page number, default: %default")
#parser.add_option("--thumbs", dest="thumbs", action="store_true",
#                  help="Activates thumbnails")
parser.add_option("-c", "--clusters", dest="clusters", type="int",
                  help="Maximum number of clusters (groups), forces action to clusterize")
parser.add_option("--id", dest="id", type="int",
                  help="ID of a reference content, forces action to similarity")

(options, args) = parser.parse_args()

if options.clusters: options.action = 'clusterize'
elif options.id: options.action = 'similarity'

import urllib2

query = u"?q=%s&output=json" % urllib2.quote(" ".join(args))

for a in "expansion", "action", "engines", "page", "clusters", "id":
    v = getattr(options, a)
    if v: query += u"&%s=%s" % (a, urllib2.quote(unicode(v)))

url = options.url + query

opener = urllib2.build_opener()
opener.addheaders = [('User-agent', 'Seeks CLI')]
json_string = opener.open(urllib2.Request(url)).read()

import json
if sys.version_info >= (2, 6): results = json.loads(json_string)
else: results = json.read(json_string)

o = sys.stdout

for s in results['snippets']:
    o.write(u"\n")
    o.write(u"URL: %s\n" % s['url'])
    if sys.version_info >= (2, 6): o.write("Summary: %s\n" % s['summary'][:70].encode('utf-8', 'replace'))
    else: o.write("Summary: %s\n" % s['summary'][:70])
    engines = u', '.join(map(lambda x: x.title(), s['engines']))
    o.write(u"Seeks score: %s | Rank: %s | Engines: %s\n" % (s['seeks_score'], s['rank'], engines))

o.write(u"\n%s results processed in %s seconds on %s.\n\n" % (len(results['snippets']), results['qtime'], results['date']))

