-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
executable file
·140 lines (107 loc) · 3.81 KB
/
app.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
#!/usr/bin/env python3
from datetime import datetime
from dateutil.tz import tzlocal
from dateutil.tz.tz import tzutc
from geopy import distance
from geopy.point import Point
from pony.orm import Database, db_session
from pony.orm.core import ObjectNotFound, Optional, PrimaryKey, Required, select, desc
from pyrogram import Client, filters
from pyrogram.types import Location, Message
from pyrogram.types.user_and_chats.user import User
import quantumrandom as qr
ADMIN_ID = 202242124
client = Client('session')
db = Database()
class UserSetting(db.Entity):
id = PrimaryKey(int)
username = Optional(str)
distance = Required(int, default=3000)
created_at = Required(datetime, default=datetime.utcnow)
updated_at = Required(datetime, default=datetime.utcnow)
points_count = Required(int, default=0)
@db_session
def get_user(user: User) -> UserSetting:
try:
u = UserSetting[user.id]
if not u.username and user.username:
u.username = user.username
except ObjectNotFound:
u = UserSetting(id=user.id, username=user.username or '')
return u
@db_session
def update_points_count(user: User):
u = UserSetting[user.id]
u.points_count += 1
u.updated_at = datetime.utcnow()
@db_session
def change_distance(user: User, val: str):
d = None
u = get_user(user)
od = u.distance
if val:
try:
d = int(val)
except:
pass
if d is None or d <= 0 or d > 20_000_000:
return od
u.distance = d
u.updated_at = datetime.utcnow()
return d
@client.on_message(filters.regex(r'^\d+'))
def dist(_: Client, m: Message):
d = change_distance(m.from_user, m.text)
m.reply('Дистанция: %d м' % d)
@client.on_message(filters.command('distance'))
def dist_cmd(_: Client, m: Message):
"""old variant with distance set"""
d = change_distance(m.from_user, m.text[len('/distance'):])
m.reply('Дистанция: %d м' % d)
@client.on_message(filters.command(['start', 'help']))
def help(_: Client, m: Message):
u = get_user(m.from_user)
m.reply((
'🙋 Добро пожаловать в ваш Фатум!\n\n'
'Дистанция генерации: %d м.\n\n'
'Отправьте местоположение '
'для генерации новой точки '
'или отправьте желаемую дистанцию в метрах.'
) % (u.distance))
@client.on_message(filters.location)
def loc(c: Client, m: Message):
m.reply_chat_action('find_location')
user = m.from_user
u = get_user(user)
loc: Location = m.location
p = Point(loc.latitude, loc.longitude)
try:
d = qr.randint(500, u.distance) / 1000
angle = qr.randint(0, 360)
np = distance.distance(d).destination(p, angle)
m.reply_location(np.latitude, np.longitude)
c.send_message(m.from_user.id, 'В добрый путь!')
update_points_count(user)
except Exception as e:
m.reply('Error =(\n%s' % e)
@db_session
def get_stats():
total = UserSetting.select().count()
users = select((u.updated_at, u.points_count, u.username, u.id)
for u in UserSetting).order_by(lambda u, p, n, i: desc(u)).limit(10)
return ('Total: %s\n\n' % total) + '\n'.join(
(
'`%s %s` %s' % (
upd.replace(tzinfo=tzutc()).astimezone(
tzlocal()).strftime('%d.%m.%y %H:%M'),
pc,
('@%s' % un) if un else uid,
)
) for upd, pc, un, uid in users
)
@ client.on_message(filters.command('stats') & filters.chat(ADMIN_ID))
def stats(_: Client, m: Message):
m.reply_text(get_stats())
db.bind(provider='sqlite', filename='db.sqlite', create_db=True)
db.generate_mapping(create_tables=True)
client.run()