from __future__ import print_function

import sys, os
from re import sub
import zipfile, json

# from pyspark import SparkContext
# from pyspark.sql import SQLContext
# from pyspark.sql import Row
# from pyspark.sql.types import StringType

def getSeries(fname):
    with zipfile.ZipFile(fname, 'r') as zf:
        names = zf.namelist()
        mfile = [f for f in names if f.endswith('.metadata.json')]
        series = fname
        if len(mfile) > 0:
            m = json.loads(zf.read(mfile[0]))
            series = m['identifier'][0]
            return m

if __name__ == "__main__":
    if len(sys.argv) < 2:
        print("Usage: europeana.py <input> <output>", file=sys.stderr)
        exit(-1)
    # sc = SparkContext(appName="Europeana Import")
    # sqlContext = SQLContext(sc)

    x = [os.path.join(d[0], f) for d in os.walk(sys.argv[1]) for f in d[2] if f.endswith('zip')]
    for f in x:
        print(json.dumps(getSeries(f)))
        
    # sc.parallelize(x, 200).flatMap(getSeries).toDF().write.save(sys.argv[2])

    # sc.stop()

@@ -414,21 +414,154 @@
 eries = 
-fname
+'europeana/' + sub('%5E.*newspapers-by-country/', '',%0A                                    sub('%5B%5Cx80-%5Cxff%5D', '', fname).replace('.zip', ''))
 %0A       
@@ -642,56 +642,80 @@
     
-series = m%5B'identifier'%5D%5B0%5D%0A            return m
+return %7B'series': series, 'title': m%5B'title'%5D%5B0%5D, 'lang': m%5B'language'%5D%7D
 %0A%0Aif

Use file path as Europeana series name.