feeds.c (2246B)
1 #include "feeds.h" 2 3 #include <_string.h> 4 #include <mrss.h> 5 #include <stdlib.h> 6 #include <string.h> 7 8 #include "config.h" 9 #include "utils.h" 10 #include "db.h" 11 12 void fetch_feed(feed_t*, char*); 13 static void populate_feed(feed_t*); 14 15 app_t 16 load_app(char* contents) 17 { 18 app_t app = { 19 .feeds_cap = FEEDS_CAP, 20 .feeds = ecalloc(FEEDS_CAP, sizeof(char*)), 21 }; 22 23 remove_all_chars(contents, '\r'); 24 char* line = strtok(contents, "\n"); 25 26 while (line != NULL) { 27 feed_t* feed = (feed_t*)ecalloc(1, sizeof(feed_t)); 28 29 fetch_feed(feed, line); // gets the content and loads it into db 30 printf("."); 31 fflush(stdout); 32 33 populate_feed(feed); // fetch back from db top 100 34 35 if (app.feeds_cap == app.feeds_len) { 36 app.feeds_cap *= 2; 37 app.feeds = realloc(app.feeds, app.feeds_cap); 38 } 39 app.feeds[app.feeds_len++] = feed; 40 line = strtok(NULL, "\n"); 41 } 42 43 return app; 44 } 45 46 void 47 fetch_feed(feed_t* feed, char* url) 48 { 49 *feed = (feed_t) { .url = url, /*.posts_cap = POSTS_CAP*/ }; 50 51 static mrss_t* rss = NULL; 52 mrss_error_t rc = mrss_parse_url(url, &rss); 53 54 if (rc != MRSS_OK || rss == NULL) { 55 char short_url[FEEDS_CAP - 1]; 56 strncpy(short_url, url, FEEDS_CAP - 2); 57 short_url[FEEDS_CAP - 2] = '\0'; 58 asprintf(&feed->title, "%s%s", "(bad) ", short_url); 59 return; 60 } 61 62 feed->title = (rss->title != NULL) ? strndup(rss->title, FEED_CAP) : "Unknown feed"; 63 64 for (mrss_item_t* it = rss->item; it; it = it->next) { 65 char* title = (it->title && *it->title) ? it->title : ""; 66 char* link = (it->link && *it->link) ? it->link : ""; 67 char* comments = (it->comments && *it->comments) ? it->comments : ""; 68 char* desc = (it->description && *it->description) ? it->description : ""; 69 char* date = (it->pubDate && *it->pubDate) ? it->pubDate : ""; 70 // char* author = (it->author && *it->author) ? it->author : ""; 71 72 remove_all_tags(desc); 73 74 db_post_t db_post = { 75 .title = title, 76 .link = link, 77 .comments = comments, 78 .pub_date = date, 79 .summary = desc, 80 .feed_url = url, 81 }; 82 83 db_insert_post(db_post); 84 } 85 86 mrss_free(rss); 87 } 88 89 static void 90 populate_feed(feed_t* feed) 91 { 92 const char* url = feed->url; 93 db_fetch_post_t dbposts = db_fetch_posts(url); 94 if (!dbposts.success) return; 95 feed->posts = dbposts.posts; 96 feed->posts_len = dbposts.count; 97 }