DaEvil1

Untitled

Aug 4th, 2014
284
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.06 KB | None | 0 0
  1. import urllib.request
  2. import praw
  3. import re
  4. import csv
  5. import sys
  6. import datetime
  7. from bs4 import BeautifulSoup
  8.  
  9. sub = sys.argv[1]
  10.  
  11. title_format = re.compile(r"(?:\*){0,2}Title(?:\*){0,2}:(?:\*){0,2} (.+)", re.IGNORECASE)
  12. type_format = re.compile(r"(?:\*){0,2}Type(?:\*){0,2}:(?:\*){0,2} (.+)", re.IGNORECASE)
  13. map_format = re.compile(r"(?:\*){0,2}Map(?:\*){0,2}:(?:\*){0,2} (.+)", re.IGNORECASE)
  14. preview_format = re.compile(r"(?:\*){0,2}Preview(?:\*){0,2}:(?:\*){0,2} (.+)", re.IGNORECASE)
  15. description_format = re.compile(r"(?:\*){0,2}Description(?:\*){0,2}:(?:\*){0,2} (.+)", re.IGNORECASE)
  16.  
  17. def get_map_id(url):
  18. try:
  19. data = urllib.request.urlopen(url)
  20. page = BeautifulSoup(data)
  21. return page.find('a', "testmap")['mapid']
  22. except:
  23. print("Invalid URL {}, skipping.".format(url))
  24. return ""
  25. r = praw.Reddit("Maptest Committee Extractor")
  26. matches = [["Title", "Type", "Map", "Preview", "Preview Link"]]
  27.  
  28. thread_data = r.get_submission(submission_id=sub)
  29. thread_data.replace_more_comments(limit=None, threshold=0)
  30. comments = thread_data.comments
  31.  
  32. for comment in comments:
  33. if isinstance(comment, praw.objects.MoreComments): continue
  34. titles = title_format.search(comment.body)
  35. types = type_format.search(comment.body)
  36. maps = map_format.search(comment.body)
  37. preview = preview_format.search(comment.body)
  38. description = comment.permalink
  39. if titles and types and maps and preview:
  40. image = preview.groups()[0].strip().strip("*")
  41. if not any([x in image for x in (".png", ".jpg", ".gif")]) and "imgur" in image:
  42. image += ".png"
  43. link = "=HYPERLINK(\"http://tagpromaptest.appspot.com/somebot/{}\", \"Test Map\")".format(get_map_id(maps.groups()[0].strip().strip("*")))
  44. out = ["=HYPERLINK(\"{}\", \"{}\")".format(description, titles.groups()[0].replace("*", "")), types.groups()[0].replace("*", ""), link, "=image(\"{}\", 1)".format(image), '=HYPERLINK("{}", "Fullsize preview")'.format(image)]
  45. matches.append(out)
  46. with open("Desktop/maps_{}.txt".format(datetime.date.today().strftime("%B_%d_%Y")), "w") as f:
  47. a = csv.writer(f, delimiter='\t', quotechar="{")
  48. a.writerows(matches)
Advertisement
Add Comment
Please, Sign In to add comment