So i had all the raw text from my blog posts backed up in a flat text file (just a bunch of text).
I wanted to be clever and not manually enter each blog post again, so I made a new button and tied it to this grabPosts function:
class BlogPostGrabber():
def grabPosts(self):
os.chdir(‘/home/colin/blogposts’)
for blogPost in glob.glob(“*.blogpost”):
filename, ext = os.path.splitext(os.path.basename(blogPost))
subject = filename.replace(“_”,” “)
if “New post” in subject:
subject = None
body=None
with open(blogPost,’r’) as bp:
body = bp.read();
shutil.move(os.path.basename(blogPost),’/home/colin/blogposts/posted/’)
language = guessLanguage(body)
if language == ‘UNKNOWN’ or len(language) > 5:
language = ‘en’
blogpost = BlogPost(body = body,
subject = subject,
timestamp = datetime.utcnow(),
language = language)
db.session.add(blogpost)
db.session.commit()
return url_for(‘blog’)
Sweet. it will grab all .blogpost files, parse and import them into the database, when I hit that button.
Now I need to create all the .blogpost files from the .txt file!
#makeposts.py
import os,glob,shutil
def makeposts():
ts = None
sub = None
bpb = None
os.chdir(‘/home/colin/sandbocks’)
try:
os.mkdir(‘./madeposts’)
except:
pass
for blogPost in glob.glob(“*.txt”):
body=None
with open(blogPost,’r’) as bp:
body = bp.read();
blogposts=body.split(‘\n\n’)
i = 1
for bp in blogposts:
spl=bp.split(‘\n’)
if len(spl) == 2:
ts=spl[0]
sub=None
bpb=spl[1]
if len(spl) == 3:
ts=spl[0]
sub=spl[1]
bpb=spl[2]
if sub == None:
filename = “New_post_”+str(i)+”.blogpost”
else:
filename = sub.replace(‘ ‘,’_’)+”.blogpost”
with open(‘./madeposts/’+filename, “w”) as outbp:
outbp.write(‘Original Timestamp: ‘+str(ts)+’\n’)
outbp.write(str(bpb))
i = i + 1
if __name__ == ‘__main__’:
makeposts()
That’ll do ya! (Worked for my particular strucure/formatting… tailor to fit your needs.)