Archived
1
0

add attachment ability to archiver

This commit is contained in:
no 2016-01-06 16:17:34 -05:00
parent 35c6875b00
commit ccc5972e67

View File

@ -8,6 +8,8 @@ import io
import nntplib import nntplib
import time import time
import urllib.parse import urllib.parse
import base64
import random
class Article: class Article:
""" """
@ -25,7 +27,6 @@ class Article:
self.board = board self.board = board
self.site = site self.site = site
self.messageID = self.genMessageID(j['no']) self.messageID = self.genMessageID(j['no'])
self.attachments = list()
def formatDate(self): def formatDate(self):
return datetime.datetime.utcfromtimestamp(self.j['time']).strftime(self.timeFormat) return datetime.datetime.utcfromtimestamp(self.j['time']).strftime(self.timeFormat)
@ -62,6 +63,9 @@ class Article:
"Path: {}\n").format(self.subject(), self.name(), self.formatDate(), self.group(), self.messageID, self.site) "Path: {}\n").format(self.subject(), self.name(), self.formatDate(), self.group(), self.messageID, self.site)
if self.j['resto'] > 0: if self.j['resto'] > 0:
hdr += "References: {}\n".format(self.genMessageID(self.j['resto'])) hdr += "References: {}\n".format(self.genMessageID(self.j['resto']))
if 'filename' in self.j:
hdr += 'Mime-Version: 1.0\n'
hdr += 'Content-Type: multipart/mixed; boundary="{}"\n'.format(self.boundary)
return hdr return hdr
def bodyPlain(self): def bodyPlain(self):
@ -70,10 +74,32 @@ class Article:
return "{}\n{}".format(self.header(), msg) return "{}\n{}".format(self.header(), msg)
def bodyMultipart(self): def bodyMultipart(self):
pass self.boundary = '========{}'.format(random.randint(0, 10000000))
msg = self.header() + '\n'
msg += '--{}\n'.format(self.boundary)
msg += 'Content-Type: text/plain; encoding=UTF-8\n'
msg += '\n'
msg += self.message() + '\n'
msg += '--{}\n'.format(self.boundary)
msg += 'Content-Type: image/{}\n'.format(self.j['ext'])
msg += 'Content-Disposition: form-data; filename="{}{}"; name="import"\n'.format(self.j['filename'], self.j['ext'])
msg += 'Content-Transfer-Encoding: base64\n'
msg += '\n'
url = 'https://{}/{}/src/{}{}'.format(self.site, self.board, self.j['tim'], self.j['ext'])
print ('obtain {}'.format(url))
r = requests.get(url)
if r.status_code == 200:
msg += base64.b64encode(r.content).decode('ascii')
msg += '\n'
else:
print ('failed to obtain attachment: {} != 200'.format(r.status_code))
return
msg += '\n--{}--\n'.format(self.boundary)
return msg
def body(self): def body(self):
if len(self.attachments) > 0: if 'filename' in self.j:
return self.bodyMultipart() return self.bodyMultipart()
else: else:
return self.bodyPlain() return self.bodyPlain()
@ -124,6 +150,7 @@ class Getter:
except: except:
pass pass
else: else:
if 'threads' in j:
for t in j['threads']: for t in j['threads']:
posts = t['posts'] posts = t['posts']
for post in posts: for post in posts: