Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Messenger #6

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 0 additions & 26 deletions IcoConverter/IconConverter.py

This file was deleted.

23 changes: 23 additions & 0 deletions Messenger v.0.1/receiver.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import requests
from datetime import datetime
import time


def print_messages(messages):
for message in messages:
dt = datetime.fromtimestamp(message['time'])
print(dt.strftime('%H:%M:%S'), message['name'])
print(message['text'] + '\n')


after = 0

while True:
response = requests.get(url='http://127.0.0.1:5000/messages',
params={'after': after})
messages = response.json()['messages']
if messages:
print_messages(messages)
after = messages[-1]['time']

time.sleep(1)
2 changes: 2 additions & 0 deletions Messenger v.0.1/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Flask==2.0.2
requests==2.27.1
8 changes: 8 additions & 0 deletions Messenger v.0.1/sender.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import requests

name = input('Введите имя: ')

while True:
text = input("Введите текст: ")
response = requests.post(url='http://127.0.0.1:5000/send',
json={'name': name, 'text': text})
72 changes: 72 additions & 0 deletions Messenger v.0.1/server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
from flask import Flask, request, abort
from time import time

app = Flask(__name__)
database = []
# Структура database
# {
# 'name': 'Имя',
# 'text': 'Текст сообщения.',
# 'time': Время когда отправлено сообщение
# },


@app.route("/")
def main():
return "Hello User! <a href='/send'>Send</a> " \
"<a href='/messages?after=0'>Messages</a> " \
"<a href='/status'>Status</a> "


@app.route("/status")
def status():
return {
'status': True,
'name': 'Messenger',
'time': time(),
'count_user': len(set(message['name'] for message in database))
}


@app.route("/send", methods=['POST'])
def send_message():
data = request.json
print(data)

name = data['name']
text = data['text']

if not isinstance(data, dict):
return abort(400)
if 'name' not in data or 'text' not in data:
return abort(400)

message = {
'name': name,
'text': text,
'time': time()
}

database.append(message)

return {'ok': True}


@app.route("/messages")
def get_message():
try:
after = float(request.args['after'])
except Exception as ex:
print(ex)
return abort(400)
messages = []

for message in database:
if message['time'] > after:
messages.append(message)

return {'messages': messages[:50]}


if __name__ == '__main__':
app.run()
64 changes: 64 additions & 0 deletions Messenger v.0.2/interface.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'interface.ui'
#
# Created by: PyQt5 UI code generator 5.15.6
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again. Do not edit this file unless you know what you are doing.


from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(570, 702)
font = QtGui.QFont()
font.setPointSize(8)
MainWindow.setFont(font)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.textEdit = QtWidgets.QTextEdit(self.centralwidget)
self.textEdit.setGeometry(QtCore.QRect(10, 640, 461, 41))
font = QtGui.QFont()
font.setPointSize(15)
self.textEdit.setFont(font)
self.textEdit.setObjectName("textEdit")
self.listWidget = QtWidgets.QListWidget(self.centralwidget)
self.listWidget.setGeometry(QtCore.QRect(10, 61, 551, 561))
font = QtGui.QFont()
font.setPointSize(12)
self.listWidget.setFont(font)
self.listWidget.setObjectName("listWidget")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(480, 640, 81, 41))
font = QtGui.QFont()
font.setPointSize(31)
self.pushButton.setFont(font)
self.pushButton.setObjectName("pushButton")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(10, 10, 291, 51))
font = QtGui.QFont()
font.setPointSize(31)
self.label.setFont(font)
self.label.setObjectName("label")
self.textEdit_2 = QtWidgets.QTextEdit(self.centralwidget)
self.textEdit_2.setGeometry(QtCore.QRect(290, 20, 271, 31))
font = QtGui.QFont()
font.setPointSize(12)
self.textEdit_2.setFont(font)
self.textEdit_2.setObjectName("textEdit_2")
MainWindow.setCentralWidget(self.centralwidget)

self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)

