Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
B
briar-cli-python-demo
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Torsten Grote
briar-cli-python-demo
Commits
cdb4b463
Verified
Commit
cdb4b463
authored
6 years ago
by
Torsten Grote
Browse files
Options
Downloads
Patches
Plain Diff
Initial commit
parents
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
.gitignore
+1
-0
1 addition, 0 deletions
.gitignore
briar-cli.py
+67
-0
67 additions, 0 deletions
briar-cli.py
with
68 additions
and
0 deletions
.gitignore
0 → 100644
+
1
−
0
View file @
cdb4b463
.idea
This diff is collapsed.
Click to expand it.
briar-cli.py
0 → 100755
+
67
−
0
View file @
cdb4b463
#!/usr/bin/env python3
import
asyncio
import
json
import
threading
from
datetime
import
datetime
import
requests
import
websockets
HOST
=
'
localhost:7000
'
CONTACT_ID
=
'
1
'
URL
=
'
http://%s/v1/messages/%s
'
%
(
HOST
,
CONTACT_ID
)
URL_WS
=
'
ws://%s/v1/ws
'
%
HOST
PROMPT
=
'
$>
'
def
main
():
load_history
()
threading
.
Thread
(
target
=
get_message_stdin
).
start
()
loop
=
asyncio
.
get_event_loop
()
loop
.
create_task
(
get_message_websocket
())
loop
.
run_forever
()
loop
.
close
()
async
def
get_message_websocket
():
async
with
websockets
.
connect
(
URL_WS
)
as
ws
:
message
=
await
ws
.
recv
()
m
=
json
.
loads
(
message
)
if
m
[
'
name
'
]
==
'
org.briarproject.briar.api.messaging.event.PrivateMessageReceivedEvent
'
:
print
()
# line-break
print_message
(
m
[
'
data
'
])
print
(
PROMPT
,
end
=
''
,
flush
=
True
)
if
not
asyncio
.
get_event_loop
().
is_closed
():
asyncio
.
get_event_loop
().
create_task
(
get_message_websocket
())
def
get_message_stdin
():
while
True
:
body
=
input
(
PROMPT
)
if
len
(
body
)
>
0
:
print_message
(
requests
.
post
(
URL
,
data
=
{
'
message
'
:
body
}).
json
())
def
print_message
(
message
):
prefix
=
'
>
'
if
message
[
'
local
'
]
else
'
<
'
time
=
get_timestamp
(
datetime
.
fromtimestamp
(
message
[
'
timestamp
'
]
/
1000
))
print
(
f
"
{
time
}
{
prefix
}
{
message
[
'
body
'
]
}
"
)
def
get_timestamp
(
date
):
return
date
.
strftime
(
'
%Y-%m-%dT%H:%M:%S
'
)
def
load_history
():
history
=
requests
.
get
(
URL
).
json
()
for
message
in
history
:
print_message
(
message
)
if
__name__
==
"
__main__
"
:
main
()
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment