Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ################
- ### Imports
- ################
- import os
- import operator
- import yaml
- import json
- import urllib.request, urllib.parse
- from time import sleep
- ################
- ### Functions
- ################
- def get_extension(file): #Returns the extension (including full stop) of file.
- return os.path.splitext(file)[1]
- def load_yaml(file):
- with open(file) as f:
- yamlfile = yaml.load(f)
- return yamlfile
- def get_results(searchfor):
- query = urllib.parse.urlencode({'q': searchfor})
- url = 'http://ajax.googleapis.com/ajax/services/search/web?v=1.0&%s' % query
- search_response = urllib.request.urlopen(url)
- search_results = search_response.read().decode("utf8")
- results = json.loads(search_results)
- data = results['responseData']
- return data['cursor']['estimatedResultCount']
- ################
- ### Main code
- ################
- yaml_fname = ""
- while True: #Acquire a YAML filename.
- yaml_fname = input("Please enter the name of your YAML file (excluding extension)\n")+".yaml"
- if os.path.isfile(yaml_fname):
- print("%s was recognised and will be loaded." %yaml_fname)
- break
- else:
- if input("%s was not recognised as a YAML file, would you like to try again? Enter 'q' to quit. Enter anything else to try again.\n" %yaml_fname) == 'q':
- quit()
- try:
- yaml_file = load_yaml(yaml_fname)
- except:
- print ("Oops! Something went wrong with loading your YAML file, after all.")
- results_list = []
- for person in yaml_file["people"]:
- print ("Searching %s on %s..." % (person, yaml_file["url"]))
- results = get_results("site:%s" %yaml_file["url"] + " " + person)
- results_list.append({"person": person, "results": results})
- print ("%s gave %s results." % (person, results))
- print("Sorting list...")
- results_list.sort(key=operator.itemgetter('results'))
- results_list = reversed(results_list)
- print("Results list (formatted; an HTML file containing the list will now be generated):")
- html_file = open(yaml_fname[:-4] + "html", "w")
- html_file.write("<ol>\n")
- for result in results_list:
- print("%s: %s results." %(result["person"], result["results"]))
- full_url = "http://google.com/search?q=site:" + yaml_file["url"] + " " + result["person"]
- html_file.write("<li><a href=\"%s\">%s: %s results</a></li>\n" % (full_url, result["person"], result["results"]))
- input("Done! Press enter to quit.")
Advertisement
Add Comment
Please, Sign In to add comment