Skip to content

Instantly share code, notes, and snippets.

@eshleebien
Last active September 16, 2015 03:18
Show Gist options
  • Save eshleebien/e5aa72fd505b23867ec5 to your computer and use it in GitHub Desktop.
Save eshleebien/e5aa72fd505b23867ec5 to your computer and use it in GitHub Desktop.
Retrieve all rewrite rules in goto.tm .htaccess
import fileinput
import datetime
import pymongo
import time
import re
from pymongo import MongoClient
content = []
new_rules = []
search_string = ''
ts = time.time()
isodate = datetime.datetime.fromtimestamp(ts, None)
for line in fileinput.input():
# RewriteRule ^FreedomSteam?$ http://steamcommunity.com/groups/MCNFDM/ [NC,L,R=302]
if "RewriteRule" in line:
search_string = re.search('(\^(.+)(?=\$))(\$ )(.+(?= \[))', line.strip())
# FreedomSteam?$ http://steamcommunity.com/groups/MCNFDM/
if search_string:
short_url = re.sub('\W(?=\?|\n)', '', search_string.group(2) + "\n")
content.append([short_url.replace('', '')[:-1], search_string.group(4)])
else:
print(line.strip())
fileinput.close()
for rule in content:
new_rules.append({
"long_url": rule[1],
"short_url": rule[0],
"updated_at": isodate,
"created_at": isodate
})
print('Records to insert: ' + str(len(new_rules)))
client = MongoClient('localhost', 27017)
db = client.gototm
result = db.redirect_rules.insert_many(new_rules)
print('Records inserted: ' + str(db.redirect_rules.count()))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment