lolamontes69

Python/ Lola69 Search - terminal web-search(uses faroo api)

May 24th, 2013
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.52 KB | None | 0 0
  1. import os as os
  2. import urllib
  3. import json                        # we are after all importing json
  4. from time import sleep
  5.  
  6. def _wrap_with(code):
  7.     def inner(text, bold=False):
  8.         c = code
  9.         if bold:
  10.             c = "1;%s" % c
  11.         return "\033[%sm%s\033[0m" % (c, text)
  12.     return inner
  13.  
  14. def get_search_results(term1):
  15.     urlstart = "http://www.faroo.com/api?q="
  16.     urlpage = "&start="
  17.     page = 1
  18.     urlend = "&length=10&l=en&src=web&i=false&f=json"
  19.     url = urlstart + term1 + urlpage
  20.  
  21.     def wrap(text, width):
  22.         return reduce(lambda line, word, width=width: '%s%s%s' %
  23.                       (line,' \n'[(len(line)-line.rfind('\n')-1 + len(word.split('\n',1)[0]) >= width)],word),text.split(' '))
  24.  
  25.     def download_it(url):
  26.         red = _wrap_with('31')
  27.         blue = _wrap_with('34')
  28.         white = _wrap_with('37')
  29.         cyan = _wrap_with('36')
  30.         yellow = _wrap_with('33')
  31.         print blue("loading...")
  32.         dic = {}
  33.         conn = urllib.urlopen(url)
  34.         for line in conn:              # comes as a unicode string
  35.             dic = json.loads(line)     # load json into dic
  36.             print "----------------------------------------------------------------------------\n"
  37.             try:
  38.                 for a in range(len(dic['results'])):
  39.                     print cyan('LINK ')+white(str(dic['results'][a]['url']))+"\n"+cyan('LINK NAME ')+white(str(dic['results'][a]['title'])),"\n"
  40.                     if 'kwic' in dic['results'][a]: print yellow(str(dic['results'][a]['kwic']))
  41.                     else: pass
  42.                     print "----------------------------------------------------------------------------\n"
  43.                     sleep(0.5)
  44.             except: pass
  45.         sleep(1.3)
  46.  
  47.     os.system('clear')
  48.     choosea = ''
  49.     while choosea == '':
  50.         url1 = url
  51.         url1 += str(page)
  52.         url1 += urlend
  53.         download_it(url1)
  54.         choosea = raw_input('press <enter> to fetch another or enter "q" to quit \n')
  55.         page += 1
  56.     hello()
  57.  
  58. def hello():
  59.     ch0 = """#######################################\n#"""
  60.     ch1 = """    _          _        __   ___"""
  61.     ch2 = """     #\n#   """
  62.     ch3 = """| |    ___ | | __ _ / /_ / _ \ """
  63.     ch4 = """   #\n#   """
  64.     ch5 = """| |   / _ \| |/ _  |  _ \ (_) |  """
  65.     ch6 = """ #\n#   """
  66.     ch7 = """| |__| (_) | | (_| | (_) \__, |   """
  67.     ch8 = """#\n#   """
  68.     ch9 = """|_____\___/|_|\__,_|\___/  /_/   """
  69.     ch10= """ #\n#  """
  70.     ch11 = """____                      _   """
  71.     ch12 = """     #\n# """
  72.     ch13 = """/ ___|  ___  __ _ _ __ ___| |__   """
  73.     ch14 = """  #\n# """
  74.     ch15 = """\___ \ / _ \/ _  | '__/ __|  _ \   """
  75.     ch16 = """#\n#  """
  76.     ch17 = """___) |  __/ (_| | | | (__| | | |  """
  77.     ch18 = """ #\n# """
  78.     ch19 = """|____/ \___|\__,_|_|  \___|_| |_|  """
  79.     ch20 = """ #\n#                    Powered by FAROO #\n#######################################\n"""
  80.     ch21 = """Enter search term or q to quit: """
  81.  
  82.     red = _wrap_with('31')
  83.     magenta = _wrap_with('35')
  84.     cyan = _wrap_with('36')
  85.  
  86.     ch = red(ch0)+magenta(ch1)+red(ch2)+magenta(ch3)+red(ch4)+magenta(ch5)+red(ch6)+magenta(ch7)+red(ch8)+magenta(ch9)+red(ch10)+magenta(ch11)+red(ch12)+magenta(ch13)+red(ch14)+magenta(ch15)+red(ch16)+magenta(ch17)+red(ch18)+magenta(ch19)+red(ch20)+cyan(ch21)
  87.     os.system('clear')
  88.     term = str(raw_input(ch))
  89.     term1 = term.replace(" ","%20")
  90.     if term1 == 'q':
  91.         pass
  92.     else: get_search_results(term1)
  93.  
  94. hello()
Advertisement
Add Comment
Please, Sign In to add comment