# Path: kraut_sharing/models.py
# class OSINT_Feed(models.Model):
#     creation_time = models.DateTimeField(auto_now_add=True)
#     name = models.CharField(max_length=255, help_text="""Name/Identifier""")
#     url = models.CharField(max_length=2014, help_text="""URL for feed""")
#     last_pull = models.DateTimeField(blank=True, null=True)
#     namespace = models.CharField(max_length=255, default='nospace')
#     source = models.CharField(max_length=255, default='unknown')
# 
#     def __unicode__(self):
#         return u"%s" % (self.name)
# 
# Path: kraut_parser/models.py
# class Indicator(models.Model):
#     name = models.CharField(max_length=255)
#     creation_time = models.DateTimeField(auto_now_add=True)
#     last_modified = models.DateTimeField(auto_now=True)
#     description = models.TextField(null=True, blank=True)
#     short_description = models.CharField(max_length=255, null=True, blank=True)
#     indicator_id = models.CharField(max_length=255)
#     namespace = models.ManyToManyField('Namespace', blank=True)
#     indicator_types = models.ManyToManyField(Indicator_Type, blank=True)
#     confidence = models.ManyToManyField(Confidence, blank=True)
#     related_indicators = models.ManyToManyField('self', blank=True)
#     indicator_composition_operator = models.CharField(max_length=3, default="OR")
#     ttps = models.ManyToManyField('TTP', blank=True)
#     produced_time = models.DateTimeField(null=True, blank=True)
#     kill_chain_phases = models.ManyToManyField(Indicator_Kill_Chain_Phase, blank=True)
# 
#     def __unicode__(self):
#         return u"%s" % (self.name)
# 
# class Package(models.Model):
#     name = models.CharField(max_length=255)
#     creation_time = models.DateTimeField(auto_now_add=True)
#     last_modified = models.DateTimeField(auto_now=True)
#     description = models.TextField(null=True, blank=True)
#     short_description = models.CharField(max_length=255, null=True, blank=True)
#     namespace = models.ManyToManyField('Namespace', blank=True)
#     version = models.CharField(max_length=255)
#     package_id = models.CharField(max_length=255)
#     source = models.CharField(max_length=255)
#     produced_time = models.DateTimeField(null=True, blank=True)
#     threat_actors = models.ManyToManyField('ThreatActor', blank=True)
#     campaigns = models.ManyToManyField('Campaign', blank=True)
#     indicators = models.ManyToManyField('Indicator', blank=True)
#     observables = models.ManyToManyField('Observable', blank=True)
#     ttps = models.ManyToManyField('TTP', blank=True)
# 
#     def __unicode__(self):
#         return u"%s" % (self.name)
# 
# class Namespace(models.Model):
#     namespace = models.CharField(max_length=255, default='nospace', unique=True)
#     description = models.TextField(null=True, blank=True)
# 
#     def __unicode__(self):
#         return u"%s" % (self.namespace)
# 
#     class Meta:
#         ordering = ['namespace']
# 
# class Indicator_Type(models.Model):
#     itype = models.CharField(max_length=255, unique=True)
# 
#     def __unicode__(self):
#         return u"%s" % (self.itype)
# 
# class Indicator_Kill_Chain_Phase(models.Model):
#     name = models.CharField(max_length=255, unique=True)
#     ordinality = models.IntegerField(null=True, blank=True)
# 
#     def __unicode__(self):
#         return u"%s" % (self.name)
# 
# class Confidence(models.Model):
#     value = models.CharField(max_length=255)
#     description = models.TextField(null=True, blank=True)
# 
#     def __unicode__(self):
#         return u"%s" % (self.value)
# 
# class Address_Object(models.Model):
#     address_value = models.CharField(max_length=255)
#     category = models.CharField(max_length=255, default="ipv4-addr")
#     condition = models.CharField(max_length=255, default="equals")
#     observables = models.ManyToManyField(Observable)
# 
#     def __unicode__(self):
#         if self.address_value:
#             return u"%s" % (self.address_value)
#         else:
#             return u"Address Object"
# 
#     class Meta:
#         unique_together = (("address_value", "category", "condition"),)
# 
# class Observable(models.Model):
#     name = models.CharField(max_length=255)
#     creation_time = models.DateTimeField(auto_now_add=True)
#     last_modified = models.DateTimeField(auto_now=True)
#     description = models.TextField(null=True, blank=True)
#     short_description = models.CharField(max_length=255, null=True, blank=True)
#     namespace = models.ManyToManyField('Namespace', blank=True)
#     indicators = models.ManyToManyField(Indicator, blank=True)
#     compositions = models.ManyToManyField(ObservableComposition, blank=True)
#     observable_type = models.CharField(max_length=255, null=True, blank=True)
#     observable_id = models.CharField(max_length=255, unique=True)
# 
#     def __unicode__(self):
#         return u"%s" % (self.name)


import requests, csv, uuid, re, hashlib
from dateutil.parser import parse
from django.core.management.base import BaseCommand, CommandError
from django.conf import settings
from django.db.models import Count
from django.utils import timezone
from kraut_sharing.models import OSINT_Feed
from kraut_parser.models import Indicator, Package, Namespace, Indicator_Type, Indicator_Kill_Chain_Phase, Confidence, Address_Object, Observable
# vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4




# import database models

class Command(BaseCommand):
    def __init__(self, *args, **kwargs):
        super(Command, self).__init__(*args, **kwargs)


