# Path: wikipedia/models.py
# class BulkImport(models.Model):
#     slug = models.SlugField(max_length=255)
#     created_at = models.DateTimeField(auto_now_add=True)
#     text = models.TextField()
# 
#     def __unicode__(self):
#         return str(self.created_at)
# 
# class ImportedEndorsement(models.Model):
#     bulk_import = models.ForeignKey(BulkImport)
#     raw_text = models.TextField()
#     imported_endorser = models.ForeignKey(
#         ImportedEndorser, blank=True, null=True
#     )
#     confirmed_endorser = models.ForeignKey(Endorser, blank=True, null=True)
#     sections = models.TextField(blank=True, null=True)
#     notes = models.TextField(blank=True, null=True)
# 
#     def parse_text(self):
#         return utils.parse_wiki_text(self.raw_text)
# 
#     def get_likely_endorser(self):
#         name = self.parse_text()['endorser_name']
#         if not name:
#             return
# 
#         name = name.lower()
# 
#         query = Endorser.objects.filter(
#             name__iexact=name,
#         )
#         if query.exists():
#             return query.latest('pk')
# 
#         # See if putting "The" in front (or removing it) helps.
#         if name.startswith('the '):
#             query = Endorser.objects.filter(
#                 name__iexact=name[4:],
#             )
#             if query.exists():
#                 return query.first()
#         else:
#             query = Endorser.objects.filter(
#                 name__iexact='the ' + name,
#             )
#             if query.exists():
#                 return query.first()
# 
#         # See if the last name and part of the first name match.
#         if ' ' in name:
#             split_name = name.split(' ')
#             first_name_start = split_name[0][:3]
#             last_name = split_name[-1]
#             if len(last_name) > 3:
#                 query = Endorser.objects.filter(
#                     name__iendswith=last_name,
#                     name__istartswith=first_name_start,
#                 )
#                 if query.exists():
#                     return query.first()
# 
#     def __unicode__(self):
#         return self.raw_text
# 
# Path: wikipedia/utils.py
# def get_ref_definitions(line):
#     for ref_match in NAMED_REF_REGEX.finditer(line):
#         yield (
#             ref_match.group('name1') or ref_match.group('name2'),
#             ref_match.group()
#         )
# 
# def replace_refs(line, definitions):
#     for ref_match in REF_BY_NAME_REGEX.finditer(line):
#         name = ref_match.group('name1') or ref_match.group('name2')
#         definition = definitions.get(name)
#         if definition:
#             line = line.replace(ref_match.group(0), definition)
# 
#     return line
# 
# ANY_REF_REGEX = re.compile(r'(<ref[^>]*(?!/)>.*?</ref>|<ref[^/]*?/>)')
# 
# def split_endorsements(line):
#     for match in SPLIT_REGEX.finditer(line):
#         yield match.group(0).strip()


import re
import requests
from django.core.management.base import BaseCommand, CommandError
from wikipedia.models import BulkImport, ImportedEndorsement
from wikipedia.utils import get_ref_definitions, replace_refs, ANY_REF_REGEX, \
                            split_endorsements




COMMENTS_REGEX = re.compile(r'<!--.*?-->')
COLON_SECTION_REGEX = re.compile('(\[?\[?[a-zA-Z ]+)\]?\]?: ')

URL = 'https://en.wikipedia.org/w/api.php?action=parse&page={slug}&prop=wikitext&format=json'
NAMED_LINKS_REGEX = re.compile(r'\[\[[^|\]]+\|([^\]]+)\]\]')
class Command(BaseCommand):
    help = 'Bulk import all the endorsements from a Wikipedia page'

    def add_arguments(self, parser):
        parser.add_argument('slug')
        parser.add_argument(
            '--create',
            action='store_true',
            dest='create',
            default=False,
            help="Creates everything (otherwise, it's a dry run)",
        )

    def import_endorsement(self, raw_text, sections):
        # Make sure that named references are replaced.
        raw_text = replace_refs(raw_text, self.reference_definitions)
        sections_string = ' > '.join(sections)
        if raw_text:
            try:

