126 lines
3.5 KiB
Python
126 lines
3.5 KiB
Python
#!/usr/bin/python
|
|
# -*- coding:utf-8 -*-
|
|
|
|
# call this to sync any new bitmaps in SSB from our friends into our posts.json EBB DB
|
|
|
|
import ConfigParser
|
|
import json
|
|
import re
|
|
import subprocess
|
|
import datetime
|
|
import time
|
|
import traceback
|
|
import iterparse
|
|
import uuid
|
|
import addtoDB
|
|
from tinydb import TinyDB, Query
|
|
|
|
|
|
def main():
|
|
nonspace = re.compile(r'\S')
|
|
|
|
|
|
def fresh():
|
|
print 'refreshing db from ssb'
|
|
#get our ssb pub key and db location from config
|
|
configParser = ConfigParser.RawConfigParser()
|
|
configFilePath = r'config.txt'
|
|
configParser.read(configFilePath)
|
|
ssbme = configParser.get('ebb-config', 'ssbme')
|
|
dbPath = configParser.get('ebb-config', 'dbPath')
|
|
imgpath = configParser.get('ebb-config', 'imagesPath')
|
|
db = TinyDB(dbPath)
|
|
|
|
#get time of last db-to-ssb sync from db
|
|
root_entry = db.get(doc_id=1)
|
|
last_run = root_entry["date"]
|
|
print 'looking for posts since: ', last_run
|
|
|
|
#if last run is never ran then only look back 1 month (new installs don't need EVERY old post...)
|
|
if last_run==4:
|
|
whatever=time.time()
|
|
whatever-=2700000
|
|
last_run=int(whatever)
|
|
|
|
newssb="nada"
|
|
newssbcmd = 'ssb-server createLogStream --reverse 1 --gt ' + str(last_run)
|
|
|
|
#get the latest posts from ssb
|
|
try:
|
|
proc=subprocess.Popen(newssbcmd, shell=True, stdout=subprocess.PIPE, )
|
|
except:
|
|
print 'traceback.format_exc():\n%s' % traceback.format_exc()
|
|
exit()
|
|
|
|
# get the ssb json from the bash command we just ran
|
|
newssb=proc.stdout.read()
|
|
|
|
|
|
# iterate through new ssb posts
|
|
for decoded in iterparse.parsy(newssb):
|
|
# get the timestamp
|
|
timestamp = decoded['timestamp']
|
|
# get the value part of the ssb post
|
|
value = decoded['value']
|
|
#get the content part within value...
|
|
content = value['content']
|
|
|
|
#check that type is post
|
|
if(content['type'] == 'post'):
|
|
#check that we are not the author
|
|
if (value['author']!=ssbme):
|
|
|
|
#gotta pull out the non blob shiz
|
|
bigblobstring = content['text']
|
|
halfblob = bigblobstring.split("(")
|
|
theblob = halfblob[1].split(")")
|
|
|
|
#so now theblob[0] should be THE blob
|
|
dablob = theblob[0]
|
|
print dablob, 'at', timestamp
|
|
|
|
#check to make sure we haven't already added this one
|
|
Fruit = Query()
|
|
isitthere=db.search(Fruit.ssb == dablob)
|
|
if (bool(isitthere)==False):
|
|
|
|
#make sure we actually have the blob
|
|
newssbcmd = 'ssb-server blobs.has "' + theblob[0] + '"'
|
|
try:
|
|
proc=subprocess.Popen(newssbcmd, shell=True, stdout=subprocess.PIPE, )
|
|
except:
|
|
print 'traceback.format_exc():\n%s' % traceback.format_exc()
|
|
exit()
|
|
#tru/false whether we have the blob:
|
|
newssb=proc.stdout.read()
|
|
print 'do we have the blob? ', newssb
|
|
|
|
# if we have it, add it!
|
|
if (str(newssb)=='true\n'):
|
|
print "yep, we have the blob"
|
|
#generate unique file_name
|
|
unique = uuid.uuid4()
|
|
unique = str(unique)
|
|
bmpath = imgpath + unique + '.bmp'
|
|
|
|
#get the blob out of .ssb/blobs and put into imgages folder
|
|
newssbcmd = 'touch ' + bmpath + ' && sbot blobs.get "' + theblob[0] + '" >> ' + bmpath
|
|
try:
|
|
proc=subprocess.Popen(newssbcmd, shell=True, stdout=subprocess.PIPE, )
|
|
except:
|
|
print 'traceback.format_exc():\n%s' % traceback.format_exc()
|
|
exit()
|
|
|
|
#add to db
|
|
addtoDB.addFile(bmpath,dbPath,dablob)
|
|
|
|
#update last_run (so that we don't have to search through ALL ssb posts everytime, this keeps track of oldest
|
|
Fruit = Query()
|
|
if timestamp>last_run:
|
|
db.update({'date': timestamp}, Fruit.ssb == 'default')
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
main()
|