flip_social_feed.h 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. #ifndef FLIP_SOCIAL_FEED_H
  2. #define FLIP_SOCIAL_FEED_H
  3. bool flip_social_get_feed()
  4. {
  5. // Get the feed from the server
  6. if (app_instance->login_username_logged_out == NULL)
  7. {
  8. FURI_LOG_E(TAG, "Username is NULL");
  9. return false;
  10. }
  11. char command[256];
  12. snprintf(command, 128, "https://www.flipsocial.net/api/feed/20/%s/", app_instance->login_username_logged_out);
  13. bool success = flipper_http_get_request_with_headers(command, "{\"Content-Type\":\"application/json\"}");
  14. if (!success)
  15. {
  16. FURI_LOG_E(TAG, "Failed to send HTTP request for feed");
  17. return false;
  18. }
  19. fhttp.state = RECEIVING;
  20. return true;
  21. }
  22. bool flip_social_parse_json_feed()
  23. {
  24. if (fhttp.received_data == NULL)
  25. {
  26. FURI_LOG_E(TAG, "No data received.");
  27. return false;
  28. }
  29. // Remove newlines
  30. char *pos = fhttp.received_data;
  31. while ((pos = strchr(pos, '\n')) != NULL)
  32. {
  33. *pos = ' ';
  34. }
  35. // Initialize feed count
  36. flip_social_feed.count = 0;
  37. // Iterate through the feed array
  38. for (int i = 0; i < MAX_FEED_ITEMS; i++)
  39. {
  40. // Parse each item in the array
  41. char *item = get_json_array_value("feed", i, fhttp.received_data, MAX_TOKENS);
  42. if (item == NULL)
  43. {
  44. break;
  45. }
  46. // Extract individual fields from the JSON object
  47. char *username = get_json_value("username", item, MAX_TOKENS);
  48. char *message = get_json_value("message", item, MAX_TOKENS);
  49. char *flipped = get_json_value("flipped", item, MAX_TOKENS);
  50. char *id = get_json_value("id", item, MAX_TOKENS);
  51. if (username == NULL || message == NULL || flipped == NULL || id == NULL)
  52. {
  53. FURI_LOG_E(TAG, "Failed to parse item fields.");
  54. free(item);
  55. continue;
  56. }
  57. // Store parsed values
  58. flip_social_feed.usernames[i] = username;
  59. flip_social_feed.messages[i] = message;
  60. flip_social_feed.is_flipped[i] = strstr(flipped, "true") != NULL;
  61. flip_social_feed.ids[i] = atoi(id);
  62. flip_social_feed.count++;
  63. free(item);
  64. }
  65. return true;
  66. }
  67. #endif // FLIP_SOCIAL_FEED_H