Skip to content

Commit

Permalink
feat(geeklist): add support for comments
Browse files Browse the repository at this point in the history
  • Loading branch information
arnauldvm committed Feb 6, 2019
1 parent 5495325 commit 1a236ae
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 5 deletions.
24 changes: 21 additions & 3 deletions boardgamegeek/loaders/geeklist.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,23 @@ def parse_date(str_date):
# example: Sat, 02 Feb 2019 15:13:54 +0000


def add_geeklist_comments_from_xml(geeklist_or_item, xml_root):
added_comments = False
for comment in xml_root.findall("comment"):
# initial data for this collection item
data = {
"username": comment.attrib["username"],
"date": parse_date(comment.attrib["date"]) or None,
"postdate": parse_date(comment.attrib["postdate"]) or None,
"editdate": parse_date(comment.attrib["editdate"]) or None,
"thumbs": int(comment.attrib["thumbs"]),
"text": comment.text.strip()
}
listcomment = geeklist_or_item.add_comment(data)
added_comments = True
return added_comments


def create_geeklist_from_xml(xml_root, listid):
data = {
'id': listid,
Expand All @@ -18,9 +35,10 @@ def create_geeklist_from_xml(xml_root, listid):
'numitems': xml_subelement_text(xml_root, 'numitems', int),
'username': xml_subelement_text(xml_root, 'username'),
'description': xml_subelement_text(xml_root, 'description')
# "comments": ...
}
return GeekList(data)
list = GeekList(data)
add_geeklist_comments_from_xml(list, xml_root)
return list

def add_geeklist_items_from_xml(geeklist, xml_root):
added_items = False
Expand All @@ -33,7 +51,6 @@ def add_geeklist_items_from_xml(geeklist, xml_root):
"editdate": parse_date(item.attrib["editdate"]) or None,
"thumbs": int(item.attrib["thumbs"]),
"body": xml_subelement_text(item, "body")
# "comments": ...
}
listitem = geeklist.add_item(data)
object_data = {
Expand All @@ -44,5 +61,6 @@ def add_geeklist_items_from_xml(geeklist, xml_root):
"subtype": item.attrib["subtype"]
}
listitem.set_object(object_data)
add_geeklist_comments_from_xml(listitem, xml_root)
added_items = True
return added_items
79 changes: 77 additions & 2 deletions boardgamegeek/objects/geeklist.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,51 @@
from ..exceptions import BGGError
from .things import Thing

class GeekListComment(DictObject):
"""
Object containing details about a comment in a geeklist
"""
def __init__(self, data):
super(GeekListComment, self).__init__(data)

def __repr__(self):
return "GeekListComment (on {} by [{}])".format(self.date, self.username)

def _format(self, log):
log.info("date : {}".format(self.date))
log.info(" username : {}".format(self.username))
log.info(" postdate : {}".format(self.postdate))
log.info(" editdate : {}".format(self.editdate))
log.info(" thumbs count : {}".format(self.thumbs))
log.info(" text : {}".format(self.text))


class GeekList(Thing):
"""
Object containing information about a geeklist
"""
def __init__(self, data):
self._comments = []
self._items = []
super(GeekList, self).__init__(data)

def __repr__(self):
return "GeekList (id: {})".format(self.id)

def add_comment(self, comment_data):
"""
Add a comment to the ``GeekList``
:param dict comment_data: comment data
:raises: :py:class:`boardgamegeek.exceptions.BoardGameGeekError` in case of invalid data
"""
try:
comment = GeekListComment(comment_data)
except KeyError:
raise BGGError("invalid item data")
self._comments.append(comment)
return comment

def add_item(self, item_data):
"""
Add an item to the ``GeekList``
Expand All @@ -27,6 +61,16 @@ def add_item(self, item_data):
self._items.append(item)
return item

@property
def comments(self):
"""
Returns the comments in the collection
:returns: the comments in the geeklist
:rtype: list of :py:class:`boardgamegeek.games.CollectionBoardGame`
"""
return self._comments

@property
def items(self):
"""
Expand All @@ -49,7 +93,10 @@ def _format(self, log):
log.info("geeklist thumbs count : {}".format(self.thumbs))
log.info("geeklist numitems : {}".format(self.numitems))
log.info("geeklist description : {}".format(self.description))
# "comments": ...
log.info("comments")
for c in self.comments:
c._format(log)
log.info("")
log.info("items")
for i in self:
i._format(log)
Expand All @@ -66,6 +113,7 @@ class GeekListItem(DictObject):
Object containing information about a geeklist item
"""
def __init__(self, data):
self._comments = []
super(GeekListItem, self).__init__(data)

def __repr__(self):
Expand All @@ -84,6 +132,30 @@ def set_object(self, object_data):
raise BGGError("invalid object data")
return self._object

def add_comment(self, comment_data):
"""
Add a comment to the ``GeekList``
:param dict comment_data: comment data
:raises: :py:class:`boardgamegeek.exceptions.BoardGameGeekError` in case of invalid data
"""
try:
comment = GeekListComment(comment_data)
except KeyError:
raise BGGError("invalid item data")
self._comments.append(comment)
return comment

@property
def comments(self):
"""
Returns the comments in the collection
:returns: the comments in the geeklist
:rtype: list of :py:class:`boardgamegeek.games.CollectionBoardGame`
"""
return self._comments

def _format(self, log):
log.info("id : {}".format(self.id))
log.info("username : {}".format(self.username))
Expand All @@ -93,7 +165,10 @@ def _format(self, log):
log.info("edited at : {}".format(self.editdate))
log.info("thumbs count : {}".format(self.thumbs))
log.info("body (description) : {}".format(self.body))
# "comments": ...
log.info("comments")
for c in self.comments:
c._format(log)
log.info("")

@property
def object(self):
Expand Down

0 comments on commit 1a236ae

Please sign in to comment.