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
|
/* Copyright (C) 2025 awy <awy@awy.one>
sttorrent is free software: you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation,
either version 3 of the License, or (at your option) any later version.
sttorrent is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public
License along with sttorrent. If not, see
<https://www.gnu.org/licenses/>. */
#include <curl/curl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "../lib/cjson/cJSON.h"
struct MemoryStruct {
char *memory;
size_t size;
};
static size_t
WriteMemoryCallback(void *contents, size_t size, size_t nmemb, void *userp)
{
size_t realsize = size * nmemb;
struct MemoryStruct *mem = (struct MemoryStruct *)userp;
mem->memory = realloc(mem->memory, mem->size + realsize + 1);
if(mem->memory == NULL) {
/* out of memory! */
puts("not enough memory (realloc returned NULL)");
return 0;
}
memcpy(&(mem->memory[mem->size]), contents, realsize);
mem->size += realsize;
mem->memory[mem->size] = 0;
return realsize;
}
int
get_session_id(char id[75])
{
CURL* handle;
CURLcode res;
struct MemoryStruct chunk;
chunk.memory = malloc(1);
chunk.size = 0;
handle = curl_easy_init();
if (!handle) { return 1; }
curl_easy_setopt(handle, CURLOPT_URL, "http://localhost:9091/transmission/rpc");
curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
curl_easy_setopt(handle, CURLOPT_WRITEDATA, (void *)&chunk);
curl_easy_setopt(handle, CURLOPT_HEADERDATA, (void *)&chunk);
res = curl_easy_perform(handle);
if (res != CURLE_OK) { return 1; }
int nl = 0;
int idx = 0;
char buff[128] = {'\0'};
for (size_t i = 0; i < chunk.size; i++) {
if (nl > 3) { break; }
if (chunk.memory[i] == '\n') {
nl++;
}
if (nl == 3) {
buff[idx] = chunk.memory[i+1];
idx++;
}
}
/* cleanup \r\n garbage at the end of the buff*/
buff[strcspn(buff, "\r\n")] = 0;
strcpy(id, buff);
curl_easy_cleanup(handle);
if (chunk.memory)
free(chunk.memory);
return 0;
}
int
main(void)
{
char id[75];
CURL* handle;
CURLcode res;
cJSON *root, *arguments, *torrents;
int status_count[7] = {0};
struct MemoryStruct chunk;
struct curl_slist *headers = NULL;
const char *json = "{\"method\":\"torrent-get\",\"arguments\":{\"fields\":[\"status\", \"percentDone\"]}}";
curl_global_init(CURL_GLOBAL_ALL);
/* To get status from transmission we need to send
two CURL requests, first one to obtain session id. */
get_session_id(id);
chunk.memory = malloc(1);
chunk.size = 0;
handle = curl_easy_init();
if (!handle) { return 1; }
curl_easy_setopt(handle, CURLOPT_URL, "localhost:9091/transmission/rpc");
curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
curl_easy_setopt(handle, CURLOPT_WRITEDATA, (void *)&chunk);
headers = curl_slist_append(headers, "Content-Type: application/json");
headers = curl_slist_append(headers, "Accept: application/json");
headers = curl_slist_append(headers, id);
curl_easy_setopt(handle, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(handle, CURLOPT_POST, 1L);
curl_easy_setopt(handle, CURLOPT_POSTFIELDS, json);
res = curl_easy_perform(handle);
if (res != CURLE_OK) { return 1; }
/* JSON parsing */
root = cJSON_Parse(chunk.memory);
/* Cleanup */
curl_easy_cleanup(handle);
if (chunk.memory)
free(chunk.memory);
curl_global_cleanup();
arguments = cJSON_GetObjectItem(root, "arguments");
torrents = cJSON_GetObjectItem(arguments, "torrents");
if (cJSON_GetArraySize(torrents) == 0) {
cJSON_Delete(root);
return 0;
}
/* Counting statuses */
for (int i = 0; i < cJSON_GetArraySize(torrents); i++) {
cJSON *val = cJSON_GetArrayItem(torrents, i);
cJSON *status = cJSON_GetObjectItem(val, "status");
if (status) {
int s = status->valueint;
status_count[s]++;
}
}
for (int i = 0; i < 7; i++) {
if (status_count[i] == 0) { continue; }
switch (i) {
case 0: printf(" "); break;
case 3: printf(" "); break;
case 4: printf(" "); break;
case 5: printf(" "); break;
case 6: printf(" "); break;
default: break;
}
printf("%d", status_count[i]);
}
cJSON_Delete(root);
return 0;
}
|