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.

125 lines
3.5 KiB

5 months ago
5 months ago
4 months ago
5 months ago
4 months ago
5 months ago
4 months ago
5 months ago
4 months ago
5 months ago
4 months ago
5 months ago
4 months ago
5 months ago
4 months ago
5 months ago
4 months ago
5 months ago
  1. #!/usr/bin/python
  2. # -*- coding:utf-8 -*-
  3. # call this to sync any new bitmaps in SSB from our friends into our posts.json EBB DB
  4. import ConfigParser
  5. import json
  6. import re
  7. import subprocess
  8. import datetime
  9. import time
  10. import traceback
  11. import iterparse
  12. import uuid
  13. import addtoDB
  14. from tinydb import TinyDB, Query
  15. def main():
  16. nonspace = re.compile(r'\S')
  17. def fresh():
  18. print 'refreshing db from ssb'
  19. #get our ssb pub key and db location from config
  20. configParser = ConfigParser.RawConfigParser()
  21. configFilePath = r'config.txt'
  22. configParser.read(configFilePath)
  23. ssbme = configParser.get('ebb-config', 'ssbme')
  24. dbPath = configParser.get('ebb-config', 'dbPath')
  25. imgpath = configParser.get('ebb-config', 'imagesPath')
  26. db = TinyDB(dbPath)
  27. #get time of last db-to-ssb sync from db
  28. root_entry = db.get(doc_id=1)
  29. last_run = root_entry["date"]
  30. print 'looking for posts since: ', last_run
  31. #if last run is never ran then only look back 1 month (new installs don't need EVERY old post...)
  32. if last_run==4:
  33. whatever=time.time()
  34. whatever-=2700000
  35. last_run=int(whatever)
  36. newssb="nada"
  37. newssbcmd = 'ssb-server createLogStream --reverse 1 --gt ' + str(last_run)
  38. #get the latest posts from ssb
  39. try:
  40. proc=subprocess.Popen(newssbcmd, shell=True, stdout=subprocess.PIPE, )
  41. except:
  42. print 'traceback.format_exc():\n%s' % traceback.format_exc()
  43. exit()
  44. # get the ssb json from the bash command we just ran
  45. newssb=proc.stdout.read()
  46. # iterate through new ssb posts
  47. for decoded in iterparse.parsy(newssb):
  48. # get the timestamp
  49. timestamp = decoded['timestamp']
  50. # get the value part of the ssb post
  51. value = decoded['value']
  52. #get the content part within value...
  53. content = value['content']
  54. #check that type is post
  55. if(content['type'] == 'post'):
  56. #check that we are not the author
  57. if (value['author']!=ssbme):
  58. #gotta pull out the non blob shiz
  59. bigblobstring = content['text']
  60. halfblob = bigblobstring.split("(")
  61. theblob = halfblob[1].split(")")
  62. #so now theblob[0] should be THE blob
  63. dablob = theblob[0]
  64. print dablob, 'at', timestamp
  65. #check to make sure we haven't already added this one
  66. Fruit = Query()
  67. isitthere=db.search(Fruit.ssb == dablob)
  68. if (bool(isitthere)==False):
  69. #make sure we actually have the blob
  70. newssbcmd = 'ssb-server blobs.has "' + theblob[0] + '"'
  71. try:
  72. proc=subprocess.Popen(newssbcmd, shell=True, stdout=subprocess.PIPE, )
  73. except:
  74. print 'traceback.format_exc():\n%s' % traceback.format_exc()
  75. exit()
  76. #tru/false whether we have the blob:
  77. newssb=proc.stdout.read()
  78. print 'do we have the blob? ', newssb
  79. # if we have it, add it!
  80. if (str(newssb)=='true\n'):
  81. print "yep, we have the blob"
  82. #generate unique file_name
  83. unique = uuid.uuid4()
  84. unique = str(unique)
  85. bmpath = imgpath + unique + '.bmp'
  86. #get the blob out of .ssb/blobs and put into imgages folder
  87. newssbcmd = 'touch ' + bmpath + ' && sbot blobs.get "' + theblob[0] + '" >> ' + bmpath
  88. try:
  89. proc=subprocess.Popen(newssbcmd, shell=True, stdout=subprocess.PIPE, )
  90. except:
  91. print 'traceback.format_exc():\n%s' % traceback.format_exc()
  92. exit()
  93. #add to db
  94. addtoDB.addFile(bmpath,dbPath,dablob)
  95. #update last_run (so that we don't have to search through ALL ssb posts everytime, this keeps track of oldest
  96. Fruit = Query()
  97. if timestamp>last_run:
  98. db.update({'date': timestamp}, Fruit.ssb == 'default')
  99. if __name__ == '__main__':
  100. main()