Skip to content

Instantly share code, notes, and snippets.

@kevinpostal
Created May 8, 2017 02:12
Show Gist options
  • Save kevinpostal/f3880b78cc4a8d7bc7c41c93622b32db to your computer and use it in GitHub Desktop.
Save kevinpostal/f3880b78cc4a8d7bc7c41c93622b32db to your computer and use it in GitHub Desktop.
JsonImporter.py
import json
import logging
import os
import os.path
import sys
import django
import requests
from optparse import OptionParser
parser = OptionParser()
parser.add_option("-f", "--file", help="json file location")
(options, args) = parser.parse_args()
if len(sys.argv) == 1:
parser.print_help()
sys.exit(1)
# Logging Setup
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s %(name)s %(levelname)s %(message)s")
logger = logging.getLogger(__name__)
JSON_FILE = options.file
# Django Setup
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "MediaStore.settings")
django.setup()
from media_vault.models import MediaItem
class JsonDataParse:
""" Import Json sample json file into Django Models """
def __init__(self):
# Check for Json File
if not os.path.isfile(JSON_FILE):
raise Exception("Unable to open Json File")
def __send_null_filelocation(self, asset_id):
url = "http://www.test.com/new_alert"
data = {"asset_id": asset_id}
requests.post(url, data=json.dumps(data))
logger.info("File not found - asset_id: %s" % asset_id)
# Could add error handling for URL post handling.
def start(self):
data = {}
# Open and parse Json file
with open(JSON_FILE) as file_data:
self.json_data = json.load(file_data)
# Only grab "Core" componententitytype
self.items = [
item
for item in self.json_data.get("experiencecomponent", {}).get(
"items") if item.get("componententitytype") == "Core"
]
# List of asset id's
asset_id_list = [item.get("id") for item in self.items]
# Remove All Assets from DB
logger.info("Removing Following MediaItem objects: %s" %
", ".join(str(x) for x in asset_id_list))
MediaItem.objects.filter(asset_id__in=asset_id_list).delete()
for item in self.items:
data["asset_id"] = item.get("id")
# Check for version
if item.get("version"):
data["title"] = item.get("version")
else:
data["title"] = item.get("componenttype")
data["type"] = item.get("componenttype")
file_path = item.get("filelocation")
# Check for filepath. Send post if not found.
if file_path:
data["filepath"] = file_path.get("items",
[{}])[0].get("filepath")
if not data["filepath"]:
# Check for empty Filepath
self.__send_null_filelocation(data["asset_id"])
else:
self.__send_null_filelocation(data["asset_id"])
data["territory"] = None # **NOT SURE ABOUT TERRITORY VAUE
obj = MediaItem.objects.create(**data)
logger.info("%s Created" % obj)
if __name__ == "__main__":
parse = JsonDataParse()
parse.start()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment