Images.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #include "Images.h"
  2. #include "../Scraping/ImageScraping.h"
  3. #include "../Utility/Unescape.h"
  4. #include "Config.h"
  5. int images_handler(UrlParams *params) {
  6. TemplateContext ctx = new_context();
  7. char *raw_query = "";
  8. int page = 1;
  9. if (params) {
  10. for (int i = 0; i < params->count; i++) {
  11. if (strcmp(params->params[i].key, "q") == 0) {
  12. raw_query = params->params[i].value;
  13. } else if (strcmp(params->params[i].key, "p") == 0) {
  14. int parsed = atoi(params->params[i].value);
  15. if (parsed > 1)
  16. page = parsed;
  17. }
  18. }
  19. }
  20. char page_str[16], prev_str[16], next_str[16];
  21. snprintf(page_str, sizeof(page_str), "%d", page);
  22. snprintf(prev_str, sizeof(prev_str), "%d", page > 1 ? page - 1 : 0);
  23. snprintf(next_str, sizeof(next_str), "%d", page + 1);
  24. context_set(&ctx, "query", raw_query);
  25. context_set(&ctx, "page", page_str);
  26. context_set(&ctx, "prev_page", prev_str);
  27. context_set(&ctx, "next_page", next_str);
  28. char *display_query = url_decode_query(raw_query);
  29. context_set(&ctx, "query", display_query);
  30. if (!raw_query || strlen(raw_query) == 0) {
  31. send_response("<h1>No query provided</h1>");
  32. if (display_query)
  33. free(display_query);
  34. free_context(&ctx);
  35. return -1;
  36. }
  37. ImageResult *results = NULL;
  38. int result_count = 0;
  39. if (scrape_images(raw_query, page, &results, &result_count) != 0 ||
  40. !results) {
  41. send_response("<h1>Error fetching images</h1>");
  42. free(display_query);
  43. free_context(&ctx);
  44. return -1;
  45. }
  46. char ***image_matrix = malloc(sizeof(char **) * result_count);
  47. int *inner_counts = malloc(sizeof(int) * result_count);
  48. if (!image_matrix || !inner_counts) {
  49. if (image_matrix)
  50. free(image_matrix);
  51. if (inner_counts)
  52. free(inner_counts);
  53. free_image_results(results, result_count);
  54. free(display_query);
  55. free_context(&ctx);
  56. return -1;
  57. }
  58. for (int i = 0; i < result_count; i++) {
  59. image_matrix[i] = malloc(sizeof(char *) * IMAGE_RESULT_FIELDS);
  60. image_matrix[i][0] = strdup(results[i].thumbnail_url);
  61. image_matrix[i][1] = strdup(results[i].title);
  62. image_matrix[i][2] = strdup(results[i].page_url);
  63. image_matrix[i][3] = strdup(results[i].full_url);
  64. inner_counts[i] = IMAGE_RESULT_FIELDS;
  65. }
  66. context_set_array_of_arrays(&ctx, "images", image_matrix, result_count,
  67. inner_counts);
  68. char *rendered = render_template("images.html", &ctx);
  69. if (rendered) {
  70. send_response(rendered);
  71. free(rendered);
  72. } else {
  73. send_response("<h1>Error rendering image results</h1>");
  74. }
  75. for (int i = 0; i < result_count; i++) {
  76. for (int j = 0; j < IMAGE_RESULT_FIELDS; j++)
  77. free(image_matrix[i][j]);
  78. free(image_matrix[i]);
  79. }
  80. free(image_matrix);
  81. free(inner_counts);
  82. free_image_results(results, result_count);
  83. free(display_query);
  84. free_context(&ctx);
  85. return 0;
  86. }