|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +from __future__ import unicode_literals |
| 3 | + |
| 4 | +import debug # pyflakes:ignore |
| 5 | + |
| 6 | +import contextlib |
| 7 | +import os |
| 8 | +import urllib2 |
| 9 | + |
| 10 | +from bs4 import BeautifulSoup |
| 11 | +from tqdm import tqdm |
| 12 | + |
| 13 | +from django.db import migrations |
| 14 | +from django.conf import settings |
| 15 | + |
| 16 | +def get_filename(doc): |
| 17 | + path = settings.DOCUMENT_PATH_PATTERN.format(doc=doc) |
| 18 | + # ! These files right now are created with no version number? |
| 19 | + #name = '%s-%s.txt' % (doc.name,doc.rev) |
| 20 | + name = '%s.txt' % (doc.name,) |
| 21 | + return os.path.join(path,name) |
| 22 | + |
| 23 | +def forward(apps,schema_editor): |
| 24 | + # for each qualifying document |
| 25 | + Document = apps.get_model('doc','Document') |
| 26 | + |
| 27 | + for doc in tqdm(Document.objects.filter(type='review',external_url__contains="www.ietf.org/mail-archive/web"),desc="Pointers into Mhonarc"): |
| 28 | + filename = get_filename(doc) |
| 29 | + if not os.path.isfile(filename): |
| 30 | + with contextlib.closing(urllib2.urlopen(doc.external_url)) as infile: |
| 31 | + fullcontents = infile.read().decode('utf-8', 'ignore'); |
| 32 | + start = fullcontents.find('<!--X-Body-of-Message-->') |
| 33 | + end = fullcontents.find('<!--X-Body-of-Message-End-->') |
| 34 | + bodyblock=fullcontents[start+len('<!--X-Body-of-Message-->'):end] |
| 35 | + text = BeautifulSoup(bodyblock,"lxml").get_text('\n\n') \ |
| 36 | + .replace('FAQ at <\n\nhttp://wiki.tools','FAQ at <http://wiki.tools') \ |
| 37 | + .replace('wiki/GenArtfaq\n\n>','wiki/GenArtfaq>') |
| 38 | + with contextlib.closing(open(filename,'w')) as outfile: |
| 39 | + outfile.write(text.encode('utf8')) |
| 40 | + |
| 41 | + for doc in tqdm(Document.objects.filter(type='review',external_url__contains="mailarchive.ietf.org"),desc="Pointers into Mailarchive"): |
| 42 | + filename = get_filename(doc) |
| 43 | + if not os.path.isfile(filename): |
| 44 | + with contextlib.closing(urllib2.urlopen(doc.external_url)) as infile: |
| 45 | + fullcontents = infile.read().decode('utf-8', 'ignore'); |
| 46 | + soup = BeautifulSoup(fullcontents,"lxml") |
| 47 | + divpre = soup.find('div',{"id":"msg-payload"}).find('pre') |
| 48 | + text = divpre.get_text('\n\n') |
| 49 | + with contextlib.closing(open(filename,'w')) as outfile: |
| 50 | + outfile.write(text.encode('utf8')) |
| 51 | + |
| 52 | + ## After this migration, we should figure out what to do with these stragglers: |
| 53 | + ## In [29]: Document.objects.filter(type='review').exclude(Q(external_url__contains="mailarchive")|Q(external_url__contains="mail-archive")).values_list('external_url',flat=True) |
| 54 | + ## Out[29]: [u'https://art.tools.ietf.org/tools/art/genart/index.cgi/t=1909/review_edit?reviewid=2300', u'https://art.tools.ietf.org/tools/art/genart/index.cgi/t=8460/review_edit?reviewid=2735', u'https://www.ietf.org/ibin/c5i?mid=6&rid=49&gid=0&k1=933&k2=55337&tid=1296220835', u'https://www.ietf.org/mailman/private/tsv-dir/2012-February/002007.html', u'', u''] |
| 55 | + |
| 56 | +def reverse(apps,schema_editor): |
| 57 | + pass |
| 58 | + |
| 59 | +class Migration(migrations.Migration): |
| 60 | + |
| 61 | + dependencies = [ |
| 62 | + ('doc', '0016_auto_20160927_0713'), |
| 63 | + ] |
| 64 | + |
| 65 | + operations = [ |
| 66 | + migrations.RunPython(forward,reverse) |
| 67 | + ] |
0 commit comments