1. Blue
  2. django-spotnet

Commits

Blue  committed ee27e1e

Fixes for some errors I got just after committing the previous version.

  • Participants
  • Parent commits e1f4310
  • Branches default

Comments (0)

Files changed (2)

File spotnet/post.py

View file
 from UserDict import UserDict
 from dateutil.parser import parse as parse_datetime
 from HTMLParser import HTMLParser
-from xml.dom.minidom import parseString
+from xml.dom.minidom import parseString, Node, Text
 from datetime import datetime, timedelta
 from nzb import decode_nzb, DecodeNzbError
 
         line_start = '%s: ' % header
         ret = []
         for h in l:
+            if h[-1] == '\n': h = h[:-1]
+            assert '\n' not in h
             if h.startswith(line_start):
                 # this is a header line
                 ret.append(h[len(line_start):])
         except Exception as e:
             raise InvalidPostXml("Post has invalid XML data for header X-XML: %s" % e)
         doc = xml.documentElement
-        if not doc.tagName == 'Spotnet':
-            raise InvalidPost(
+        if not doc.tagName in ('Spotnet', 'SpotNet'):
+            raise InvalidPostXml(
                 "XML for spotnet post does not have a main "
                 "node called 'Spotnet'"
             )
-        if not len(doc.childNodes) == 1:
-            raise InvalidPost(
+        if not len(doc.childNodes) in (1, 2):
+            raise InvalidPostXml(
                 "XML for spotnet post does not have 1 child "
                 "for main node 'Spotnet'"
             )
         main = doc.childNodes[0]
         if not main.tagName == 'Posting':
-            raise InvalidPost(
+            raise InvalidPostXml(
                 "XML for spotnet post does not have a main "
                 "child node called 'Posting' for 'Spotnet'"
             )
+        if len(doc.childNodes) >= 2:
+            signature = doc.childNodes[1]
+            if not signature.tagName == 'Signature':
+                raise InvalidPostXml(
+                    "XML for spotnet post has a second child node "
+                    "not called 'Signature' for 'Spotnet'"
+                )
+            # TODO: use the signature
+        else:
+            signature = None
         # assemble dict of content
         d = {}
         for e in main.childNodes:
                 # the location, the result is similar (a string instead of a string list)
                 d['NZB'] = []
                 for nzb_node in e.childNodes:
-                    if not nzb_node.tagName == 'Segment':
-                        raise InvalidPost(
-                            "XML for spotnet post, in NZB node "
-                            "there are child nodes that are not named 'Segment'"
-                        )
-                    d['NZB'].append(nzb_node.childNodes[0].nodeValue)
+                    if nzb_node.nodeType in (3, 4):
+                        # this is a textnode or cdata section
+                        d['NZB'].append(nzb_node.nodeValue)
+                    elif nzb_node.nodeType in (1, ):
+                        # this is an element
+                        if not nzb_node.tagName == 'Segment':
+                            raise InvalidPostXml(
+                                "XML for spotnet post, in NZB node "
+                                "there are child nodes that are not named 'Segment'"
+                            )
+                        d['NZB'].append(nzb_node.childNodes[0].nodeValue)
+                    else:
+                        # don't know how to parse this...
+                        # we raise an error to signal when this ever occurs
+                        raise InvalidPostXml("Invalid child in NZB node")
         if isinstance(d.get('Category', None), list):
             if len(d['Category']) == 0:
                 d['Category'] = None
 
     @property
     def category(self):
-        return self.decode(self.extra.get('Category', None), None)
+        try:
+            return int(self.extra.get('Category', None))
+        except (TypeError, ValueError):
+            return None
 
     @property
     def subcategories(self):
             return None
 
     @property
+    def filename(self):
+        return self.decode(self.extra.get('Filename', None), None)
+
+    # TODO: what are these? :
+    # (found these in a post with X-XML root tag 'SpotNet')
+    # extra.get('FType', None)
+    # extra.get('FCount', None)
+    # extra.get('Key-ID', None)
+    # extra.get('Message-ID', None)
+    # extra.get('Header', None)
+
+    @property
     def nzb(self):
         if 'NZB' in self.extra:
             nzb_raw = self.extra['NZB']

File spotnet/tests/parsing.py

View file
             '      continuing here ',
             'X-XML: and all the way into this other header]]></Key></Posting></Spotnet>',
         ]))
-        self.assertEqual(raw.extra['Key'], u'the long valuueeee\n continuing here \nand all the way into this other header')
+        self.assertEqual(raw.extra['Key'], u'the long valuueeee continuing here and all the way into this other header')
 
     def test_parse_datetime(self):
         post = self.construct(dict(Date='11 Jun 2011 08:50:22 GMT'), '')