from django.db         import IntegrityError
from scrapy.exceptions import DropItem
from titlecase         import titlecase
from dateutil          import parser, relativedelta


class EventPipeline(object):
    def process_item(self, item, spider):
        item['titleRaw'] = item['title']
        item['title']    = titlecase(item['title'])

        if (item['end'] < item['start']):
            item['end'] = item['end'] + relativedelta.relativedelta(years =+ 1)

        try:
            item.save()
        except IntegrityError:
            raise DropItem('Duplicate: ' + item['venue'] + ' - ' + item['title'])
        return item

from django.db         import IntegrityError
from scrapy.exceptions import DropItem
from titlecase         import titlecase
from dateutil          import parser, relativedelta


class EventPipeline(object):
    def process_item(self, item, spider):
        item['titleRaw'] = item['title']
        item['title']    = titlecase(item['title'])

        if 'end' in item and 'start' in item:
            if (item['end'] < item['start']):
                item['end'] = item['end'] + relativedelta.relativedelta(years =+ 1)

        try:
            item.save()
        except IntegrityError:
            raise DropItem('Duplicate: ' + item['venue'] + ' - ' + item['title'])
        return item

Verify that event end and start keys exist before accessing.
Verify that event end and start keys exist before accessing.

  from django.db         import IntegrityError
  from scrapy.exceptions import DropItem
  from titlecase         import titlecase
  from dateutil          import parser, relativedelta
  
  
  class EventPipeline(object):
      def process_item(self, item, spider):
          item['titleRaw'] = item['title']
          item['title']    = titlecase(item['title'])
  
+         if 'end' in item and 'start' in item:
-         if (item['end'] < item['start']):
+             if (item['end'] < item['start']):
-             item['end'] = item['end'] + relativedelta.relativedelta(years =+ 1)
+                 item['end'] = item['end'] + relativedelta.relativedelta(years =+ 1)
  
          try:
              item.save()
          except IntegrityError:
              raise DropItem('Duplicate: ' + item['venue'] + ' - ' + item['title'])
          return item
  
Verify that event end and start keys exist before accessing.
## Code Before:
from django.db         import IntegrityError
from scrapy.exceptions import DropItem
from titlecase         import titlecase
from dateutil          import parser, relativedelta


class EventPipeline(object):
    def process_item(self, item, spider):
        item['titleRaw'] = item['title']
        item['title']    = titlecase(item['title'])

        if (item['end'] < item['start']):
            item['end'] = item['end'] + relativedelta.relativedelta(years =+ 1)

        try:
            item.save()
        except IntegrityError:
            raise DropItem('Duplicate: ' + item['venue'] + ' - ' + item['title'])
        return item

## Instruction:
Verify that event end and start keys exist before accessing.
## Code After:
from django.db         import IntegrityError
from scrapy.exceptions import DropItem
from titlecase         import titlecase
from dateutil          import parser, relativedelta


class EventPipeline(object):
    def process_item(self, item, spider):
        item['titleRaw'] = item['title']
        item['title']    = titlecase(item['title'])

        if 'end' in item and 'start' in item:
            if (item['end'] < item['start']):
                item['end'] = item['end'] + relativedelta.relativedelta(years =+ 1)

        try:
            item.save()
        except IntegrityError:
            raise DropItem('Duplicate: ' + item['venue'] + ' - ' + item['title'])
        return item
