2
0
mirror of https://github.com/ComradCollective/Comrad synced 2024-11-01 21:40:32 +00:00
Comrad/client/main.py

260 lines
7.7 KiB
Python
Raw Normal View History

2020-08-03 10:25:58 +00:00
from kivy.uix.screenmanager import Screen,ScreenManager
from kivymd.app import MDApp
2020-08-03 15:48:47 +00:00
from kivymd.uix.button import MDFillRoundFlatButton, MDIconButton
from kivymd.uix.toolbar import MDToolbar
from kivymd.uix.screen import MDScreen
2020-08-03 10:25:58 +00:00
from kivy.lang import Builder
2020-08-03 15:48:47 +00:00
from kivy.uix.boxlayout import BoxLayout
from kivymd.theming import ThemeManager
from kivy.properties import ObjectProperty,ListProperty
import time
from collections import OrderedDict
from functools import partial
from kivy.uix.screenmanager import NoTransition
from kivymd.uix.label import MDLabel
from kivy.uix.widget import Widget
2020-08-03 16:29:03 +00:00
from kivymd.uix.list import OneLineListItem
from kivymd.uix.card import MDCard, MDSeparator
2020-08-06 10:24:09 +00:00
from kivymd.uix.boxlayout import MDBoxLayout
2020-08-03 20:16:30 +00:00
from kivy.uix.gridlayout import GridLayout
from kivy.metrics import dp
from kivy.properties import NumericProperty
2020-08-03 21:20:38 +00:00
from kivymd.uix.list import * #MDList, ILeftBody, IRightBody, ThreeLineAvatarListItem, TwoLineAvatarListItem, BaseListItem, ImageLeftWidget
from kivy.uix.image import Image, AsyncImage
2020-08-06 08:46:51 +00:00
import requests,json
2020-08-06 09:43:13 +00:00
from kivy.storage.jsonstore import JsonStore
from kivy.core.window import Window
2020-08-09 10:32:42 +00:00
from kivy.core.text import LabelBase
2020-08-06 09:43:13 +00:00
Window.size = (640, 1136) #(2.65 * 200, 5.45 * 200)
2020-08-06 08:46:51 +00:00
2020-08-03 10:25:58 +00:00
2020-08-03 15:48:47 +00:00
root = None
app = None
2020-08-03 10:25:58 +00:00
2020-08-03 15:48:47 +00:00
def log(x):
with open('log.txt','a+') as of:
of.write(str(x)+'\n')
2020-08-03 10:25:58 +00:00
2020-08-06 10:24:09 +00:00
class MyLayout(MDBoxLayout):
2020-08-03 15:48:47 +00:00
scr_mngr = ObjectProperty(None)
2020-08-06 08:46:51 +00:00
2020-08-03 15:48:47 +00:00
def change_screen(self, screen, *args):
self.scr_mngr.current = screen
2020-08-03 20:16:30 +00:00
2020-08-06 10:24:09 +00:00
class MyBoxLayout(MDBoxLayout): pass
class MyLabel(MDLabel): pass
2020-08-03 20:16:30 +00:00
2020-08-09 10:32:42 +00:00
### POST CODE
2020-08-06 12:55:26 +00:00
class PostTitle(MDLabel): pass
2020-08-09 10:32:42 +00:00
class PostGridLayout(GridLayout): pass
class PostImage(AsyncImage): pass
class PostContent(MDLabel):
def __init__(self,**kwargs):
super().__init__(**kwargs)
self.bind(width=lambda s, w: s.setter('text_size')(s, (w, None)))
self.bind(texture_size=self.setter('size'))
self.font_name='assets/overpass-mono-regular.otf'
#pass
class PostAuthorLayout(MDBoxLayout): pass
class PostAuthorLabel(MDLabel):
def __init__(self,**kwargs):
super().__init__(**kwargs)
self.bind(width=lambda s, w: s.setter('text_size')(s, (w, None)))
self.bind(texture_size=self.setter('size'))
self.font_name='assets/overpass-mono-regular.otf'
pass
class PostAuthorAvatar(AsyncImage): pass
2020-08-05 09:58:31 +00:00
class PostCard(MDCard):
2020-08-09 10:32:42 +00:00
def __init__(self, author = None, title = None, img_src = None, content = None):
2020-08-05 09:58:31 +00:00
super().__init__()
2020-08-09 10:32:42 +00:00
self.author = author
2020-08-06 12:55:26 +00:00
self.title = title
self.img_src = img_src
self.content = content
2020-08-09 10:32:42 +00:00
self.bind(minimum_height=self.setter('height'))
# pieces
author_section_layout = PostAuthorLayout()
author_label = PostAuthorLabel(text=self.author)
author_label.font_size = '28dp'
author_avatar = PostAuthorAvatar(source=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)
2020-08-06 12:55:26 +00:00
2020-08-09 10:32:42 +00:00
2020-08-06 12:55:26 +00:00
title = PostTitle(text=self.title)
2020-08-09 10:32:42 +00:00
# image = PostImage(source=self.img_src)
2020-08-06 12:55:26 +00:00
content = PostContent(text=self.content)
2020-08-09 10:32:42 +00:00
2020-08-06 12:55:26 +00:00
#content = PostContent()
2020-08-09 10:32:42 +00:00
# add to screen
2020-08-06 12:55:26 +00:00
self.add_widget(title)
2020-08-09 10:32:42 +00:00
# self.add_widget(image)
2020-08-06 12:55:26 +00:00
self.add_widget(content)
2020-08-09 10:32:42 +00:00
#self.add_widget(layout)
#####
2020-08-03 16:29:03 +00:00
2020-08-06 08:46:51 +00:00
2020-08-09 10:32:42 +00:00
#### LOGIN
2020-08-06 08:46:51 +00:00
class ProtectedScreen(MDScreen):
def on_pre_enter(self):
global app
2020-08-06 09:43:13 +00:00
if not app.is_logged_in():
2020-08-06 08:46:51 +00:00
app.root.change_screen('login')
2020-08-06 10:24:09 +00:00
class WelcomeScreen(ProtectedScreen): pass
2020-08-06 12:55:26 +00:00
class LoginScreen(MDScreen):
#def on_pre_enter(self):
# global app
# if app.is_logged_in():
# app.root.change_screen('feed')
pass
2020-08-06 08:46:51 +00:00
class PeopleScreen(ProtectedScreen): pass
2020-08-10 15:05:36 +00:00
class AddPostScreen(ProtectedScreen): pass
2020-08-06 08:46:51 +00:00
class EventsScreen(ProtectedScreen): pass
class MessagesScreen(ProtectedScreen): pass
class NotificationsScreen(ProtectedScreen): pass
2020-08-03 10:25:58 +00:00
2020-08-06 08:46:51 +00:00
class FeedScreen(ProtectedScreen):
2020-08-03 16:29:03 +00:00
def on_enter(self):
2020-08-05 09:58:31 +00:00
i=0
lim=5
2020-08-03 20:44:53 +00:00
with open('tweets.txt') as f:
2020-08-05 09:58:31 +00:00
for ln in f:
if ln.startswith('@') or ln.startswith('RT '): continue
i+=1
2020-08-03 20:44:53 +00:00
if i>lim: break
2020-08-05 09:58:31 +00:00
#post = Post(title=f'Marx Zuckerberg', content=ln.strip())
2020-08-10 15:05:36 +00:00
post = PostCard(
author='Marx Zuckerberg',
title='',
img_src='avatar.jpg',
content=ln.strip())
2020-08-05 09:58:31 +00:00
print(post)
root.ids.post_carousel.add_widget(post)
2020-08-06 12:55:26 +00:00
2020-08-06 08:46:51 +00:00
2020-08-09 10:32:42 +00:00
def get_tor_proxy_session():
session = requests.session()
# Tor uses the 9050 port as the default socks port
session.proxies = {'http': 'socks5://127.0.0.1:9050',
'https': 'socks5://127.0.0.1:9050'}
return session
def get_tor_python_session():
from torpy.http.requests import TorRequests
with TorRequests() as tor_requests:
with tor_requests.get_session() as s:
return s
from kivymd.font_definitions import theme_font_styles
2020-08-03 10:25:58 +00:00
class MainApp(MDApp):
2020-08-06 10:24:09 +00:00
title = 'Komrade'
2020-08-09 10:32:42 +00:00
#api = 'http://localhost:5555/api'
api = 'http://128.232.229.63:5555/api'
#api = 'http://komrades.net:5555/api'
2020-08-06 09:43:13 +00:00
logged_in=False
2020-08-06 10:24:09 +00:00
store = JsonStore('komrade.json')
2020-08-06 09:43:13 +00:00
login_expiry = 60 * 60 * 24 * 7 # once a week
#login_expiry = 5 # 5 seconds
2020-08-03 10:25:58 +00:00
2020-08-09 10:32:42 +00:00
def get_session(self):
return get_tor_proxy_session()
#return get_tor_python_session()
2020-08-03 10:25:58 +00:00
def build(self):
2020-08-09 10:32:42 +00:00
# bind
2020-08-03 16:29:03 +00:00
global app,root
2020-08-03 15:48:47 +00:00
app = self
2020-08-03 16:29:03 +00:00
self.root = root = Builder.load_file('main.kv')
2020-08-06 12:55:26 +00:00
# edit logo
logo=root.ids.toolbar.ids.label_title
logo.font_name='assets/Strengthen.ttf'
logo.font_size='58dp'
logo.pos_hint={'center_y':0.43}
# icons
icons=root.ids.toolbar.ids.right_actions.children
for icon in icons:
#log(dir(icon))
#icon.icon='android' #user_font_size='200sp'
icon.font_size='58dp'
icon.user_font_size='58dp'
2020-08-10 15:05:36 +00:00
icon.width='58dp'
icon.size_hint=(None,None)
icon.height='58dp'
2020-08-06 13:02:26 +00:00
2020-08-06 09:43:13 +00:00
if not self.is_logged_in():
self.root.change_screen('login')
else:
2020-08-06 12:55:26 +00:00
self.root.change_screen('feed')
2020-08-03 16:29:03 +00:00
return self.root
2020-08-03 10:25:58 +00:00
2020-08-06 09:43:13 +00:00
def is_logged_in(self):
if self.logged_in: return True
if not self.store.exists('user'): return False
if self.store.get('user')['logged_in']:
if time.time() - self.store.get('user')['logged_in_when'] < self.login_expiry:
self.logged_in=True
return True
return False
def do_login(self):
self.logged_in=True
self.store.put('user',logged_in=True,logged_in_when=time.time())
2020-08-06 12:55:26 +00:00
self.root.change_screen('feed')
2020-08-06 09:43:13 +00:00
2020-08-03 10:25:58 +00:00
2020-08-06 08:46:51 +00:00
def login(self,un,pw):
2020-08-09 10:32:42 +00:00
url = self.api+'/login'
2020-08-06 08:46:51 +00:00
2020-08-09 10:32:42 +00:00
with self.get_session() as sess:
#res = requests.post(url, json={'name':un, 'passkey':pw})
res = sess.post(url, json={'name':un, 'passkey':pw})
if res.status_code==200:
self.do_login()
else:
self.root.ids.login_status.text=res.text
2020-08-06 08:46:51 +00:00
def register(self,un,pw):
2020-08-09 10:32:42 +00:00
url = self.api+'/register'
with self.get_session() as sess:
#res = requests.post(url, json={'name':un, 'passkey':pw})
res = sess.post(url, json={'name':un, 'passkey':pw})
if res.status_code==200:
self.do_login()
else:
self.root.ids.login_status.text=res.text
2020-08-06 08:46:51 +00:00
2020-08-03 10:25:58 +00:00
if __name__ == '__main__':
App = MainApp()
App.run()