import sys
import csv

from sqlalchemy.exc import IntegrityError

from openledger.models import db, Image

filename = sys.argv[1]
fields = ('ImageID', 'Subset', 'OriginalURL', 'OriginalLandingURL', 'License',
          'AuthorProfileURL', 'Author', 'Title')

with open(filename) as csvfile:
    db.create_all()
    reader = csv.DictReader(csvfile)
    for row in reader:
        image = Image()
        image.google_imageid = row['ImageID']
        image.image_url = row['OriginalURL']
        image.original_landing_url = row['OriginalLandingURL']
        image.license_url = row['License']
        image.author_url = row['AuthorProfileURL']
        image.author = row['Author']
        image.title = row['Title']
        db.session.add(image)
        try:
            db.session.commit()
            print("Adding image ", row['ImageID'])
        except IntegrityError:
            db.session.rollback()

import csv
import argparse


from sqlalchemy.exc import IntegrityError

from openledger.models import db, Image

def import_from_open_images(filename):
    fields = ('ImageID', 'Subset', 'OriginalURL', 'OriginalLandingURL', 'License',
              'AuthorProfileURL', 'Author', 'Title')

    with open(filename) as csvfile:
        db.create_all()
        reader = csv.DictReader(csvfile)
        for row in reader:
            image = Image()
            image.google_imageid = row['ImageID']
            image.image_url = row['OriginalURL']
            image.original_landing_url = row['OriginalLandingURL']
            image.license_url = row['License']
            image.author_url = row['AuthorProfileURL']
            image.author = row['Author']
            image.title = row['Title']
            db.session.add(image)
            try:
                db.session.commit()
                print("Adding image ", row['ImageID'])
            except IntegrityError:
                db.session.rollback()

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument("--open-images-path",
                        dest="openimages_path",
                        help="The location of the Google Open Images csv file")
    parser.add_argument("--flickr-100m-path",
                        dest="flickr100m_path",
                        help="The location of the Flickr 100M tsv directory")
    args = parser.parse_args()
    if args.openimages_path:
        import_from_open_images(args.openimages_path)

Tidy up database import to take arguments for multiple sources
Tidy up database import to take arguments for multiple sources

- import sys
  import csv
+ import argparse
+ 
  
  from sqlalchemy.exc import IntegrityError
  
  from openledger.models import db, Image
  
- filename = sys.argv[1]
+ def import_from_open_images(filename):
- fields = ('ImageID', 'Subset', 'OriginalURL', 'OriginalLandingURL', 'License',
+     fields = ('ImageID', 'Subset', 'OriginalURL', 'OriginalLandingURL', 'License',
-           'AuthorProfileURL', 'Author', 'Title')
+               'AuthorProfileURL', 'Author', 'Title')
  
- with open(filename) as csvfile:
+     with open(filename) as csvfile:
-     db.create_all()
+         db.create_all()
-     reader = csv.DictReader(csvfile)
+         reader = csv.DictReader(csvfile)
-     for row in reader:
+         for row in reader:
-         image = Image()
+             image = Image()
-         image.google_imageid = row['ImageID']
+             image.google_imageid = row['ImageID']
-         image.image_url = row['OriginalURL']
+             image.image_url = row['OriginalURL']
-         image.original_landing_url = row['OriginalLandingURL']
+             image.original_landing_url = row['OriginalLandingURL']
-         image.license_url = row['License']
+             image.license_url = row['License']
-         image.author_url = row['AuthorProfileURL']
+             image.author_url = row['AuthorProfileURL']
-         image.author = row['Author']
+             image.author = row['Author']
-         image.title = row['Title']
+             image.title = row['Title']
-         db.session.add(image)
+             db.session.add(image)
-         try:
+             try:
-             db.session.commit()
+                 db.session.commit()
-             print("Adding image ", row['ImageID'])
+                 print("Adding image ", row['ImageID'])
-         except IntegrityError:
+             except IntegrityError:
-             db.session.rollback()
+                 db.session.rollback()
  
+ if __name__ == '__main__':
+     parser = argparse.ArgumentParser()
+     parser.add_argument("--open-images-path",
+                         dest="openimages_path",
+                         help="The location of the Google Open Images csv file")
+     parser.add_argument("--flickr-100m-path",
+                         dest="flickr100m_path",
+                         help="The location of the Flickr 100M tsv directory")
+     args = parser.parse_args()
+     if args.openimages_path:
+         import_from_open_images(args.openimages_path)
+ 
Tidy up database import to take arguments for multiple sources
## Code Before:
import sys
import csv

from sqlalchemy.exc import IntegrityError

from openledger.models import db, Image

filename = sys.argv[1]
fields = ('ImageID', 'Subset', 'OriginalURL', 'OriginalLandingURL', 'License',
          'AuthorProfileURL', 'Author', 'Title')

with open(filename) as csvfile:
    db.create_all()
    reader = csv.DictReader(csvfile)
    for row in reader:
        image = Image()
        image.google_imageid = row['ImageID']
        image.image_url = row['OriginalURL']
        image.original_landing_url = row['OriginalLandingURL']
        image.license_url = row['License']
        image.author_url = row['AuthorProfileURL']
        image.author = row['Author']
        image.title = row['Title']
        db.session.add(image)
        try:
            db.session.commit()
            print("Adding image ", row['ImageID'])
        except IntegrityError:
            db.session.rollback()

## Instruction:
Tidy up database import to take arguments for multiple sources
## Code After:
import csv
import argparse


from sqlalchemy.exc import IntegrityError

from openledger.models import db, Image

def import_from_open_images(filename):
    fields = ('ImageID', 'Subset', 'OriginalURL', 'OriginalLandingURL', 'License',
              'AuthorProfileURL', 'Author', 'Title')

    with open(filename) as csvfile:
        db.create_all()
        reader = csv.DictReader(csvfile)
        for row in reader:
            image = Image()
            image.google_imageid = row['ImageID']
            image.image_url = row['OriginalURL']
            image.original_landing_url = row['OriginalLandingURL']
            image.license_url = row['License']
            image.author_url = row['AuthorProfileURL']
            image.author = row['Author']
            image.title = row['Title']
            db.session.add(image)
            try:
                db.session.commit()
                print("Adding image ", row['ImageID'])
            except IntegrityError:
                db.session.rollback()

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument("--open-images-path",
                        dest="openimages_path",
                        help="The location of the Google Open Images csv file")
    parser.add_argument("--flickr-100m-path",
                        dest="flickr100m_path",
                        help="The location of the Flickr 100M tsv directory")
    args = parser.parse_args()
    if args.openimages_path:
        import_from_open_images(args.openimages_path)
