2012-02-15 09:35:06 +00:00
|
|
|
import urllib2
|
|
|
|
import re
|
|
|
|
from texts.models import *
|
|
|
|
|
2012-02-26 00:28:22 +00:00
|
|
|
NEXT = "http://pad.ma/"
|
2012-02-15 09:35:06 +00:00
|
|
|
OLD = "http://pad.ma/"
|
|
|
|
IdMapping = {}
|
2012-02-15 12:32:38 +00:00
|
|
|
regex = re.compile(r'(http:\/\/.*?pad.ma\/(V.*?))[\s\"\<\n]')
|
|
|
|
regex2 = re.compile(r'(http:\/\/.*?pad.ma\/(V.*?))$')
|
2012-02-15 09:35:06 +00:00
|
|
|
def getNewUrl(oldUrl):
|
2012-02-15 12:32:38 +00:00
|
|
|
new = re.sub(regex2, lambda pat: NEXT + pat.groups()[1], oldUrl)
|
|
|
|
# print new
|
|
|
|
# new = oldUrl.replace(OLD, NEXT)
|
2012-02-15 09:35:06 +00:00
|
|
|
try:
|
|
|
|
return urllib2.urlopen(new).url
|
|
|
|
except:
|
|
|
|
print "ERROR: " + oldUrl
|
2012-02-15 12:32:38 +00:00
|
|
|
return None
|
2012-02-15 09:35:06 +00:00
|
|
|
|
|
|
|
def getNewId(oldId):
|
|
|
|
oldId = oldId.group().replace(OLD, "")[:-1]
|
|
|
|
print oldId
|
|
|
|
if oldId in IdMapping:
|
|
|
|
return IdMapping[oldId]
|
|
|
|
url = NEXT + oldId
|
|
|
|
u = urllib2.urlopen(url)
|
|
|
|
newId = u.url.replace(NEXT, "")
|
|
|
|
IdMapping[oldId] = newId
|
|
|
|
return newId
|
|
|
|
|
|
|
|
def replaceUrls(text):
|
2012-02-15 12:32:38 +00:00
|
|
|
|
2012-02-15 09:35:06 +00:00
|
|
|
for f in re.findall(regex, text):
|
2012-02-15 12:32:38 +00:00
|
|
|
newUrl = getNewUrl(f[0])
|
|
|
|
if newUrl is None:
|
|
|
|
continue
|
|
|
|
# print newUrl
|
|
|
|
text = text.replace(f[0], newUrl)
|
2012-02-15 09:35:06 +00:00
|
|
|
return text
|
|
|
|
# for m in re.findall(regex, text):
|
|
|
|
# print getNewId(m)
|
|
|
|
|
|
|
|
def doAll():
|
|
|
|
for p in PadmaText.objects.all():
|
|
|
|
print p.title
|
|
|
|
newText = replaceUrls(p.html)
|
|
|
|
p.html = newText
|
|
|
|
p.save()
|
|
|
|
|
2012-02-26 00:28:22 +00:00
|
|
|
def doAllSide():
|
|
|
|
for p in PadmaText.objects.all():
|
|
|
|
print p.title
|
|
|
|
newLeftBar = replaceUrls(p.left_bar)
|
|
|
|
p.left_bar = newLeftBar
|
|
|
|
p.save()
|
2012-02-25 12:33:00 +00:00
|
|
|
|
|
|
|
def nextToCurrent(text):
|
|
|
|
return text.replace(NEXT, OLD)
|
|
|
|
|
|
|
|
def nextAll():
|
|
|
|
for p in PadmaText.objects.all():
|
|
|
|
print p.title
|
|
|
|
p.html = nextToCurrent(p.html)
|
|
|
|
p.save()
|
|
|
|
|