def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "Messenger"))
self.textEdit.setPlaceholderText(_translate("MainWindow", "Введите сообщение..."))
self.pushButton.setText(_translate("MainWindow", ">"))
self.label.setText(_translate("MainWindow", "Messenger"))
self.textEdit_2.setPlaceholderText(_translate("MainWindow", "Введите имя"))
113 changes: 113 additions & 0 deletions Messenger v.0.2/interface.ui
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>570</width>
<height>702</height>
</rect>
</property>
<property name="font">
<font>
<pointsize>8</pointsize>
</font>
</property>
<property name="windowTitle">
<string>Messenger</string>
</property>
<widget class="QWidget" name="centralwidget">
<widget class="QTextEdit" name="textEdit">
<property name="geometry">
<rect>
<x>10</x>
<y>640</y>
<width>461</width>
<height>41</height>
</rect>
</property>
<property name="font">
<font>
<pointsize>15</pointsize>
</font>
</property>
<property name="placeholderText">
<string>Введите сообщение...</string>
</property>
</widget>
<widget class="QListWidget" name="listWidget">
<property name="geometry">
<rect>
<x>10</x>
<y>61</y>
<width>551</width>
<height>561</height>
</rect>
</property>
<property name="font">
<font>
<pointsize>12</pointsize>
</font>
</property>
</widget>
<widget class="QPushButton" name="pushButton">
<property name="geometry">
<rect>
<x>480</x>
<y>640</y>
<width>81</width>
<height>41</height>
</rect>
</property>
<property name="font">
<font>
<pointsize>31</pointsize>
</font>
</property>
<property name="text">
<string>&gt;</string>
</property>
</widget>
<widget class="QLabel" name="label">
<property name="geometry">
<rect>
<x>10</x>
<y>10</y>
<width>291</width>
<height>51</height>
</rect>
</property>
<property name="font">
<font>
<pointsize>31</pointsize>
</font>
</property>
<property name="text">
<string>Messenger</string>
</property>
</widget>
<widget class="QTextEdit" name="textEdit_2">
<property name="geometry">
<rect>
<x>290</x>
<y>20</y>
<width>271</width>
<height>31</height>
</rect>
</property>
<property name="font">
<font>
<pointsize>12</pointsize>
</font>
</property>
<property name="placeholderText">
<string>Введите имя</string>
</property>
</widget>
</widget>
</widget>
<resources/>
<connections/>
</ui>
63 changes: 63 additions & 0 deletions Messenger v.0.2/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import interface
from PyQt5 import QtWidgets, QtCore
import requests
from datetime import datetime


class Messenger(QtWidgets.QMainWindow, interface.Ui_MainWindow):
def __init__(self):
super(Messenger, self).__init__()
self.setupUi(self)
self.after = 0
self.pushButton.clicked.connect(self.send_message)
self.timer = QtCore.QTimer()
self.timer.timeout.connect(self.get_messages)
self.timer.start(1000)

def show_messages(self, message):
item = QtWidgets.QListWidgetItem()
dt = datetime.fromtimestamp(message['time'])
item.setText(f"{message['name']} {dt.strftime('%H:%M')}\n{message['text']}")
self.listWidget.addItem(item)

def get_messages(self):
try:
response = requests.get(url='http://127.0.0.1:5000/messages', params={'after': self.after})
except Exception as e:
print(e)
return
messages = response.json()['messages']
for i in range(len(messages)):
self.show_messages(messages[i])
self.after = messages[i]['time']
self.listWidget.scrollToBottom()

def send_message(self):
name = self.textEdit_2.toPlainText()
text = self.textEdit.toPlainText()
if len(name) > 0 and len(text) > 0:
try:
response = requests.post(url='http://127.0.0.1:5000/send', json={'name': name, 'text': text})
except Exception as e:
item = QtWidgets.QListWidgetItem()
item.setTextAlignment(QtCore.Qt.AlignCenter)
item.setText('Сервер недоступен!\n')
self.listWidget.addItem(item)
print(e)
return

if response.status_code != 200:
item = QtWidgets.QListWidgetItem()
item.setTextAlignment(QtCore.Qt.AlignCenter)
item.setText('Неправильное имя или текст!\n')
self.listWidget.addItem(item)
return

self.textEdit.clear()


if __name__ == '__main__':
App = QtWidgets.QApplication([])
window = Messenger()
window.show()
App.exec()
6 changes: 6 additions & 0 deletions Messenger v.0.2/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Flask==2.0.2
requests==2.27.1
PyQt5==5.15.6
PyQt5-Qt5==5.15.2
PyQt5-sip==12.8.1
PyQt5-stubs==5.14.2.2
Loading