This repository has been archived by the owner on Apr 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
cmd_post.py
263 lines (218 loc) · 8.94 KB
/
cmd_post.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
"""
outputs evetns as they're seen from connected relays
"""
import logging
import sys
import time
from datetime import datetime, timedelta
from pathlib import Path
import getopt
from db.db import SQLiteDatabase
from nostr.ident.profile import Profile, ProfileList
from nostr.ident.persist import SQLProfileStore, MemoryProfileStore
from nostr.client.client import ClientPool, Client
# from nostr.client.persist import SQLEventStore, TransientEventStore
from nostr.event.persist import ClientSQLEventStore, ClientMemoryEventStore
from nostr.ident.event_handlers import ProfileEventHandler
from nostr.event.event_handlers import PersistEventHandler
from nostr.event.event import Event
from app.post import PostApp
from cmd_line.post_loop_app import PostAppGui
from nostr.util import util_funcs
# TODO: also postgres
WORK_DIR = '%s/.nostrpy/' % Path.home()
DB = SQLiteDatabase('%s/nostrpy-client.db' % WORK_DIR)
EVENT_STORE = ClientSQLEventStore(DB)
# EVENT_STORE = ClientMemoryEventStore()
# EVENT_STORE = TransientEventStore()
PROFILE_STORE = SQLProfileStore(DB)
# RELAYS = ['wss://rsslay.fiatjaf.com','wss://nostr-pub.wellorder.net']
# RELAYS = ['wss://rsslay.fiatjaf.com']
# RELAYS = ['ws://localhost:8081']
RELAYS = ['ws://localhost:8081']
# RELAYS = ['wss://nostr-pub.wellorder.net']
def usage():
print("""
usage:
""")
sys.exit(2)
def _get_profile(key, peh, err_str, create_type=ProfileList.CREATE_PRIVATE):
ret = peh.profiles.get_profile(key,
create_type=create_type)
if not ret:
print(err_str)
return ret
def do_post(client: Client,
post_app: PostApp,
msg):
client.start()
while not post_app.connection_status:
time.sleep(0.2)
post_app.do_post(msg)
client.end()
def show_post_info(as_user: Profile,
msg, to_users, is_encrypt, subject,
public_inbox: Profile):
if msg is None:
msg = '<no msg supplied>'
just = 10
print('from:'.rjust(just), as_user.display_name())
if to_users:
p: Profile
print('to:'.rjust(just), [p.display_name() for p in to_users])
if public_inbox:
print('via:'.rjust(just), public_inbox.display_name())
if subject:
print('subject:'.rjust(just), subject)
enc_text = 'encrypted'
if not is_encrypt:
enc_text = 'plain_text'
print('format:'.rjust(just), enc_text)
print('%s\n%s\n%s' % (''.join(['-'] * 10),
msg,
''.join(['-'] * 10)))
def run_post():
relays = RELAYS
as_user = None
is_encrypt = True
ignore_missing = False
is_loop = False
subject = None
peh = ProfileEventHandler(PROFILE_STORE)
to_users = []
public_inbox = None
try:
opts, args = getopt.getopt(sys.argv[1:], 'ha:t:piles:r:e:v:', ['help',
'relay=',
'as_profile=',
'plain_text',
'to=',
'via=',
'ignore_missing',
'loop',
'subject=',
'event='])
for o, a in opts:
if o in ('-i', '--ignore_missing'):
ignore_missing = True
if o in ('-e','--event'):
the_event = EVENT_STORE.get_filter({
'ids' : [a]
})
if not the_event:
print('no event found %s' % a)
sys.exit(2)
else:
to_users.append(peh.profiles.get_profile(the_event[0].pub_key,
create_type=ProfileList.CREATE_PUBLIC))
for c_pk in the_event[0].p_tags:
to_users.append(peh.profiles.get_profile(c_pk,
create_type=ProfileList.CREATE_PUBLIC))
# attempt interpret action
for o, a in opts:
if o in ('-h', '--help'):
usage()
elif o in ('-r', '--relay'):
relays = a.split(',')
elif o in ('-a', '--as_profile'):
as_user = _get_profile(a, peh, '--as_profile %s not found' % a)
if to_users:
if as_user in to_users:
to_users.remove(as_user)
elif o in ('-p', '--plain_text'):
is_encrypt = False
elif o in ('-t', '--to'):
for c_t in a.split(','):
to_add = _get_profile(c_t, peh, 'to profile %s not found' % c_t,
create_type=ProfileList.CREATE_PUBLIC)
if to_add:
to_users.append(to_add)
elif not ignore_missing:
print('to profile missing and ignore_missing not set')
sys.exit(2)
elif o in ('-v', '--via'):
public_inbox = _get_profile(a, peh, 'via public_inbox %s not found' % a)
if public_inbox is None:
print('via public inbox but it couldn\'t be created bad private key or unknown profile?')
sys.exit(2)
elif o in ('-s', '--subject'):
subject = a
elif o in ('-l', '--loop'):
is_loop = True
if not as_user and len(args) > 0:
a = args.pop(0)
as_user = _get_profile(a, peh, 'args[] %s not found' % a)
if not as_user:
print('no profile to post as supplied or unable to find')
sys.exit(2)
if not to_users and is_encrypt:
print('to users must be defined for encrypted posts')
sys.exit(2)
msg = None
if len(args) > 0:
msg = ' '.join(args)
my_client = ClientPool(relays)
my_post = PostApp(
use_relay=my_client,
as_user=as_user,
to_users=to_users,
is_encrypt=is_encrypt,
subject=subject,
public_inbox=public_inbox
)
if is_loop:
# for profile lookup
persist_profile = ProfileEventHandler(PROFILE_STORE)
# pre load local events
local_events = EVENT_STORE.get_filter({
'kind': [Event.KIND_TEXT_NOTE, Event.KIND_ENCRYPT],
'since': util_funcs.date_as_ticks(datetime.now() - timedelta(days=10))
})
# local_events.reverse()
for c_evt in local_events:
my_post.do_event(None, c_evt, None)
persist_event = PersistEventHandler(EVENT_STORE)
my_gui = PostAppGui(my_post,
profile_handler=persist_profile)
def my_connect(the_client: Client):
the_client.subscribe(filters={
'kind': [Event.KIND_META],
'since': EVENT_STORE.get_newest(the_client.url, {
'kind': [Event.KIND_META]
}) + 1
}, handlers=[persist_profile])
the_client.subscribe(filters={
# rem'd as if we're persisting locally it's best just to get everything else we're more likely
# to end up with gaps
'kind': [Event.KIND_TEXT_NOTE, Event.KIND_ENCRYPT],
'since': EVENT_STORE.get_newest(the_client.url, {
'kind': [Event.KIND_TEXT_NOTE, Event.KIND_ENCRYPT]
})+1
}, handlers=[my_post, persist_event])
con_status = my_client.connected
def on_status(status):
nonlocal con_status
if con_status != status['connected']:
con_status = status['connected']
my_gui.draw_messages()
my_client.set_on_connect(my_connect)
my_client.set_status_listener(on_status)
my_client.start()
my_gui.run()
my_client.end()
# my_post.run()
else:
if msg is None:
print('no message supplied!!!')
sys.exit(2)
else:
show_post_info(as_user, msg, to_users, is_encrypt, subject, public_inbox)
do_post(client=my_client,
post_app=my_post,
msg=msg)
except getopt.GetoptError as e:
print(e)
usage()
if __name__ == "__main__":
logging.getLogger().setLevel(logging.DEBUG)
run_post()