LiveParise/main.c

236 lines
6.6 KiB
C

#include <cJSON.h>
#include <curl/curl.h>
#include <curl/easy.h>
#include <pthread.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define REGEN_ENC_URL_F(u, timestamp) \
{ \
sprintf(u, ENC_URL, SUBROOMID, timestamp, SS); \
}
#define REGEN_URL_F(u, enc, timestamp) \
{ \
sprintf(u, URL, SUBROOMID, enc, timestamp); \
}
#ifndef NDEBUG
#define DPRINT(s, __VA_ARGS__) \
{ \
printf(s, __VA_ARGS__); \
}
#else
#define DPRINT(s, ...)
#endif
struct memory
{
char *response;
size_t size;
};
struct enc
{
char *enc;
long long timestamp;
};
CURLcode curl_get(CURL *, const char *, struct memory *);
size_t cb(char *, size_t, size_t, void *);
struct enc get_enc(CURL *);
void parise(void *);
void exit_handler();
const char ENC_URL[] = "https://lbapi-rk.chaoxing.com/lb/parise/enc/get?subRoomId=%s&timestamp=%llu&ss=%s";
const char URL[] = "https://zhibo.chaoxing.com/apis/live/setLivePariseCountByEnc?subroomId=%s&enc=%s&timestamp=%llu";
const char SUBROOMID[] = "381236458034402305";
const char SS[] = "381237373432790016";
const char DNS[] = "8.8.8.8:53,114.114.114.114:53";
static int flag = 1;
int main(int argc, char *argv[])
{
int thread;
#ifdef WIN32
SetConsoleOutputCP(CP_UTF8);
#endif
if (argc < 2)
thread = 4;
else
thread = atoi(argv[1]);
curl_global_init(CURL_GLOBAL_ALL);
printf("线程\t状态\t点赞量\t(按下回车键退出)\n");
pthread_t *thread_pool = malloc(sizeof(pthread_t) * thread);
for (int i = 0; i < thread; i++)
{
if (i == thread - 1)
pthread_create(&thread_pool[i], NULL, (void *)&exit_handler, NULL);
else
pthread_create(&thread_pool[i], NULL, (void *)&parise, (void *)i);
}
for (int i = 0; i < thread; i++)
pthread_join(thread_pool[i], NULL);
free(thread_pool);
curl_global_cleanup();
return 0;
}
CURLcode curl_custom_init(CURL **curl, struct curl_slist **headers)
{
*curl = curl_easy_init();
if (*curl == NULL)
return CURLE_FAILED_INIT;
*headers = curl_slist_append(*headers, "Accept: application/json, text/javascript, */*; q=0.01");
*headers =
curl_slist_append(*headers, "User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like "
"Gecko) Chrome/128.0.0.0 Safari/537.36 Edg/128.0.0.0");
curl_easy_setopt(*curl, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(*curl, CURLOPT_DNS_SERVERS, DNS);
curl_easy_setopt(*curl, CURLOPT_USE_SSL, true);
curl_easy_setopt(*curl, CURLOPT_WRITEFUNCTION, cb);
return CURLE_OK;
}
CURLcode curl_get(CURL *curl, const char *const url, struct memory *chunk)
{
int status;
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)chunk);
status = curl_easy_perform(curl);
if (status != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(status));
return status;
}
long long current_timestamp()
{
const time_t timestamp = time(NULL);
srand(timestamp);
return timestamp * 1000 + (rand() % 1999 - 999); // -999 ~ 999
}
size_t cb(char *data, size_t size, size_t nmemb, void *clientp)
{
size_t realsize = size * nmemb;
struct memory *mem = (struct memory *)clientp;
char *ptr = realloc(mem->response, mem->size + realsize + 1);
if (!ptr)
return 0; /* out of memory! */
mem->response = ptr;
memcpy(&mem->response[mem->size], data, realsize);
mem->size += realsize;
mem->response[mem->size] = 0;
return realsize;
}
struct enc get_enc(CURL *curl)
{
char url[200];
struct enc ret = {0};
struct memory data = {0};
const cJSON *json = NULL;
cJSON *data_json = NULL;
ret.timestamp = current_timestamp();
REGEN_ENC_URL_F(url, ret.timestamp);
if (curl_get(curl, url, &data) != CURLE_OK)
return ret;
DPRINT("%s\n", data.response);
data_json = cJSON_Parse(data.response);
json = cJSON_GetObjectItemCaseSensitive(data_json, "enc");
const size_t s = strlen(json->valuestring);
ret.enc = malloc(s + 1);
memcpy(ret.enc, json->valuestring, s + 1);
free(data.response);
cJSON_Delete(data_json);
return ret;
}
// {'data': 293687, 'msg': '操作成功', 'result': 1}
void parise(void *arg)
{
const int idx = (int)arg;
CURL *curl = NULL;
struct curl_slist *headers = NULL;
if (curl_custom_init(&curl, &headers) != CURLE_OK)
{
fprintf(stderr, "T%d: curl_custom_init() failed\n", idx);
return;
}
while (flag)
{
char url[200];
struct memory json_c = {0};
struct enc enc;
const cJSON *result = NULL;
const cJSON *msg = NULL;
const cJSON *data = NULL;
const cJSON *errorMsg = NULL;
cJSON *json = NULL;
if ((enc = get_enc(curl)).enc == NULL)
continue;
REGEN_URL_F(url, enc.enc, enc.timestamp);
if (curl_get(curl, url, &json_c) != CURLE_OK)
goto clean;
json = cJSON_Parse(json_c.response);
result = cJSON_GetObjectItemCaseSensitive(json, "result");
msg = cJSON_GetObjectItemCaseSensitive(json, "msg");
data = cJSON_GetObjectItemCaseSensitive(json, "data");
errorMsg = cJSON_GetObjectItemCaseSensitive(json, "errorMsg");
if (result->valueint == 1)
printf("\rT%d\t%s\t%d", idx, msg->valuestring, data->valueint);
else
fprintf(stderr, "\rT%d\t%s", idx, errorMsg->valuestring);
fflush(stdout);
clean:
free(enc.enc);
free(json_c.response);
cJSON_Delete(json);
}
curl_easy_cleanup(curl);
curl_slist_free_all(headers);
printf("\rSignal Received: exit. T%d exiting\n", idx);
}
void exit_handler()
{
getchar();
flag = 0;
}