# Path: shiori/bookmark/models.py
# class Bookmark(BaseObject):
#     """ Bookmark model """
#     url = models.URLField()
#     title = models.CharField(max_length=255)
#     category = models.ForeignKey(Category)
#     tags = models.ManyToManyField(Tag, through='BookmarkTag')
#     registered_datetime = models.DateTimeField(auto_now=True,
#                                                auto_now_add=True)
#     description = models.TextField(blank=True)
#     owner = models.ForeignKey(User)
#     is_hide = models.BooleanField(default=False)
# 
#     class Meta(object):
#         """ meta class """
#         db_table = 'bookmark'
#         unique_together = ('url', 'owner')
# 
#     def __unicode__(self):
#         return self.title
# 
#     def get_absolute_url(self):
#         """ bookmark permalink path """
#         return "/shiori/b/%s" % self.id
# 
# class Category(BaseObject):
#     """ Category model """
#     category = models.CharField(max_length=255, unique=True)
# 
#     class Meta(object):
#         """ meta class """
#         db_table = 'category'
# 
#     def __unicode__(self):
#         return self.category
# 
# class FeedSubscription(BaseObject):
#     """ FeedSubsscription model """
#     url = models.URLField()
#     name = models.CharField(max_length=255, editable=False)
#     owner = models.ForeignKey(User)
#     default_category = models.ForeignKey(Category)
# 
#     class Meta(object):
#         """ meta class """
#         db_table = 'feed_subscription'
#         unique_together = ('url', 'owner')
# 
#     def __unicode__(self):
#         return self.url
# 
# class CrawlingHistory(BaseObject):
#     """ Crawling History model """
#     feed = models.ForeignKey(FeedSubscription)
#     result = jsonfield.JSONField()
#     update_datetime = models.DateTimeField(auto_now=True,
#                                            auto_now_add=True)
# 
#     class Meta(object):
#         """ meta class """
#         db_table = 'crawling_history'
# 
#     def __unicode__(self):
#         return self.id
# 
# Path: shiori/bookmark/agents/feed_parser.py
# class FeedParser(object):
#     """ FeedParser class """
# 
#     def __init__(self, url):
#         try:
#             response = requests.get(url, stream=True)
#         except requests.ConnectionError as error:
#             print(error)
#             sys.exit(1)
#         try:
#             etree = lxml.fromstring(response.content)
#         except XMLSyntaxError as error:
#             print(error)
#             sys.exit(1)
#         self.nsmap = etree.nsmap
#         self.namespace = get_ns(etree.nsmap, None)
# 
#         self.updated = get_updated(etree, self.nsmap)
#         self.title = get_title(etree, self.namespace)
#         self.items = get_items(etree, self.namespace)
#         self.etree = etree
# 
#     def retrieve_items(self):
#         """ retrieve feed items """
#         return [dict(title=get_title(item, self.namespace),
#                      link=get_link(item, self.namespace),
#                      updated=get_updated(item, self.nsmap))
#                 for item in self.items]


from django.contrib.auth.models import User
from django.db import IntegrityError
from shiori.bookmark.models import (Bookmark,
                                    Category,
                                    FeedSubscription,
                                    CrawlingHistory)
