the public repo for the e-paper bulletin board. It's a bulletin board that syncs over scuttlebutt :)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

126 lines
3.5 KiB

6 months ago
#!/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
6 months ago
import traceback
import iterparse
import uuid
import addtoDB
from tinydb import TinyDB, Query
def main():
nonspace = re.compile(r'\S')
def fresh():
6 months ago
print 'refreshing db from ssb'
6 months ago
#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"]
6 months ago
print 'looking for posts since: ', last_run
6 months ago
#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()
6 months ago
whatever-=2700000
last_run=int(whatever)
6 months ago
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]
6 months ago
print dablob, 'at', timestamp
6 months ago
#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()
6 months ago
#tru/false whether we have the blob:
6 months ago
newssb=proc.stdout.read()
6 months ago
print 'do we have the blob? ', newssb
6 months ago
6 months ago
# if we have it, add it!
6 months ago
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()
6 months ago
if timestamp>last_run:
db.update({'date': timestamp}, Fruit.ssb == 'default')
6 months ago
if __name__ == '__main__':
main()