# Path: kraut_sharing/models.py
# class OSINT_Feed(models.Model):
#     creation_time = models.DateTimeField(auto_now_add=True)
#     name = models.CharField(max_length=255, help_text="""Name/Identifier""")
#     url = models.CharField(max_length=2014, help_text="""URL for feed""")
#     last_pull = models.DateTimeField(blank=True, null=True)
#     namespace = models.CharField(max_length=255, default='nospace')
#     source = models.CharField(max_length=255, default='unknown')
# 
#     def __unicode__(self):
#         return u"%s" % (self.name)
# 
# Path: kraut_parser/models.py
# class Indicator(models.Model):
#     name = models.CharField(max_length=255)
#     creation_time = models.DateTimeField(auto_now_add=True)
#     last_modified = models.DateTimeField(auto_now=True)
#     description = models.TextField(null=True, blank=True)
#     short_description = models.CharField(max_length=255, null=True, blank=True)
#     indicator_id = models.CharField(max_length=255)
#     namespace = models.ManyToManyField('Namespace', blank=True)
#     indicator_types = models.ManyToManyField(Indicator_Type, blank=True)
#     confidence = models.ManyToManyField(Confidence, blank=True)
#     related_indicators = models.ManyToManyField('self', blank=True)
#     indicator_composition_operator = models.CharField(max_length=3, default="OR")
#     ttps = models.ManyToManyField('TTP', blank=True)
#     produced_time = models.DateTimeField(null=True, blank=True)
#     kill_chain_phases = models.ManyToManyField(Indicator_Kill_Chain_Phase, blank=True)
# 
#     def __unicode__(self):
#         return u"%s" % (self.name)
# 
# class Package(models.Model):
#     name = models.CharField(max_length=255)
#     creation_time = models.DateTimeField(auto_now_add=True)
#     last_modified = models.DateTimeField(auto_now=True)
#     description = models.TextField(null=True, blank=True)
#     short_description = models.CharField(max_length=255, null=True, blank=True)
#     namespace = models.ManyToManyField('Namespace', blank=True)
#     version = models.CharField(max_length=255)
#     package_id = models.CharField(max_length=255)
#     source = models.CharField(max_length=255)
#     produced_time = models.DateTimeField(null=True, blank=True)
#     threat_actors = models.ManyToManyField('ThreatActor', blank=True)
#     campaigns = models.ManyToManyField('Campaign', blank=True)
#     indicators = models.ManyToManyField('Indicator', blank=True)
#     observables = models.ManyToManyField('Observable', blank=True)
#     ttps = models.ManyToManyField('TTP', blank=True)
# 
#     def __unicode__(self):
#         return u"%s" % (self.name)
# 
# class Namespace(models.Model):
#     namespace = models.CharField(max_length=255, default='nospace', unique=True)
#     description = models.TextField(null=True, blank=True)
# 
#     def __unicode__(self):
#         return u"%s" % (self.namespace)
# 
#     class Meta:
#         ordering = ['namespace']
# 
# class Indicator_Type(models.Model):
#     itype = models.CharField(max_length=255, unique=True)
# 
#     def __unicode__(self):
#         return u"%s" % (self.itype)
# 
# class Indicator_Kill_Chain_Phase(models.Model):
#     name = models.CharField(max_length=255, unique=True)
#     ordinality = models.IntegerField(null=True, blank=True)
# 
#     def __unicode__(self):
#         return u"%s" % (self.name)
# 
# class Confidence(models.Model):
#     value = models.CharField(max_length=255)
#     description = models.TextField(null=True, blank=True)
# 
#     def __unicode__(self):
#         return u"%s" % (self.value)
# 
# class Address_Object(models.Model):
#     address_value = models.CharField(max_length=255)
#     category = models.CharField(max_length=255, default="ipv4-addr")
#     condition = models.CharField(max_length=255, default="equals")
#     observables = models.ManyToManyField(Observable)
# 
#     def __unicode__(self):
#         if self.address_value:
#             return u"%s" % (self.address_value)
#         else:
#             return u"Address Object"
# 
#     class Meta:
#         unique_together = (("address_value", "category", "condition"),)
# 
# class Observable(models.Model):
#     name = models.CharField(max_length=255)
#     creation_time = models.DateTimeField(auto_now_add=True)
#     last_modified = models.DateTimeField(auto_now=True)
#     description = models.TextField(null=True, blank=True)
#     short_description = models.CharField(max_length=255, null=True, blank=True)
#     namespace = models.ManyToManyField('Namespace', blank=True)
#     indicators = models.ManyToManyField(Indicator, blank=True)
#     compositions = models.ManyToManyField(ObservableComposition, blank=True)
#     observable_type = models.CharField(max_length=255, null=True, blank=True)
#     observable_id = models.CharField(max_length=255, unique=True)
# 
#     def __unicode__(self):
#         return u"%s" % (self.name)


# Path: kraut_sharing/management/commands/pull_osint.py
import requests, csv, uuid, re, hashlib
from dateutil.parser import parse
from django.core.management.base import BaseCommand, CommandError
from django.conf import settings
from django.db.models import Count
from django.utils import timezone
from kraut_sharing.models import OSINT_Feed
from kraut_parser.models import Indicator, Package, Namespace, Indicator_Type, Indicator_Kill_Chain_Phase, Confidence, Address_Object, Observable

# vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4




# import database models

class Command(BaseCommand):
    def __init__(self, *args, **kwargs):
        super(Command, self).__init__(*args, **kwargs)


    def create_package(self, ptitle, description, short_description, source, namespace, ptime, version="1.1.1"):