-
Notifications
You must be signed in to change notification settings - Fork 2
/
fleet.py
1234 lines (1156 loc) · 40.8 KB
/
fleet.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from flask import Flask
from flask import send_file, render_template_string, render_template, flash, redirect, url_for, request, make_response, send_from_directory, session
from flask_cors import CORS, cross_origin
import os, sys, json, time, mimetypes, feedparser
from datetime import datetime, timedelta
from html.parser import HTMLParser
import urllib.request
#from flask_socketio import SocketIO
import random, string, tempfile
import sqlite3, re, hashlib
from pytz import timezone
from PIL import Image, ExifTags
from sendgrid import SendGridAPIClient
from sendgrid.helpers.mail import Mail
from random import randint
from twilio.rest import Client
from shutil import copyfile
from lxml import etree
from werkzeug.serving import WSGIRequestHandler
from dateutil import parser
from flask_talisman import Talisman
from flask_seasurf import SeaSurf
from pydub import AudioSegment
import cv2
from email import utils
import html
import subprocess
import html2text
import requests
import tensorflow as tf
import tensorflow_hub as hub
import pandas as pd
from socket import timeout
from extruct.opengraph import OpenGraphExtractor
import bcrypt
import csv
import socket
import ssl
from io import BytesIO
from gensim.models import Word2Vec
import dataclasses
import nltk
from nltk.corpus import wordnet
from itertools import chain
from math import ceil
appdir = os.getcwd() + '/'
cities = []
tf.get_logger().setLevel('ERROR')
h2t = html2text.HTML2Text()
twilio_id = os.getenv('TWILIO_ID')
twilio_token = os.getenv('TWILIO_TOKEN')
sendgrid_token = os.getenv('SENDGRID_TOKEN')
app = Flask(__name__, static_url_path=appdir)
app.secret_key = os.getenv('APP_SECRET_KEY')
app.permanent_session_lifetime = timedelta(days=31)
#csrf = SeaSurf()
#csrf.init_app(app)
talisman = Talisman(
app,
content_security_policy={
'default-src': [
'\'self\'',
'\'unsafe-inline\'',
'\'unsafe-eval\'',
'data:',
'blob:',
'https://www.youtube.com/embed/90kS-WljHX0',
],
'font-src': [
'*',
'data:',
],
'img-src': [
'*',
'data:',
],
'media-src': [
'*',
'data:',
],
}
)
#socketio = SocketIO(app)
#socketio.init_app(app)
myapp = appdir + os.getenv('FLEET_APP')
mydomain = os.getenv('APP_HOSTNAME')
UPLOAD_FOLDER = appdir + 'myfiles/'
ALLOWED_EXTENSIONS = set(['xml','opml','txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif', 'mov', 'mp4', 'mp3'])
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
@app.route('/file/<filename>')
def sendstatic(filename):
return send_from_directory('public', filename)
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
@app.route('/post/<postid>')
def post(postid):
p = get_one_by( 'posts', postid, 'key' )
if not len(p) > 0:
return 'OK'
urlsrc = 'https://' + mydomain + '/p/' + postid
#ht = '<!DOCTYPE html>' + "\n"
#ht = ht + '<html>' + "\n"
#ht = ht + ' <head>' + "\n"
#ht = ht + ' <title>' + grps[0]['name'] + '</title>'
ht = ''
title = re.sub('<[^<]+?>', '', p[0]['title'])
ht = ht + ' <meta property="og:title" content="' + title + '" />' + "\n"
ht = ht + ' <meta property="og:type" content="website" />'
if not p[0]['image'] is None:
ht = ht + ' <meta property="og:image:type" content="image/jpeg" />'
imgsrc = 'https://' + mydomain + '/myFile?field=image&name=' + p[0]['image']
ht = ht + ' <meta property="og:image" content="' + imgsrc + '" />' + "\n"
ht = ht + ' <meta property="og:url" content="' + urlsrc + '" />'
ht = ht + ' <meta property="og:description" content="' + title + '" />'
ht = ht + ' </head>' + "\n"
#ht = ht + ' <body>Group Page</body>' + "\n"
#ht = ht + '</html>' + "\n"
fleet = open(myapp).read()
fleet = fleet.replace('rp.ly',mydomain)
fleet = fleet.replace('</head>',ht)
response = make_response(fleet,200)
return response
@app.route('/p/<postid>')
def postuser(postid):
p = get_one_by( 'posts', postid, 'key' )
if not len(p) > 0:
return 'OK'
urlsrc = 'https://' + mydomain + '/p/' + postid
ht = ''
title = re.sub('<[^<]+?>', '', p[0]['title'])
ht = ht + ' <meta property="og:title" content="' + title + '" />' + "\n"
ht = ht + ' <meta property="og:type" content="website" />'
if not p[0]['image'] is None:
ht = ht + ' <meta property="og:image:type" content="image/jpeg" />'
imgsrc = 'https://' + mydomain + '/myFile?field=image&name=' + p[0]['image']
ht = ht + ' <meta property="og:image" content="' + imgsrc + '" />' + "\n"
ht = ht + ' <meta property="og:url" content="' + urlsrc + '" />'
ht = ht + ' <meta property="og:description" content="' + title + '" />'
ht = ht + ' </head>' + "\n"
#ht = ht + ' <body>Group Page</body>' + "\n"
#ht = ht + '</html>' + "\n"
fleet = open(myapp).read()
fleet = fleet.replace('rp.ly',mydomain)
fleet = fleet.replace('</head>',ht)
response = make_response(fleet,200)
return response
@app.route('/grp/<grpid>')
def group(grpid):
grps = get_one_by( 'groups', grpid, 'key' )
if not len(grps) > 0:
return 'OK'
urlsrc = 'https://' + mydomain + '/g/' + grpid
#ht = '<!DOCTYPE html>' + "\n"
#ht = ht + '<html>' + "\n"
#ht = ht + ' <head>' + "\n"
#ht = ht + ' <title>' + grps[0]['name'] + '</title>'
ht = ''
ht = ht + ' <meta property="og:title" content="' + grps[0]['name'] + '" />' + "\n"
ht = ht + ' <meta property="og:type" content="website" />'
if not grps[0]['image'] is None:
ht = ht + ' <meta property="og:image:type" content="image/jpeg" />'
imgsrc = 'https://' + mydomain + '/grpFile?field=image&name=' + grps[0]['image']
ht = ht + ' <meta property="og:image" content="' + imgsrc + '" />' + "\n"
ht = ht + ' <meta property="og:url" content="' + urlsrc + '" />'
ht = ht + ' <meta property="og:description" content="' + grps[0]['name'] + '" />'
ht = ht + ' </head>' + "\n"
#ht = ht + ' <body>Group Page</body>' + "\n"
#ht = ht + '</html>' + "\n"
fleet = open(myapp).read()
fleet = fleet.replace('rp.ly',mydomain)
fleet = fleet.replace('</head>',ht)
response = make_response(fleet,200)
return response
#ht = ht + ' <meta name="description" content="' + grps[0]['name'] + '" />'
#ht = ht + ' <link rel="shortcut icon" href="' + imgsrc + '" type="image/x-icon" />'
@app.route('/', defaults={'path': ''})
@app.route('/<path:path>')
def catch_all(path):
context = ssl._create_unverified_context()
grps = get_one_by( 'groups', path, 'publicPath' )
if (len(grps) > 0):
if path in cities:
#mod_one( 'groups', {'feeds':json.dumps(result)}, grps[0]['id'] )
#return 'OK!nice '
#return 'OK'
#if path == 'nyssao':
# del_one('groups',grps[0]['id'])
# print(' DEL CITY')
# return 'OK'
print('CITY')
if 'key' in grps[0]:
urlsrc = 'https://' + mydomain + '/grp/' + grps[0]['key']
return redirect(urlsrc)
else:
return 'OK'
else:
if path in cities:
srch = 'https://www.googleapis.com/customsearch/v1?' + urllib.parse.urlencode({
'key':os.getenv('GOO_KEY'),
'cx':os.getenv('GOO_CTX'),
'q':cities[path],
'searchType':'image'
})
data = urllib.request.urlopen(srch).read()
arr = json.loads(data)
newfile = ''
if 'items' in arr:
for search in arr['items']:
if any(x in search['link'].lower() for x in ['.jpg','.jpeg']):
newfile = time.strftime("%d_%m_%Y_%H_%M_%S_" + '.jpg')
data = False
try:
data = urllib.request.urlopen(search['link'], timeout=5, context=context).read()
except urllib.error.HTTPError as err:
data = False
except urllib.error.URLError as err:
data = False
except socket.timeout as err:
data = False
if data:
f = open(appdir + 'myfiles/' + newfile,'wb')
f.write(data)
f.close()
break
regex = re.compile(
r'^(?:http|ftp)s?://' # http:// or https://
r'(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+(?:[A-Z]{2,6}\.?|[A-Z0-9-]{2,}\.?)|' #domain...
r'localhost|' #localhost...
r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})' # ...or ip
r'(?::\d+)?' # optional port
r'(?:/?|[/?]\S+)$', re.IGNORECASE)
result = []
possible_feeds = []
sites = []
srch = 'https://www.googleapis.com/customsearch/v1?' + urllib.parse.urlencode({
'key':os.getenv('GOO_KEY'),
'cx':os.getenv('GOO_CTX'),
'q':cities[path] + ' news'
})
data = urllib.request.urlopen(srch).read()
arr = json.loads(data)
if 'items' in arr:
for search in arr['items']:
sites.append(search['link'])
uri = urllib.parse.urlparse(search['link'])
baseurl = uri.scheme + '://' + uri.netloc
if baseurl not in sites and not baseurl + '/' in sites:
sites.append( uri.scheme + '://' + uri.netloc )
srch = 'https://www.googleapis.com/customsearch/v1?' + urllib.parse.urlencode({
'key':os.getenv('GOO_KEY'),
'cx':os.getenv('GOO_CTX'),
'q':cities[path] + ' news',
'start': 11
})
data = urllib.request.urlopen(srch).read()
arr = json.loads(data)
if 'items' in arr:
for search in arr['items']:
sites.append(search['link'])
uri = urllib.parse.urlparse(search['link'])
baseurl = uri.scheme + '://' + uri.netloc
if baseurl not in sites and not baseurl + '/' in sites:
sites.append( uri.scheme + '://' + uri.netloc )
srch = 'https://www.googleapis.com/customsearch/v1?' + urllib.parse.urlencode({
'key':os.getenv('GOO_KEY'),
'cx':os.getenv('GOO_CTX'),
'q':cities[path] + ' news',
'start': 21
})
data = urllib.request.urlopen(srch).read()
arr = json.loads(data)
if 'items' in arr:
for search in arr['items']:
sites.append(search['link'])
uri = urllib.parse.urlparse(search['link'])
baseurl = uri.scheme + '://' + uri.netloc
if baseurl not in sites and not baseurl + '/' in sites:
sites.append( uri.scheme + '://' + uri.netloc )
for site in sites:
time.sleep(0.3)
try:
data = urllib.request.urlopen(site, timeout=5, context=context).read()
except urllib.error.HTTPError as err:
data = False
except urllib.error.URLError as err:
data = False
except socket.timeout as err:
data = False
if data:
tree = etree.HTML(data)
parsed_url = urllib.parse.urlparse(site)
base = parsed_url.scheme+"://"+parsed_url.hostname
for node in tree.findall('.//link'):
t = node.attrib.get('type')
if t:
if "rss" in t or "xml" in t:
href = node.attrib.get('href')
if href:
if '//' == href[:2]:
possible_feeds.append('http:'+href)
elif 'http' == href[:4]:
possible_feeds.append(href)
else:
possible_feeds.append(base+href)
for node in tree.findall('.//a'):
href = node.attrib.get('href')
if href:
if "xml" in href or "rss" in href or "feed" in href:
if 'http' == href[:4]:
possible_feeds.append(href)
else:
possible_feeds.append(base+href)
for url in possible_feeds:
if not re.match(regex, url):
print('NOT URL BRE ' + url)
else:
time.sleep(0.3)
try:
data = urllib.request.urlopen(url, timeout=5, context=context).read()
except urllib.error.HTTPError as err:
continue
except urllib.error.URLError as err:
continue
except socket.timeout as err:
continue
f = feedparser.parse(data)
if len(f.entries) > 0:
ftitle = 'News Feed'
if 'feed' in f:
if 'title' in f['feed']:
ftitle = f['feed']['title']
if url not in result:
result.append(url)
newrec = {
'name' : cities[path],
'image': newfile,
'maxres': '2000',
'allowPosts': '0',
'blockDownloads': '0',
'user_id' : 0,
'key' : randomword(8),
'public': '1',
'publicImage': newfile,
'publicName': cities[path],
'publicBio': '',
'publicName': cities[path],
'publicLink': 'https://rp.ly/' + path,
'publicPath': path,
'publicGroups': '[]',
'feeds': json.dumps(result),
'created' : str( timezone( 'US/Pacific' ).localize( datetime.now() ) )
}
print(json.dumps(result))
gr1 = add_one( 'groups', newrec )
if gr1 > 0:
newrec = {
'title' : '',
'image' : newfile,
'sound' : '',
'video' : '',
'groups' : json.dumps([int(gr1)]),
'inreplyto' : '',
'link':'',
'user_id' : 0,
'created' : str( timezone( 'US/Pacific' ).localize( datetime.now() ) )
}
rid = add_one( 'posts', newrec )
grps = get_one_by( 'groups', path, 'publicPath' )
if (len(grps) > 0):
urlsrc = 'https://' + mydomain + '/grp/' + grps[0]['key']
return redirect(urlsrc)
return 'OK'
@app.route('/g/<grpid>')
def groupuser(grpid):
grps = get_one_by( 'groups', grpid, 'key' )
if not len(grps) > 0:
return 'OK'
urlsrc = 'https://' + mydomain + '/g/' + grpid
#ht = '<!DOCTYPE html>' + "\n"
#ht = ht + '<html>' + "\n"
#ht = ht + ' <head>' + "\n"
#ht = ht + ' <title>' + grps[0]['name'] + '</title>'
ht = ''
ht = ht + ' <meta property="og:title" content="' + grps[0]['name'] + '" />' + "\n"
ht = ht + ' <meta property="og:type" content="website" />'
if not grps[0]['image'] is None:
ht = ht + ' <meta property="og:image:type" content="image/jpeg" />'
imgsrc = 'https://' + mydomain + '/grpFile?field=image&name=' + grps[0]['image']
ht = ht + ' <meta property="og:image" content="' + imgsrc + '" />' + "\n"
ht = ht + ' <meta property="og:url" content="' + urlsrc + '" />'
ht = ht + ' <meta property="og:description" content="' + grps[0]['name'] + '" />'
ht = ht + ' </head>' + "\n"
#ht = ht + ' <body>Group Page</body>' + "\n"
#ht = ht + '</html>' + "\n"
fleet = open(myapp).read()
fleet = fleet.replace('rp.ly',mydomain)
fleet = fleet.replace('</head>',ht)
response = make_response(fleet,200)
return response
#ht = ht + ' <meta name="description" content="' + grps[0]['name'] + '" />'
#ht = ht + ' <link rel="shortcut icon" href="' + imgsrc + '" type="image/x-icon" />'
@app.route('/updater')
def updater():
item = get_all('posts')[0]
if not 'video' in item:
rid = add_one( 'posts', {'video':''} )
if rid > 0:
del_one('posts',rid)
for p in get_all('posts'):
mod_one( 'posts', {'video':''}, p['id'])
item = get_all('posts')[0]
if not 'inreplyto' in item:
rid = add_one( 'posts', {'inreplyto':''} )
if rid > 0:
del_one('posts',rid)
for p in get_all('posts'):
mod_one( 'posts', {'inreplyto':''}, p['id'])
item = get_all('groups')[0]
if not 'publicBio' in item:
grobj = {
'public':'0',
'publicImage':'',
'publicName':'',
'publicBio':'',
'publicLink':'',
'publicGroups':'[]'
}
rid = add_one( 'groups', grobj )
if rid > 0:
del_one('groups',rid)
for i in get_all('groups'):
mod_one( 'groups', grobj, i['id'])
item = get_all('groups')[0]
if not 'publicPath' in item:
rid = add_one( 'groups', {'publicPath':''} )
if rid > 0:
del_one('groups',rid)
grobj = {
'publicPath':''
}
for i in get_all('groups'):
mod_one( 'groups', grobj, i['id'])
item = get_all('groups')[0]
if not 'shareLinks' in item:
rid = add_one( 'groups', {'shareLinks':'0'} )
if rid > 0:
del_one('groups',rid)
grobj = {
'shareLinks':'0'
}
for i in get_all('groups'):
mod_one( 'groups', grobj, i['id'])
item = get_all('posts')[0]
if not 'link' in item:
rid = add_one( 'posts', {'link':''} )
if rid > 0:
del_one('posts',rid)
grobj = {
'link':''
}
for i in get_all('posts'):
mod_one( 'posts', grobj, i['id'])
item = get_all('groups')[0]
if not 'feeds' in item:
rid = add_one( 'groups', {'feeds':'[]'} )
if rid > 0:
del_one('groups',rid)
grobj = {
'feeds':'[]'
}
for i in get_all('groups'):
mod_one( 'groups', grobj, i['id'])
return 'UPDATED DB'
@app.route('/')
def index():
global fleet
if not os.getenv('DEV_IP') is None:
fleet = open(myapp).read()
fleet = fleet.replace('rp.ly',mydomain)
return fleet
pall = False
if mydomain == 'photo.gy':
pall = 'ef6c57 7ed3b2 b9e6d3 f2f2f2'.split()
#if mydomain == 'audio.gy':
pall = '445c3c fda77f c9d99e fae8c8'.split()
if mydomain == 'movie.gd':
pall = '557571 d49a89 d49a89 f4f4f4'.split()
if pall:
fleet = fleet.replace('222831',pall[0])
fleet = fleet.replace('393e46',pall[1])
fleet = fleet.replace('32e0c4',pall[2])
fleet = fleet.replace('ffd3e1',pall[3])
return fleet
def randomword(length):
letters = string.ascii_lowercase
return ''.join(random.choice(letters) for i in range(length))
def saveFile(field):
curr = current_user()
if not 'id' in curr:
return ''
sess = get_one_by('sessions',field + str(curr['id']),'field')
if len(sess) > 0:
save = sess[0]['data']
return save
return ''
def setSession(field,data):
curr = current_user()
if not 'id' in curr:
return ''
sess = get_one_by('sessions',field + str(curr['id']),'field')
if len(sess) > 0:
mod_one('sessions',{'data':data},sess[0]['id'])
else:
add_one('sessions',{'field':field + str(curr['id']),'data':data})
return True
def clearSession(field):
curr = current_user()
if not 'id' in curr:
return ''
sess = get_one_by('sessions',field + str(curr['id']),'field')
if len(sess) > 0:
mod_one('sessions',{'data':''},sess[0]['id'])
return True
class FleetParser(HTMLParser):
def __init__(self):
HTMLParser.__init__(self)
self.recording = 0
self.data = []
def handle_starttag(self, tag, attributes):
if tag != 'script':
return
if self.recording:
self.recording += 1
return
for name, value in attributes:
if name == 'id' and value == 'server':
break
else:
return
self.recording = 1
def handle_endtag(self, tag):
if tag == 'script' and self.recording:
self.recording -= 1
def handle_data(self, data):
if self.recording:
self.data.append(data)
if not 'username' in vars() or 'username' in globals():
username = ''
def dict_factory(cursor, row):
d = {}
for idx, col in enumerate(cursor.description):
d[col[0]] = row[idx]
return d
def add_one(resource,obj):
resource = username + resource
conn = sqlite3.connect('sqlite.db')
cur = conn.cursor()
cur.execute("""SELECT name FROM sqlite_master WHERE type='table' AND name=?;
""",(resource,))
if bool(cur.fetchone()) == False:
sql = "CREATE TABLE " + resource + "(id INTEGER PRIMARY KEY"
for key, val in obj.items():
sql = sql + "," + key + " TEXT"
sql = sql + ")"
cur.execute(sql)
sql = "pragma table_info(" + resource + ")"
result = cur.execute(sql)
fields = result.fetchall()
exist = []
for fie in fields:
exist.append(fie[1])
for field in obj.keys():
if not field in exist:
sql = "alter table " + resource + " add column " + field + " TEXT"
cur.execute(sql)
cols = ', '.join('"{}"'.format(col) for col in obj.keys())
vals = ', '.join(':{}'.format(col) for col in obj.keys())
sql = 'INSERT INTO "{0}" ({1}) VALUES ({2})'.format(resource, cols, vals)
cur = conn.cursor()
cur.execute(sql, obj)
conn.commit()
rid = cur.lastrowid
conn.close()
return rid
def get_one_by(resource,value,field):
resource = username + resource
conn = sqlite3.connect('sqlite.db')
conn.row_factory = dict_factory
cur = conn.cursor()
vals = []
sql = "SELECT * FROM " + resource
sql = sql + " WHERE " + field + " like ?"
vals.append(value)
try:
result = conn.cursor().execute(sql,vals)
except sqlite3.OperationalError:
return []
return result.fetchall()
def get_one(resource,id):
resource = username + resource
conn = sqlite3.connect('sqlite.db')
conn.row_factory = dict_factory
cur = conn.cursor()
cur.execute("""SELECT name FROM sqlite_master WHERE type='table' AND name=?;
""",(resource,))
if bool(cur.fetchone()) == False:
return []
sql = "SELECT * FROM " + resource + " WHERE id = ?"
try:
result = conn.cursor().execute(sql,[id])
except sqlite3.OperationalError:
return []
return result.fetchall()
def mod_one(resource,obj,id):
resource = username + resource
conn = sqlite3.connect('sqlite.db')
cur = conn.cursor()
sql = "UPDATE " + resource + " SET "
comma = ""
vals = []
for key, val in obj.items():
sql = sql + comma + key + "=?"
vals.append(val)
comma = ","
sql = sql + " WHERE id = ?"
vals.append(id)
conn.cursor().execute(sql,vals)
conn.commit()
return True
def del_one(resource,id):
resource = username + resource
conn = sqlite3.connect('sqlite.db')
cur = conn.cursor()
sql = "DELETE FROM " + resource + " WHERE id = ?"
conn.cursor().execute(sql,[id])
conn.commit()
return True
def get_all(resource):
resource = username + resource
conn = sqlite3.connect('sqlite.db')
cur = conn.cursor()
cur.execute("""SELECT name FROM sqlite_master WHERE type='table' AND name=?;
""",(resource,))
if bool(cur.fetchone()) == False:
return []
conn.row_factory = dict_factory
try:
result = conn.cursor().execute("SELECT * FROM " + resource + ' ORDER BY id DESC')
except sqlite3.OperationalError:
return []
return result.fetchall()
def sendcomet(recips,obj):
for id in recips:
recip = get_parent(id)
print('SEND ' + '/rply/' + recip['code'],file=sys.stderr)
#socketio.emit('new item', obj, namespace='/rply/' + recip['code'] )
return True
def get_parent(id):
id = int(id)
conn = sqlite3.connect('sqlite.db')
conn.row_factory = dict_factory
cur = conn.cursor()
sql = "SELECT * FROM contacts WHERE id = ?"
result = conn.cursor().execute(sql,[id])
rows = result.fetchall()
if len(rows) > 0:
if len(rows[0]['email']) > 0:
items = get_one_by( 'contacts', rows[0]['email'], 'email' )
if len(items) > 0:
for con in items:
if con['user_id'] is None:
return con
if len(rows[0]['phone']) > 0:
items = get_one_by( 'contacts', rows[0]['phone'], 'phone' )
if len(items) > 0:
for con in items:
if con['user_id'] is None:
return con
return rows[0]
def share_my_contact(newrec,grps):
usr = []
user = current_user()
if len(newrec['phone']) > 0:
loginuser = newrec['phone']
usr = get_one_by('contacts',loginuser,'phone')
if len(newrec['email']) > 0:
loginuser = newrec['email']
usr = get_one_by('contacts',loginuser.lower(),'email')
if len(usr) > 0:
for u in usr:
if u['user_id'] is None:
contacts = get_one_by('contacts',u['id'],'user_id')
exists = False
existscon = {}
for con in contacts:
if len(con['phone']) > 0:
if user['phone'] == con['phone']:
exists = True
existscon = con
if len(con['email']) > 0:
if user['email'] == con['email']:
exists = True
existscon = con
name = ''
if not exists:
ra = randomword(6)
copy = current_user()
copy['user_id'] = u['id']
copy['groups'] = json.dumps(grps)
copy['code'] = ra
del copy['id']
con = add_one( 'contacts', copy )
name = copy['name']
else:
if len(grps) > 0:
items = get_one( 'groups', grps[0] )
user = get_user(existscon['id'])
groups = json.loads(user['groups'])
groups.append(items[0]['id'])
con = mod_one('contacts',{'groups': json.dumps(groups)},existscon['id'])
name = user['name']
if len(grps) > 0:
cont = u
grpdata = get_one('groups',grps[0])
if len(cont['phone']) > 0:
client = Client(twilio_id, twilio_token)
rl = name + " joined your gallery " + grpdata[0]['name'] + " on " + mydomain
message = client.messages.create(
body=rl,
from_='+1' + os.getenv('TWILIO_FROM'),
to=cont['phone']
)
print('notify JOIN ' + cont['phone'], file=sys.stderr)
if len(cont['email']) > 0:
owner = current_user()
em = cont['email']
message = Mail(
from_email=owner['name'] + ' via ' + mydomain + ' <' + os.getenv('SENDGRID_FROM') + '>',
to_emails=em,
subject=name + " joined your gallery " + grpdata[0]['name'] + " on " + mydomain,
html_content='<strong><p>' + name + " joined your gallery " + grpdata[0]['name'] + " on " + mydomain + '</p></strong>')
sg = SendGridAPIClient(sendgrid_token)
response = sg.send(message)
print('notify JOIN ' + em, file=sys.stderr)
return
def notify_comment(addgrp,name):
grpdata = get_one('groups',addgrp)
if 'user_id' in grpdata[0]:
cont = get_user(grpdata[0]['user_id'])
if len(cont['phone']) > 0:
client = Client(twilio_id, twilio_token)
rl = name + " commented in your gallery " + grpdata[0]['name'] + " on " + mydomain
message = client.messages.create(
body=rl,
from_='+1' + os.getenv('TWILIO_FROM'),
to=cont['phone']
)
print('notify COMMENT ' + cont['phone'], file=sys.stderr)
if len(cont['email']) > 0:
owner = current_user()
em = cont['email']
message = Mail(
from_email=owner['name'] + ' via ' + mydomain + ' <' + os.getenv('SENDGRID_FROM') + '>',
to_emails=em,
subject=name + " commented in your gallery " + grpdata[0]['name'] + " on " + mydomain,
html_content='<strong><p>' + name + " commented in your gallery " + grpdata[0]['name'] + " on " + mydomain + '</p></strong>')
sg = SendGridAPIClient(sendgrid_token)
response = sg.send(message)
print('notify COMMENT ' + em, file=sys.stderr)
return
def notify_sponsor(plan,addgrp,name):
grpdata = get_one('groups',addgrp)
plans = {'fans300':'$3/mo','fans50':'$6/yr','firstyear5':'$5/mo'}
if 'user_id' in grpdata[0]:
cont = get_user(grpdata[0]['user_id'])
if len(cont['phone']) > 0:
client = Client(twilio_id, twilio_token)
rl = name + " paid for a " + plans[plan] + " subscription to " + grpdata[0]['name'] + " on " + mydomain
message = client.messages.create(
body=rl,
from_='+1' + os.getenv('TWILIO_FROM'),
to=cont['phone']
)
print('notify PAY ' + cont['phone'], file=sys.stderr)
if len(cont['email']) > 0:
owner = current_user()
em = cont['email']
message = Mail(
from_email=owner['name'] + ' via ' + mydomain + ' <' + os.getenv('SENDGRID_FROM') + '>',
to_emails=em,
subject=name + " paid for a " + plans[plan] + " subscription to " + grpdata[0]['name'] + " on " + mydomain,
html_content='<strong><p>' + name + " bought a " + plan + " subscription on " + mydomain + '</p></strong>')
sg = SendGridAPIClient(sendgrid_token)
response = sg.send(message)
print('notify PAY ' + em, file=sys.stderr)
return True
def notify_photo(id,grpcode,email,phone):
num = randint(100, 999)
cont = get_user(id)
if len(cont['phone']) > 0 and phone:
urlsrc = 'https://' + mydomain + '/g/' + grpcode + '#/connect/' + cont['code']
items = get_one_by( 'groups', grpcode, 'key' )
client = Client(twilio_id, twilio_token)
rl = current_user()['name'] + " updated the gallery " + items[0]['name'] + ' ' + urlsrc
message = client.messages.create(
body=rl,
from_='+1' + os.getenv('TWILIO_FROM'),
to=cont['phone']
)
print('notify ' + cont['phone'], file=sys.stderr)
if len(cont['email']) > 0 and email:
owner = current_user()
em = cont['email']
invitetext = ' updated the gallery '
items = get_one_by( 'groups', grpcode, 'key' )
imgsrc = ''
if not items[0]['image'] is None:
imgsrc = urllib.parse.quote(items[0]['image'])
html = '<div style="font-family:courier,monospace;width:90%"><img alt="group image for ' + items[0]['name'] + '" style="height:auto;padding:30px;display:block;margin-left:auto;margin-right:auto;width:50%;padding:10px;" src="https://' + mydomain + '/grpFile?field=image&name='
html = html + imgsrc + '" /><p style="text-align:center;display:block;padding:10px;">' + owner['name'] + invitetext + items[0]['name'] + '</p><a style="text-align:center;background-color:#32e0c4;display:block;color:black;text-decoration:none;padding:10px;" href="https://' + mydomain + '/g/' + grpcode + '#/connect/' + cont['code'] + '">View Gallery</a></div>'
regex = re.compile('[^a-zA-Z ]')
oname = regex.sub('', owner['name'])
message = Mail(
from_email=oname + ' via ' + mydomain + ' <' + os.getenv('SENDGRID_FROM') + '>',
to_emails=em,
subject="Updated gallery, " + items[0]['name'],
html_content=html)
sg = SendGridAPIClient(sendgrid_token)
response = sg.send(message)
print('notify ' + em, file=sys.stderr)
return True
def expired(item):
if 'expires' in item:
if not item['expires'] is None:
from dateutil import tz
tzinfos = {"-08:00": tz.gettz('US/Pacific')}
dt = parser.parse(item['expires'], tzinfos=tzinfos)
if dt < timezone( 'US/Pacific' ).localize( datetime.now() ):
return True
return False
def notify_del(id):
num = randint(100, 999)
cont = get_user(id)
if len(cont['phone']) > 0:
client = Client(twilio_id, twilio_token)
rl = cont['name'] + " your " + mydomain + " post expired and was deleted "
message = client.messages.create(
body=rl,
from_='+1' + os.getenv('TWILIO_FROM'),
to=cont['phone']
)
print('del notify ' + cont['phone'], file=sys.stderr)
if len(cont['email']) > 0:
owner = cont
em = cont['email']
message = Mail(
from_email=owner['name'] + ' via ' + mydomain + ' <' + os.getenv('SENDGRID_FROM') + '>',
to_emails=em,
subject=owner['name'] + " your post expired ",
html_content='<strong><p>your post expired and was deleted</p></strong>')
sg = SendGridAPIClient(sendgrid_token)
response = sg.send(message)
print('del notify ' + em, file=sys.stderr)
return True
def get_user(id):
resource = username + 'contacts'
id = int(id)
conn = sqlite3.connect('sqlite.db')
conn.row_factory = dict_factory
cur = conn.cursor()
sql = "SELECT * FROM " + resource + " WHERE id = ?"
try:
result = conn.cursor().execute(sql,[id])
except sqlite3.OperationalError:
return []
rows = result.fetchall()
if len(rows) > 0:
if len(rows[0]['email']) > 0:
items = get_one_by( 'contacts', rows[0]['email'], 'email' )
if len(items) > 0:
for con in items:
rows[0]['groups'] = json.dumps(json.loads(rows[0]['groups']) + json.loads(con['groups']))
if len(rows[0]['phone']) > 0:
items = get_one_by( 'contacts', rows[0]['phone'], 'phone' )
if len(items) > 0:
for con in items:
rows[0]['groups'] = json.dumps(json.loads(rows[0]['groups']) + json.loads(con['groups']))
rows[0]['groups'] = json.dumps(list(set(json.loads(rows[0]['groups']))))
return rows[0]
else:
return []
def current_user():
rows = []
if 'user' in session:
id = session['user']
conn = sqlite3.connect('sqlite.db')
conn.row_factory = dict_factory
cur = conn.cursor()
sql = "SELECT * FROM contacts WHERE id = ?"
result = conn.cursor().execute(sql,[id])
rows = result.fetchall()
if len(rows) > 0:
if len(rows[0]['email']) > 0:
items = get_one_by( 'contacts', rows[0]['email'], 'email' )
if len(items) > 0:
for con in items:
rows[0]['groups'] = json.dumps(json.loads(rows[0]['groups']) + json.loads(con['groups']))
if len(rows[0]['phone']) > 0:
items = get_one_by( 'contacts', rows[0]['phone'], 'phone' )
if len(items) > 0:
for con in items:
rows[0]['groups'] = json.dumps(json.loads(rows[0]['groups']) + json.loads(con['groups']))
rows[0]['groups'] = json.dumps(list(set(json.loads(rows[0]['groups']))))
return rows[0]
return {'user_id':0,'groups':'[0]','code':'xoxo2020'}
def can(action,resource,user,obj=False):
if user['groups'] is None:
return False
groups = json.loads(user['groups'])
for grp in groups:
for abb in all_abilities:
if abb['group_id'] == grp and abb['resource'] == resource:
if action in abb['actions']:
if 'conditions' in abb:
for cond in abb['conditions']:
if cond[0] in obj:
if isinstance(obj[cond[0]],str):
if isinstance(user[cond[1]],int):
if user[cond[1]] == int(obj[cond[0]]):
return True
if isinstance(obj[cond[0]],int):
if isinstance(user[cond[1]],int):
if user[cond[1]] == obj[cond[0]]:
return True
if not obj[cond[0]] is None:
needle = json.loads(obj[cond[0]])
if isinstance(needle,list):
haystack = json.loads(user[cond[1]])
if isinstance(haystack,list):
for findit in needle:
if findit in haystack:
return True
return False
else:
return True
return False