from shiori.bookmark.agents.feed_parser import FeedParser
import json
        url: feed entry url
        category: feed entry category
        owner: feed subscriber user
    Return:
        List of Feeds data
    """
    result = []
    for entry in FeedParser(kwargs.get('url')).retrieve_items():
        return_code, msg = add_item(url=entry.get('link'),
                                    title=entry.get('title'),
                                    category=kwargs.get('category'),
                                    owner=kwargs.get('owner'))
        if return_code is False and "already registered:" in msg:
            break
        result.append(dict(link=entry.get('link'),
                           rc=return_code,
                           msg=msg))
    return result


def add_item(**kwargs):
    """ Adding bookmark.
    Arguments:
        url: feed entry url
        title: feed entry title
        category: feed etnry category
        owner: feed subscriber user
    Return:
        (Bool, error message)
    """

# Path: shiori/bookmark/models.py
# class Bookmark(BaseObject):
#     """ Bookmark model """
#     url = models.URLField()
#     title = models.CharField(max_length=255)
#     category = models.ForeignKey(Category)
#     tags = models.ManyToManyField(Tag, through='BookmarkTag')
#     registered_datetime = models.DateTimeField(auto_now=True,
#                                                auto_now_add=True)
#     description = models.TextField(blank=True)
#     owner = models.ForeignKey(User)
#     is_hide = models.BooleanField(default=False)
# 
#     class Meta(object):
#         """ meta class """
#         db_table = 'bookmark'
#         unique_together = ('url', 'owner')
# 
#     def __unicode__(self):
#         return self.title
# 
#     def get_absolute_url(self):
#         """ bookmark permalink path """
#         return "/shiori/b/%s" % self.id
# 
# class Category(BaseObject):
#     """ Category model """
#     category = models.CharField(max_length=255, unique=True)
# 
#     class Meta(object):
#         """ meta class """
#         db_table = 'category'
# 
#     def __unicode__(self):
#         return self.category
# 
# class FeedSubscription(BaseObject):
#     """ FeedSubsscription model """
#     url = models.URLField()
#     name = models.CharField(max_length=255, editable=False)
#     owner = models.ForeignKey(User)
#     default_category = models.ForeignKey(Category)
# 
#     class Meta(object):
#         """ meta class """
#         db_table = 'feed_subscription'
#         unique_together = ('url', 'owner')
# 
#     def __unicode__(self):
#         return self.url
# 
# class CrawlingHistory(BaseObject):
#     """ Crawling History model """
#     feed = models.ForeignKey(FeedSubscription)
#     result = jsonfield.JSONField()
#     update_datetime = models.DateTimeField(auto_now=True,
#                                            auto_now_add=True)
# 
#     class Meta(object):
#         """ meta class """
#         db_table = 'crawling_history'
# 
#     def __unicode__(self):
#         return self.id
# 
# Path: shiori/bookmark/agents/feed_parser.py
# class FeedParser(object):
#     """ FeedParser class """
# 
#     def __init__(self, url):
#         try:
#             response = requests.get(url, stream=True)
#         except requests.ConnectionError as error:
#             print(error)
#             sys.exit(1)
#         try:
#             etree = lxml.fromstring(response.content)
#         except XMLSyntaxError as error:
#             print(error)
#             sys.exit(1)
#         self.nsmap = etree.nsmap
#         self.namespace = get_ns(etree.nsmap, None)
# 
#         self.updated = get_updated(etree, self.nsmap)
#         self.title = get_title(etree, self.namespace)
#         self.items = get_items(etree, self.namespace)
#         self.etree = etree
# 
#     def retrieve_items(self):
#         """ retrieve feed items """
#         return [dict(title=get_title(item, self.namespace),
#                      link=get_link(item, self.namespace),
#                      updated=get_updated(item, self.nsmap))
#                 for item in self.items]


# Path: shiori/bookmark/agents/feed.py
from django.contrib.auth.models import User
from django.db import IntegrityError
from shiori.bookmark.models import (Bookmark,
                                    Category,
                                    FeedSubscription,
                                    CrawlingHistory)
from shiori.bookmark.agents.feed_parser import FeedParser
import json

        url: feed entry url
        category: feed entry category
        owner: feed subscriber user
    Return:
        List of Feeds data
    """
    result = []
    for entry in FeedParser(kwargs.get('url')).retrieve_items():
        return_code, msg = add_item(url=entry.get('link'),
                                    title=entry.get('title'),
                                    category=kwargs.get('category'),
                                    owner=kwargs.get('owner'))
        if return_code is False and "already registered:" in msg:
            break
        result.append(dict(link=entry.get('link'),
                           rc=return_code,
                           msg=msg))
    return result


def add_item(**kwargs):
    """ Adding bookmark.
    Arguments:
        url: feed entry url
        title: feed entry title
        category: feed etnry category
        owner: feed subscriber user
    Return:
        (Bool, error message)
    """

    category = Category.objects.get(category=kwargs.get('category'))