Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dynamic Default Namespace in ActionParse #9

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions wikiraider/actions/ActionParse.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,16 @@ def run(self):
colorlog.getLogger().info('Iterating all of the XML files and pushing pages to queue. This might take a while...')
colorlog.getLogger().info('In the meantime the consumers are already processing the pages...')

namespaces = {'': None}
for xml_file in xml_files:
for event, element in xml.etree.cElementTree.iterparse(open(xml_file, 'r')):
if element.tag.startswith('{'):
# extract last used namespace dynamically and set it as default
namespaces[''] = element.tag.split('}')[0][1:]
if element.tag.endswith('page'):
title = element.find('.//{http://www.mediawiki.org/xml/export-0.10/}title')
revision = element.find('.//{http://www.mediawiki.org/xml/export-0.10/}revision')
text = revision.find('.//{http://www.mediawiki.org/xml/export-0.10/}text')
title = element.find('.//title', namespaces)
revision = element.find('.//revision', namespaces)
text = revision.find('.//text', namespaces)

element.clear()

Expand Down