# Path: wikipedia/models.py
# class BulkImport(models.Model):
#     slug = models.SlugField(max_length=255)
#     created_at = models.DateTimeField(auto_now_add=True)
#     text = models.TextField()
# 
#     def __unicode__(self):
#         return str(self.created_at)
# 
# class ImportedEndorsement(models.Model):
#     bulk_import = models.ForeignKey(BulkImport)
#     raw_text = models.TextField()
#     imported_endorser = models.ForeignKey(
#         ImportedEndorser, blank=True, null=True
#     )
#     confirmed_endorser = models.ForeignKey(Endorser, blank=True, null=True)
#     sections = models.TextField(blank=True, null=True)
#     notes = models.TextField(blank=True, null=True)
# 
#     def parse_text(self):
#         return utils.parse_wiki_text(self.raw_text)
# 
#     def get_likely_endorser(self):
#         name = self.parse_text()['endorser_name']
#         if not name:
#             return
# 
#         name = name.lower()
# 
#         query = Endorser.objects.filter(
#             name__iexact=name,
#         )
#         if query.exists():
#             return query.latest('pk')
# 
#         # See if putting "The" in front (or removing it) helps.
#         if name.startswith('the '):
#             query = Endorser.objects.filter(
#                 name__iexact=name[4:],
#             )
#             if query.exists():
#                 return query.first()
#         else:
#             query = Endorser.objects.filter(
#                 name__iexact='the ' + name,
#             )
#             if query.exists():
#                 return query.first()
# 
#         # See if the last name and part of the first name match.
#         if ' ' in name:
#             split_name = name.split(' ')
#             first_name_start = split_name[0][:3]
#             last_name = split_name[-1]
#             if len(last_name) > 3:
#                 query = Endorser.objects.filter(
#                     name__iendswith=last_name,
#                     name__istartswith=first_name_start,
#                 )
#                 if query.exists():
#                     return query.first()
# 
#     def __unicode__(self):
#         return self.raw_text
# 
# Path: wikipedia/utils.py
# def get_ref_definitions(line):
#     for ref_match in NAMED_REF_REGEX.finditer(line):
#         yield (
#             ref_match.group('name1') or ref_match.group('name2'),
#             ref_match.group()
#         )
# 
# def replace_refs(line, definitions):
#     for ref_match in REF_BY_NAME_REGEX.finditer(line):
#         name = ref_match.group('name1') or ref_match.group('name2')
#         definition = definitions.get(name)
#         if definition:
#             line = line.replace(ref_match.group(0), definition)
# 
#     return line
# 
# ANY_REF_REGEX = re.compile(r'(<ref[^>]*(?!/)>.*?</ref>|<ref[^/]*?/>)')
# 
# def split_endorsements(line):
#     for match in SPLIT_REGEX.finditer(line):
#         yield match.group(0).strip()


# Path: wikipedia/management/commands/importendorsements.py
import re
import requests
from django.core.management.base import BaseCommand, CommandError
from wikipedia.models import BulkImport, ImportedEndorsement
from wikipedia.utils import get_ref_definitions, replace_refs, ANY_REF_REGEX, \
                            split_endorsements





COMMENTS_REGEX = re.compile(r'<!--.*?-->')
COLON_SECTION_REGEX = re.compile('(\[?\[?[a-zA-Z ]+)\]?\]?: ')

URL = 'https://en.wikipedia.org/w/api.php?action=parse&page={slug}&prop=wikitext&format=json'
NAMED_LINKS_REGEX = re.compile(r'\[\[[^|\]]+\|([^\]]+)\]\]')
class Command(BaseCommand):
    help = 'Bulk import all the endorsements from a Wikipedia page'

    def add_arguments(self, parser):
        parser.add_argument('slug')
        parser.add_argument(
            '--create',
            action='store_true',
            dest='create',
            default=False,
            help="Creates everything (otherwise, it's a dry run)",
        )

    def import_endorsement(self, raw_text, sections):
        # Make sure that named references are replaced.
        raw_text = replace_refs(raw_text, self.reference_definitions)
        sections_string = ' > '.join(sections)
        if raw_text:
            try:

                ImportedEndorsement.objects.get(