def create(name, parent_category_name, binary): """Create the NZB, store it in GridFS and return the ID to be linked to the release.""" xml = io.StringIO() xml.write( '<?xml version="1.0" encoding="UTF-8"?>\n' '<!DOCTYPE nzb PUBLIC "-//newzBin//DTD NZB 1.1//EN" "http://www.newzbin.com/DTD/nzb/nzb-1.1.dtd">\n' '<nzb>\n' '<head><meta type="category">{}</meta><meta type="name">{}</meta></head>\n' .format(parent_category_name, escape(name))) for part in binary.parts: timestamp = calendar.timegm( part.posted.replace(tzinfo=pytz.utc).utctimetuple()) xml.write('<file poster={} date="{}" subject={}>\n<groups>'.format( quoteattr(binary.posted_by), timestamp, quoteattr('{0} (1/{1:d})'.format(part.subject, part.total_segments)))) for group in pynab.binaries.parse_xref(binary.xref): xml.write('<group>{}</group>\n'.format(group)) xml.write('</groups>\n<segments>\n') for segment in part.segments: xml.write('<segment bytes="{}" number="{}">{}</segment>\n'.format( segment.size, segment.segment, escape(segment.message_id))) xml.write('</segments>\n</file>\n') xml.write('</nzb>') nzb = NZB() nzb.data = gzip.compress(xml.getvalue().encode('utf-8')) return nzb
def create(name, parent_category_name, binary): """Create the NZB, store it in GridFS and return the ID to be linked to the release.""" xml = io.StringIO() xml.write('<?xml version="1.0" encoding="UTF-8"?>\n' '<!DOCTYPE nzb PUBLIC "-//newzBin//DTD NZB 1.1//EN" "http://www.newzbin.com/DTD/nzb/nzb-1.1.dtd">\n' '<nzb>\n' '<head><meta type="category">{}</meta><meta type="name">{}</meta></head>\n'.format(parent_category_name, escape(name)) ) for part in binary.parts: timestamp = calendar.timegm(part.posted.replace(tzinfo=pytz.utc).utctimetuple()) xml.write('<file poster={} date="{}" subject={}>\n<groups>'.format( quoteattr(binary.posted_by), timestamp, quoteattr('{0} (1/{1:d})'.format(part.subject, part.total_segments)) )) for group in pynab.binaries.parse_xref(binary.xref): xml.write('<group>{}</group>\n'.format(group)) xml.write('</groups>\n<segments>\n') for segment in part.segments: xml.write('<segment bytes="{}" number="{}">{}</segment>\n'.format( segment.size, segment.segment, escape(segment.message_id) )) xml.write('</segments>\n</file>\n') xml.write('</nzb>') nzb = NZB() nzb.data = gzip.compress(xml.getvalue().encode('utf-8')) return nzb
def import_nzb(name, nzb_data): """Import an NZB and directly load it into releases.""" release = {'added': pytz.utc.localize(datetime.datetime.now()), 'size': None, 'spotnab_id': None, 'completion': None, 'grabs': 0, 'passworded': None, 'file_count': None, 'tvrage': None, 'tvdb': None, 'imdb': None, 'nfo': None, 'tv': None, 'total_parts': 0} try: for event, elem in cet.iterparse(io.StringIO(nzb_data)): if 'meta' in elem.tag: release[elem.attrib['type']] = elem.text if 'file' in elem.tag: release['total_parts'] += 1 release['posted'] = elem.get('date') release['posted_by'] = elem.get('poster') if 'group' in elem.tag and 'groups' not in elem.tag: release['group_name'] = elem.text except Exception as e: log.error('nzb: error parsing NZB files: file appears to be corrupt.') return False if 'name' not in release: log.error('nzb: failed to import nzb: {0}'.format(name)) return False # check that it doesn't exist first with db_session() as db: r = db.query(Release).filter(Release.name == release['name']).first() if not r: r = Release() r.name = release['name'] r.search_name = release['name'] r.posted = release['posted'] r.posted_by = release['posted_by'] if 'posted' in release: r.posted = datetime.datetime.fromtimestamp(int(release['posted']), pytz.utc) else: r.posted = None if 'category' in release: parent, child = release['category'].split(' > ') category = db.query(Category).filter(Category.name == parent).filter(Category.name == child).first() if category: r.category = category else: r.category = None else: r.category = None # make sure the release belongs to a group we have in our db if 'group_name' in release: group = db.query(Group).filter(Group.name == release['group_name']).first() if not group: group = Group(name=release['group_name']) db.add(group) r.group = group # rebuild the nzb, gzipped nzb = NZB() nzb.data = gzip.compress(nzb_data.encode('utf-8')) r.nzb = nzb db.merge(r) return True else: log.error('nzb: release already exists: {0}'.format(release['name'])) return False
def import_nzb(name, nzb_data): """Import an NZB and directly load it into releases.""" release = { 'added': pytz.utc.localize(datetime.datetime.now()), 'size': None, 'spotnab_id': None, 'completion': None, 'grabs': 0, 'passworded': None, 'file_count': None, 'tvrage': None, 'tvdb': None, 'imdb': None, 'nfo': None, 'tv': None, 'total_parts': 0 } try: for event, elem in cet.iterparse(io.StringIO(nzb_data)): if 'meta' in elem.tag: release[elem.attrib['type']] = elem.text if 'file' in elem.tag: release['total_parts'] += 1 release['posted'] = elem.get('date') release['posted_by'] = elem.get('poster') if 'group' in elem.tag and 'groups' not in elem.tag: release['group_name'] = elem.text except Exception as e: log.error('nzb: error parsing NZB files: file appears to be corrupt.') return False if 'name' not in release: log.error('nzb: failed to import nzb: {0}'.format(name)) return False # check that it doesn't exist first with db_session() as db: r = db.query(Release).filter(Release.name == release['name']).first() if not r: r = Release() r.name = release['name'] r.search_name = release['name'] r.posted = release['posted'] r.posted_by = release['posted_by'] if 'posted' in release: r.posted = datetime.datetime.fromtimestamp( int(release['posted']), pytz.utc) else: r.posted = None if 'category' in release: parent, child = release['category'].split(' > ') category = db.query(Category).filter( Category.name == parent).filter( Category.name == child).first() if category: r.category = category else: r.category = None else: r.category = None # make sure the release belongs to a group we have in our db if 'group_name' in release: group = db.query(Group).filter( Group.name == release['group_name']).first() if not group: group = Group(name=release['group_name']) db.add(group) r.group = group # rebuild the nzb, gzipped nzb = NZB() nzb.data = gzip.compress(nzb_data.encode('utf-8')) r.nzb = nzb db.merge(r) return True else: log.error('nzb: release already exists: {0}'.format( release['name'])) return False