Lot of stuff

p2p
quadrismegistus 4 years ago
parent 091f63b4eb
commit a3a314669f

4
.gitignore vendored

@ -3,4 +3,6 @@ client/examples/*
client/komrade.json
uploads
uploads/*
uploads/*/*
uploads/*/*
client/cache
client/log.txt

@ -25,6 +25,8 @@ import requests,json
from kivy.storage.jsonstore import JsonStore
from kivy.core.window import Window
from kivy.core.text import LabelBase
import shutil
Window.size = (640, 1136) #(2.65 * 200, 5.45 * 200)
@ -58,9 +60,6 @@ class MyLabel(MDLabel): pass
def get_tor_proxy_session():
@ -116,7 +115,7 @@ class MainApp(MDApp):
if not self.is_logged_in():
self.root.change_screen('login')
else:
self.root.post_id=135
self.root.post_id=179
self.root.change_screen('view')
return self.root
@ -172,36 +171,60 @@ class MainApp(MDApp):
server_filename=''
if filename:
with self.get_session() as sess:
#res = sess.post(url, files=filesd, data={'data':json.dumps(jsond)}, headers=headers)
with self.get_session() as sess:
if filename:
log(filename)
self.root.ids.add_post_screen.ids.post_status.text='Uploading file'
r = sess.post(url_upload,files={'file':open(filename,'rb')})
if r.status_code==200:
rdata = r.json()
server_filename = rdata.get('filename','')
if server_filename:
self.root.ids.add_post_screen.ids.post_status.text='File uploaded'
with self.get_session() as sess:
with sess.post(url_upload,files={'file':open(filename,'rb')}) as r1:
if r1.status_code==200:
rdata1 = r1.json()
server_filename = rdata1.get('filename','')
if server_filename:
self.root.ids.add_post_screen.ids.post_status.text='File uploaded'
# add post
#log(self.root.ids.add_post_screen.ids.keys())
self.root.ids.add_post_screen.ids.post_status.text='Creating post'
jsond={'img_src':server_filename, 'content':content}
r = sess.post(url_post, json=jsond)
log('got back from post: ' + r.text)
rdata = r.json()
post_id = rdata.get('post_id',None)
if post_id:
self.root.ids.add_post_screen.ids.post_status.text='Post created'
self.root.view_post(int(post_id))
with sess.post(url_post, json=jsond) as r2:
log('got back from post: ' + r2.text)
rdata2 = r2.json()
post_id = rdata2.get('post_id',None)
if post_id:
self.root.ids.add_post_screen.ids.post_status.text='Post created'
self.root.view_post(int(post_id))
def get_post(self,post_id):
with self.get_session() as sess:
r = sess.get(self.api+'/post/'+str(post_id))
jsond = r.json()
return jsond
# get json from cache?
ofn_json = os.path.join('cache','json',str(post_id)+'.json')
if os.path.exists(ofn_json):
with open(ofn_json) as f:
jsond = json.load(f)
else:
with self.get_session() as sess:
with sess.get(self.api+'/post/'+str(post_id)) as r:
jsond = r.json()
# cache it!
with open(ofn_json,'w') as of:
json.dump(jsond, of)
# is there an image?
img_src = jsond.get('img_src','')
if img_src:
# is it cached?
ofn_image = os.path.join('cache','img',img_src)
if not os.path.exists(ofn_image):
# create dir?
ofn_image_dir = os.path.split(ofn_image)[0]
if not os.path.exists(ofn_image_dir): os.makedirs(ofn_image_dir)
log('getting image!')
with self.get_session() as sess:
with sess.get(self.api+'/download/'+img_src,stream=True) as r:
with open(ofn_image,'wb') as of:
shutil.copyfileobj(r.raw, of)
return jsond
if __name__ == '__main__':

@ -84,7 +84,10 @@ MyLayout:
# transition: NoTransition()
LoginScreen:
id: login_screen
FeedScreen:
id: feed_screen
AddPostScreen:
id: add_post_screen
@ -93,4 +96,7 @@ MyLayout:
id: view_post_screen
MessagesScreen:
id: messages_screen
NotificationsScreen:
id: notifications_screen

@ -1,4 +1,5 @@
#:import FeedScreen screens.feed.feed.FeedScreen
#:import Window kivy.core.window.Window
<FeedScreen>:
name: 'feed'
@ -20,16 +21,49 @@
height: '25'
size_hint_y: None
<PostImageLayout>:
id: image_layout
cols: 1
orientation: 'vertical'
size_hint: (1,None)
# md_bg_color: 1,1,0,1
pos_hint: {'center_x':0.5} #, 'y':0.5}
# height: '100dp' #self.minimum_height
# width: '100dp'
height: '300dp'
# spacing:'100dp'
# padding:'100dp'
<PostImage>:
# height: '25'
# size: self.norm_image_size
allow_stretch: True
# keep_ratio: True
size_hint_y: None
size_hint_x: None
pos_hint: {'center_x':0.5}
size: (self.height*self.image_ratio,self.height)
# width: self.parent.width
# height: self.parent.width/self.image_ratio
# height: self.height
# width: self.height*self.image_ratio
canvas:
Color:
rgb: 0,0,0,1
Line:
width: 7
rectangle: (self.x, self.y, self.width, self.height)
<PostGridLayout>:
cols: 1
size_hint: (1,None)
pos_hint: {'center_x':0.5, 'center_y':0}
md_bg_color: 1,1,0,1
height: self.minimum_height
# height: self.minimum_height
<PostAuthorLayout>:
@ -43,6 +77,8 @@
height: '100dp' #self.minimum_height
# radius:[20,]
# border_radius:20
<PostAuthorAvatar>:
size_hint:(None,None)
@ -88,7 +124,11 @@
text_color:1,0,0,1
theme_text_color: 'Custom'
halign: 'left'
<PostScrollView>:
size_hint: (1,None)
# pos_hint: {'center_y':0.5}
#size: ('300dp',self.height)
<PostCard>:
id: post

@ -2,9 +2,10 @@ from kivymd.uix.label import MDLabel
from kivy.uix.gridlayout import GridLayout
from kivy.uix.image import AsyncImage
from kivymd.uix.boxlayout import MDBoxLayout
from kivymd.uix.card import MDCard
from kivymd.uix.card import MDCard, MDSeparator
from kivy.uix.scrollview import ScrollView
from screens.base import ProtectedScreen
from main import log
### POST CODE
class PostTitle(MDLabel): pass
@ -21,6 +22,8 @@ class PostContent(MDLabel):
class PostAuthorLayout(MDBoxLayout): pass
class PostImageLayout(MDBoxLayout): pass
class PostAuthorLabel(MDLabel):
def __init__(self,**kwargs):
super().__init__(**kwargs)
@ -30,6 +33,10 @@ class PostAuthorLabel(MDLabel):
pass
class PostAuthorAvatar(AsyncImage): pass
class PostLayout(MDBoxLayout): pass
class PostScrollView(ScrollView): pass
class PostCard(MDCard):
def __init__(self, author = None, title = None, img_src = None, content = None):
super().__init__()
@ -43,30 +50,59 @@ class PostCard(MDCard):
author_section_layout = PostAuthorLayout()
author_label = PostAuthorLabel(text=self.author)
author_label.font_size = '28dp'
author_avatar = PostAuthorAvatar(source=self.img_src)
author_avatar = PostAuthorAvatar(source='avatar.jpg') #self.img_src)
author_section_layout.add_widget(author_avatar)
author_section_layout.add_widget(author_label)
# author_section_layout.add_widget(author_avatar)
self.add_widget(author_section_layout)
# self.add_widget(author_section_layout)
if self.img_src:
image_layout = PostImageLayout()
image = PostImage(source=self.img_src)
image.height = '300dp'
image_layout.add_widget(image)
image_layout.height='300dp'
# log(image.image_ratio)
title = PostTitle(text=self.title)
# image = PostImage(source=self.img_src)
content = PostContent(text=self.content)
# post_layout = PostGridLayout()
#content = PostContent()
# add to screen
self.add_widget(title)
# self.add_widget(image)
self.add_widget(content)
#self.add_widget(layout)
# self.add_widget(title)
# post_layout.add_widget(author_section_layout)
# post_layout.add_widget(image_layout)
# post_layout.add_widget(content)
scroller = PostScrollView()
self.add_widget(author_section_layout)
# self.add_widget(MDLabel(text='hello'))
log('img_src ' + str(bool(self.img_src)))
if self.img_src: self.add_widget(image_layout)
def estimate_height(minlen=100,maxlen=500):
num_chars = len(self.content)
# num_lines = num_chars
height = num_chars*1.1
if height>maxlen: height=maxlen
if height<minlen: height=minlen
return height
scroller.size = ('300dp','%sdp' % estimate_height())
# scroller.bind(size=('300dp',scroller.setter('height'))
scroller.add_widget(content)
self.add_widget(scroller)
# self.add_widget(post_layout)
#####
class FeedScreen(ProtectedScreen):
def on_enter(self):
def on_pre_enter_test(self):
i=0
lim=5
with open('tweets.txt') as f:
@ -83,3 +119,5 @@ class FeedScreen(ProtectedScreen):
content=ln.strip())
print(post)
self.ids.post_carousel.add_widget(post)

@ -9,7 +9,7 @@
MyBoxLayout:
id: postbox
size_hint:0.5,0.18
size_hint:0.5,0.2
# FileChoose:
# size_hint_y: 0.1
@ -24,38 +24,40 @@
multiline: True
helper_text_mode: "on_error"
color_mode: 'custom'
size_hint_y:0.5
# size: ('300dp','300dp')
line_color_focus: 1,0,0,1
line_color_normal: 1,0,0,1
line_color_normal: 0,0,0,1
current_hint_text_color: 1,0,0,1
MDBoxLayout:
id: buttonbox
size_hint_y: None
adaptive_width: True
height: '56dp'
spacing: '10dp'
pos_hint: {'center_x': .5}
MDBoxLayout:
id: buttonbox
# size_hint_y: None
adaptive_width: True
height: '56dp'
spacing: '10dp'
pos_hint: {'center_x': .5, 'y':0.41}
FileChoose:
id: file_chooser_button
text: "upload"
#text: app.root.ids.scr_mngr.etc
on_release: self.choose()
#app.register(username.text, password.text)
theme_text_color: "Custom"
text_color: 1,0,0,1
md_bg_color: 0,0,0,1
FileChoose:
id: file_chooser_button
text: "upload"
#text: app.root.ids.scr_mngr.etc
on_release: self.choose()
#app.register(username.text, password.text)
theme_text_color: "Custom"
text_color: 1,0,0,1
md_bg_color: 0,0,0,1
MDRectangleFlatButton:
text: "post"
on_release: app.post(post_content_input.text, file_chooser_button.selection)
#app.root.change_screen("welcome")
theme_text_color: "Custom"
text_color: 1,0,0,1
md_bg_color: 0,0,0,1
MDRectangleFlatButton:
text: "post"
on_release: app.post(post_content_input.text, file_chooser_button.selection)
#app.root.change_screen("welcome")
theme_text_color: "Custom"
text_color: 1,0,0,1
md_bg_color: 0,0,0,1

@ -5,7 +5,7 @@ from kivy.properties import ListProperty,ObjectProperty
from kivy.app import App
from main import log
from screens.feed.feed import *
import os
class FileChoose(MDRectangleFlatButton):
@ -44,17 +44,26 @@ class AddPostScreen(ProtectedScreen):
class ViewPostScreen(ProtectedScreen):
post_id = ObjectProperty()
def on_enter(self):
def on_pre_enter(self):
for child in self.children:
log('child: '+str(child))
self.remove_widget(child)
post = self.app.get_post(self.root.post_id)
log(post)
post = PostCard(
author='Marx Zuckerberg',
img_src=os.path.join('cache','img',post['img_src']) if post['img_src'] else ''
kwargs = dict(author='Marx Zuckerberg',
title='',
img_src='avatar.jpg',
img_src=img_src,
content=post['content'])
log(kwargs)
post = PostCard(**kwargs)
print(post)
self.add_widget(post)
def on_enter(self):
pass
pass

@ -36,6 +36,8 @@ class Handler(FileSystemEventHandler):
@staticmethod
def on_any_event(event):
if '/cache/' in str(event.src_path): return None
if event.is_directory:
return None
@ -47,6 +49,7 @@ class Handler(FileSystemEventHandler):
# Taken any action here when a file is modified.
if event.src_path.endswith('.json'): return None
if '.json' in str(event.src_path): return None
if event.src_path.endswith('log.txt'): return None
print(" \n\n\n\n\nReceived modified event - %s." % event.src_path)
run_command()

@ -1,5 +1,5 @@
import flask,os
from flask import request, jsonify
from flask import request, jsonify, send_from_directory
from pathlib import Path
from models import *
from flask_api import FlaskAPI, status, exceptions
@ -21,7 +21,7 @@ app.config['UPLOAD_DIR'] = 'uploads/'
ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg', 'gif'}
@app.route('/')
def home(): return '404 go home friend'
def home(): return {'error':'404 go home friend'}
# Api Functions
@ -34,18 +34,18 @@ def login():
name=data.get('name','')
passkey=data.get('passkey','')
if not (name and passkey):
return 'Login failed',status.HTTP_400_BAD_REQUEST
return {'error':'Login failed'},status.HTTP_400_BAD_REQUEST
persons = list(Person.match(G,name))
if not persons:
return 'Login failed',status.HTTP_401_UNAUTHORIZED
return {'error':'Login failed'},status.HTTP_401_UNAUTHORIZED
person = persons[0]
real_passkey = person.passkey
if not check_password_hash(real_passkey, passkey):
return 'Login failed',status.HTTP_401_UNAUTHORIZED
return {'error':'Login failed'},status.HTTP_401_UNAUTHORIZED
return 'Login success',status.HTTP_200_OK
return {'success':'Login success'},status.HTTP_200_OK
@app.route('/api/register',methods=['POST'])
def register():
@ -55,11 +55,11 @@ def register():
passkey=data.get('passkey','')
if not (name and passkey):
return 'Register failed',status.HTTP_400_BAD_REQUEST
return {'error':'Register failed'},status.HTTP_400_BAD_REQUEST
person = list(Person.match(G,name))
if person:
return 'User exists',status.HTTP_401_UNAUTHORIZED
return {'error':'User exists'},status.HTTP_401_UNAUTHORIZED
passkey = generate_password_hash(passkey)
@ -70,7 +70,7 @@ def register():
G.push(person)
print('REGISTERED!',data)
return 'Account created',status.HTTP_200_OK
return {'success':'Account created'},status.HTTP_200_OK
@ -93,7 +93,7 @@ def upload_file():
files = request.files
# check if the post request has the file part
if 'file' not in request.files:
return 'No file found',status.HTTP_204_NO_CONTENT
return {'error':'No file found'},status.HTTP_204_NO_CONTENT
file = request.files['file']
@ -101,9 +101,10 @@ def upload_file():
# submit an empty part without filename
print('filename!',file.filename)
if file.filename == '':
return 'No filename',status.HTTP_206_PARTIAL_CONTENT
return {'error':'No filename'},status.HTTP_206_PARTIAL_CONTENT
if file and allowed_file(file.filename):
print('uploading file...')
prefix,filename = get_random_filename(file.filename) #secure_filename(file.filename)
#odir = os.path.join(app.config['UPLOAD_DIR'], os.path.dirname(filename))
#if not os.path.exists(odir):
@ -111,10 +112,9 @@ def upload_file():
if not os.path.exists(folder): os.mkdir(folder)
file.save(os.path.join(folder, filename))
#return redirect(url_for('uploaded_file', filename=filename))
return prefix+'/'+filename, status.HTTP_200_OK
return {'filename':prefix+'/'+filename}, status.HTTP_200_OK
# print('RECEIVED:',files['file'])
return 'Post created',status.HTTP_200_OK
return {'error':'Upload failed'},status.HTTP_406_NOT_ACCEPTABLE
@app.route('/api/post',methods=['POST'])
@ -141,6 +141,11 @@ def post(post_id=None):
print(post.data)
return post.data,status.HTTP_200_OK
@app.route('/api/download/<prefix>/<filename>',methods=['GET'])
def download(prefix, filename):
filedir = os.path.join(app.config['UPLOAD_DIR'], prefix)
print(filedir, filename)
return send_from_directory(filedir, filename)
### READ

Loading…
Cancel
Save