Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import urllib.request
- import praw
- import re
- import csv
- import sys
- import datetime
- from bs4 import BeautifulSoup
- sub = sys.argv[1]
- title_format = re.compile(r"(?:\*){0,2}Title(?:\*){0,2}:(?:\*){0,2} (.+)", re.IGNORECASE)
- type_format = re.compile(r"(?:\*){0,2}Type(?:\*){0,2}:(?:\*){0,2} (.+)", re.IGNORECASE)
- map_format = re.compile(r"(?:\*){0,2}Map(?:\*){0,2}:(?:\*){0,2} (.+)", re.IGNORECASE)
- preview_format = re.compile(r"(?:\*){0,2}Preview(?:\*){0,2}:(?:\*){0,2} (.+)", re.IGNORECASE)
- description_format = re.compile(r"(?:\*){0,2}Description(?:\*){0,2}:(?:\*){0,2} (.+)", re.IGNORECASE)
- def get_map_id(url):
- try:
- data = urllib.request.urlopen(url)
- page = BeautifulSoup(data)
- return page.find('a', "testmap")['mapid']
- except:
- print("Invalid URL {}, skipping.".format(url))
- return ""
- r = praw.Reddit("Maptest Committee Extractor")
- matches = [["Title", "Type", "Map", "Preview", "Preview Link"]]
- thread_data = r.get_submission(submission_id=sub)
- thread_data.replace_more_comments(limit=None, threshold=0)
- comments = thread_data.comments
- for comment in comments:
- if isinstance(comment, praw.objects.MoreComments): continue
- titles = title_format.search(comment.body)
- types = type_format.search(comment.body)
- maps = map_format.search(comment.body)
- preview = preview_format.search(comment.body)
- description = comment.permalink
- if titles and types and maps and preview:
- image = preview.groups()[0].strip().strip("*")
- if not any([x in image for x in (".png", ".jpg", ".gif")]) and "imgur" in image:
- image += ".png"
- link = "=HYPERLINK(\"http://tagpromaptest.appspot.com/somebot/{}\", \"Test Map\")".format(get_map_id(maps.groups()[0].strip().strip("*")))
- out = ["=HYPERLINK(\"{}\", \"{}\")".format(description, titles.groups()[0].replace("*", "")), types.groups()[0].replace("*", ""), link, "=image(\"{}\", 1)".format(image), '=HYPERLINK("{}", "Fullsize preview")'.format(image)]
- matches.append(out)
- with open("Desktop/maps_{}.txt".format(datetime.date.today().strftime("%B_%d_%Y")), "w") as f:
- a = csv.writer(f, delimiter='\t', quotechar="{")
- a.writerows(matches)
Advertisement
Add Comment
Please, Sign In to add comment