import glob
paths = glob.glob('/public/dumps/public/nlwiki/20160801/nlwiki-20160801-pages-meta-history*.xml.bz2')
paths
import glob
paths = glob.glob('/public/dumps/public/dewiki/20160901/dewiki-20160901-pages-meta-history*.xml-*.bz2')
paths

import mwxml
import re

EXTS = ["png", "gif", "jpg", "jpeg"]
# [[(file|image):<file>.<ext>]]
IMAGE_LINK_RE = re.compile(r"\[\[" + 
                           r"(file|image|datei|bild):" +  # Group 1
                           r"([^\]]+.(" + "|".join(EXTS) + r"))" +  # Group 2 & 3
                           r"(|[^\]]+)?" +  # Group 4
                           r"\]\]")

def extract_image_links(text):
  for m in IMAGE_LINK_RE.finditer(text):
    yield m.group(2)
    
def process_dump(dump, path):
  for page in dump:
    last_count = 0
    for revision in page:
      image_links = list(extract_image_links(revision.text or ""))
      delta = len(image_links) - last_count
      if delta != 0:
        yield revision.page.id, revision.page.namespace, revision.user.id if revision.user else 0, revision.id, revision.timestamp, delta
      last_count = len(image_links)
count = 0
f = open('data_de_full.txt','w')
for rev_page, page_namespace, rev_user, rev_id, rev_timestamp, delta in mwxml.map(process_dump, paths):
    #print("\t".join(str(v) for v in [rev_page, page_namespace, rev_user, rev_id, rev_timestamp.strftime('%d-%m-%Y'), delta]))
    f.write(",".join(str(v) for v in [rev_page, page_namespace, rev_user, rev_id, rev_timestamp.strftime('%d-%m-%Y'), delta, '\n']))
    #f.write('test')
    count += 1
    if False:
        break
f.close()
import glob
paths = glob.glob('/public/dumps/public/dewiki/20160901/dewiki-20160901-pages-meta-history*.xml-*.bz2')
paths
['/public/dumps/public/dewiki/20160901/dewiki-20160901-pages-meta-history4.xml-p004964502p005397066.bz2',
 '/public/dumps/public/dewiki/20160901/dewiki-20160901-pages-meta-history4.xml-p008042286p009043989.bz2',
 '/public/dumps/public/dewiki/20160901/dewiki-20160901-pages-meta-history3.xml-p003822171p004358274.bz2',
 '/public/dumps/public/dewiki/20160901/dewiki-20160901-pages-meta-history1.xml-p000000001p000008196.bz2',
 '/public/dumps/public/dewiki/20160901/dewiki-20160901-pages-meta-history3.xml-p003211964p003822169.bz2',
 '/public/dumps/public/dewiki/20160901/dewiki-20160901-pages-meta-history1.xml-p000008197p000021172.bz2',
 '/public/dumps/public/dewiki/20160901/dewiki-20160901-pages-meta-history1.xml-p000055572p000118657.bz2',
 '/public/dumps/public/dewiki/20160901/dewiki-20160901-pages-meta-history4.xml-p005875665p006359556.bz2',
 '/public/dumps/public/dewiki/20160901/dewiki-20160901-pages-meta-history2.xml-p000610687p000794601.bz2',
 '/public/dumps/public/dewiki/20160901/dewiki-20160901-pages-meta-history4.xml-p007165977p008042283.bz2',
 '/public/dumps/public/dewiki/20160901/dewiki-20160901-pages-meta-history1.xml-p000400461p000425449.bz2',
 '/public/dumps/public/dewiki/20160901/dewiki-20160901-pages-meta-history2.xml-p000794602p001069677.bz2',
 '/public/dumps/public/dewiki/20160901/dewiki-20160901-pages-meta-history1.xml-p000021173p000055571.bz2',
 '/public/dumps/public/dewiki/20160901/dewiki-20160901-pages-meta-history2.xml-p000425451p000610686.bz2',
 '/public/dumps/public/dewiki/20160901/dewiki-20160901-pages-meta-history1.xml-p000118658p000218045.bz2',
 '/public/dumps/public/dewiki/20160901/dewiki-20160901-pages-meta-history3.xml-p002737370p003211957.bz2',
 '/public/dumps/public/dewiki/20160901/dewiki-20160901-pages-meta-history1.xml-p000218046p000400460.bz2',
 '/public/dumps/public/dewiki/20160901/dewiki-20160901-pages-meta-history4.xml-p009043990p009572983.bz2',
 '/public/dumps/public/dewiki/20160901/dewiki-20160901-pages-meta-history3.xml-p004358276p004964500.bz2',
 '/public/dumps/public/dewiki/20160901/dewiki-20160901-pages-meta-history2.xml-p001411674p001833566.bz2',
 '/public/dumps/public/dewiki/20160901/dewiki-20160901-pages-meta-history2.xml-p001833569p001877043.bz2',
 '/public/dumps/public/dewiki/20160901/dewiki-20160901-pages-meta-history4.xml-p006770728p007165976.bz2',
 '/public/dumps/public/dewiki/20160901/dewiki-20160901-pages-meta-history3.xml-p001877047p002312677.bz2',
 '/public/dumps/public/dewiki/20160901/dewiki-20160901-pages-meta-history3.xml-p002312678p002737368.bz2',
 '/public/dumps/public/dewiki/20160901/dewiki-20160901-pages-meta-history4.xml-p006359557p006770727.bz2',
 '/public/dumps/public/dewiki/20160901/dewiki-20160901-pages-meta-history2.xml-p001069678p001411671.bz2',
 '/public/dumps/public/dewiki/20160901/dewiki-20160901-pages-meta-history4.xml-p005397068p005875664.bz2']