add attachment ability to archiver
This commit is contained in:
parent
35c6875b00
commit
ccc5972e67
@ -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,10 +150,11 @@ class Getter:
|
|||||||
except:
|
except:
|
||||||
pass
|
pass
|
||||||
else:
|
else:
|
||||||
for t in j['threads']:
|
if 'threads' in j:
|
||||||
posts = t['posts']
|
for t in j['threads']:
|
||||||
for post in posts:
|
posts = t['posts']
|
||||||
yield Article(post, self.board, self.site)
|
for post in posts:
|
||||||
|
yield Article(post, self.board, self.site)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user