-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.php
118 lines (108 loc) · 3.95 KB
/
cli.php
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
<?php
require_once( dirname( dirname( dirname( __DIR__ ) ) ) . '/wp-load.php' );
require_once( __DIR__ . '/class-pjw-groupme-rest-api.php' );
require_once( __DIR__ . '/class-pjw-groupme-wp-admin.php' );
require_once( __DIR__ . '/class-pjw-groupme-wp-api.php' );
function __parse_cli_args() {
$short_to_long = array(
'u:' => 'user-id:',
'g:' => 'group-id:',
'dry-run:',
'verbose:',
'action:'
);
$options = getopt( implode( '', array_keys( $short_to_long ) ), $short_to_long );
// Defaults
$parsed = array( 'dry-run' => true, 'verbose' => false );
$datatype = array( 'dry-run' => 'boolean', 'verbose' => 'boolean' );
foreach ( $short_to_long as $short => $long ) {
$short = rtrim( $short, ':' );
$long = rtrim( $long, ':' );
if ( isset( $options[$long] ) ) {
$parsed[$long] = $options[$long];
} elseif( isset( $options[$short] ) ) {
$parsed[$long] = $options[$short];
}
}
foreach ( $datatype as $key => $type ) {
if ( isset( $parsed[$key] ) && gettype( $parsed[$key] ) != $type ) {
switch ( $type ) {
case 'boolean':
$parsed[$key] = 'true' == $parsed[$key] ? true : false;
break;
case 'int':
$parsed[$key] = (int) $parsed[$key];
break;
}
}
}
return $parsed;
}
function __dispatch_request( $args ) {
if ( isset( $args['user-id'] ) && isset( $args['action'] )) {
wp_set_current_user( $args['user-id'] );
$_access_token = get_user_meta( get_current_user_id(), 'pjw_groupme_oauth_token', true );
$rest_api = new pjw_groupme_rest_api( $_access_token );
switch ( $args['action'] ) {
case 'list-groups':
$groups = $rest_api->groups();
foreach ( $groups as $group ) {
echo "{$group->name} - {$group->id} - {$group->messages->count} messages\n";
}
break;
case 'fetch-messages':
if ( isset( $args['group-id' ] ) ) {
$messages = $rest_api->messages( $args['group-id'] );
foreach ( $messages->messages as $message ) {
echo date( DATE_ISO8601, $message->created_at ) . " {$message->name}: {$message->text}\n";
}
} else {
echo "Usage: --user-id=X --action=fetch-messages --group-id=X ...\n\n";
}
break;
case 'walk-messages':
if ( isset( $args['group-id' ] ) ) {
// Hack use an after_id of 1 to get the first message in a group
$messages = $rest_api->messages( $args['group-id'], array( 'limit' => 100, 'after_id' => 1 ) );
while( !empty( $messages->messages ) ) {
foreach ( $messages->messages as $message ) {
echo date( DATE_ISO8601, $message->created_at ) . " {$message->name}: {$message->text}\n";
if ( count( $message->attachments ) ){
foreach( $message->attachments as $attachment ) {
echo "\t" . date( DATE_ISO8601, $message->created_at ) . " {$attachment->type} - {$attachment->url}\n";
}
}
}
$messages = $rest_api->messages( $args['group-id'], array( 'limit' => 100, 'after_id' => $message->id ) );
}
} else {
echo "Usage: --user-id=X --action=walk-messages --group-id=X ...\n\n";
}
break;
case 'sync-messages':
if ( isset( $args['group-id' ] ) ) {
// Find ID of last synced message
$pjw_groupme_wp_api = new pjw_groupme_wp_api();
$after_id = $pjw_groupme_wp_api->get_last_synced_message_id( $args['group-id'] );
if ( ! $after_id ) {
// Hack use an after_id of 1 to get the first message in a group
$after_id = 1;
}
$messages = $rest_api->messages( $args['group-id'], array( 'limit' => 100, 'after_id' => $after_id ) );
while( !empty( $messages->messages ) ) {
foreach ( $messages->messages as $message ) {
echo '.';
$pjw_groupme_wp_api->sync_message( $message );
}
$messages = $rest_api->messages( $args['group-id'], array( 'limit' => 100, 'after_id' => $message->id ) );
}
} else {
echo "Usage: --user-id=X --action=sync-messages --group-id=X ...\n\n";
}
break;
}
} else {
echo "Usage: --user-id=X --action=Y ...\n\n";
}
}
__dispatch_request( __parse_cli_args() );