Krakob

KnightPopularityCalculator

Jul 27th, 2014
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.31 KB | None | 0 0
  1. ################
  2. ### Imports
  3. ################
  4.  
  5. import os
  6. import operator
  7. import yaml
  8. import json
  9. import urllib.request, urllib.parse
  10. from time import sleep
  11.  
  12. ################
  13. ### Functions
  14. ################
  15.  
  16. def get_extension(file):    #Returns the extension (including full stop) of file.
  17.     return os.path.splitext(file)[1]
  18.  
  19. def load_yaml(file):
  20.     with open(file) as f:
  21.         yamlfile = yaml.load(f)
  22.     return yamlfile
  23.  
  24. def get_results(searchfor):
  25.     query = urllib.parse.urlencode({'q': searchfor})
  26.     url = 'http://ajax.googleapis.com/ajax/services/search/web?v=1.0&%s' % query
  27.     search_response = urllib.request.urlopen(url)
  28.     search_results = search_response.read().decode("utf8")
  29.     results = json.loads(search_results)
  30.     data = results['responseData']
  31.     return data['cursor']['estimatedResultCount']
  32.  
  33.  
  34. ################
  35. ### Main code
  36. ################
  37.  
  38. yaml_fname = ""
  39. while True: #Acquire a YAML filename.
  40.     yaml_fname = input("Please enter the name of your YAML file (excluding extension)\n")+".yaml"
  41.     if os.path.isfile(yaml_fname):
  42.         print("%s was recognised and will be loaded." %yaml_fname)
  43.         break
  44.     else:
  45.         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':
  46.             quit()
  47.  
  48. try:
  49.     yaml_file = load_yaml(yaml_fname)
  50. except:
  51.     print ("Oops! Something went wrong with loading your YAML file, after all.")
  52.  
  53. results_list = []
  54. for person in yaml_file["people"]:
  55.     print ("Searching %s on %s..." % (person, yaml_file["url"]))
  56.     results = get_results("site:%s" %yaml_file["url"] + " " + person)
  57.     results_list.append({"person": person, "results": results})
  58.     print ("%s gave %s results." % (person, results))
  59.  
  60. print("Sorting list...")
  61. results_list.sort(key=operator.itemgetter('results'))
  62. results_list = reversed(results_list)
  63.  
  64. print("Results list (formatted; an HTML file containing the list will now be generated):")
  65. html_file = open(yaml_fname[:-4] + "html", "w")
  66. html_file.write("<ol>\n")
  67. for result in results_list:
  68.     print("%s: %s results." %(result["person"], result["results"]))
  69.     full_url = "http://google.com/search?q=site:" + yaml_file["url"] + " " + result["person"]
  70.     html_file.write("<li><a href=\"%s\">%s: %s results</a></li>\n" % (full_url, result["person"], result["results"]))
  71.  
  72. input("Done! Press enter to quit.")
Advertisement
Add Comment
Please, Sign In to add comment