tscrape_html.c - tscrape - twitter scraper (not working anymore)
 (HTM) git clone git://git.codemadness.org/tscrape
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
       tscrape_html.c (4358B)
       ---
            1 #include <err.h>
            2 #include <stdio.h>
            3 #include <stdlib.h>
            4 #include <string.h>
            5 #include <time.h>
            6 
            7 #include "util.h"
            8 
            9 static struct feed **feeds;
           10 static int showsidebar;
           11 static char *line;
           12 static size_t linesize;
           13 static unsigned long totalnew;
           14 static time_t comparetime;
           15 
           16 static void
           17 printfeed(FILE *fp, struct feed *f)
           18 {
           19         char *fields[FieldLast];
           20         struct tm *tm;
           21         time_t parsedtime;
           22         unsigned int islink, isnew;
           23         ssize_t linelen;
           24 
           25         if (f->name[0]) {
           26                 fputs("<h2 id=\"", stdout);
           27                 xmlencode(f->name, stdout);
           28                 fputs("\"><a href=\"#", stdout);
           29                 xmlencode(f->name, stdout);
           30                 fputs("\">", stdout);
           31                 xmlencode(f->name, stdout);
           32                 fputs("</a></h2>\n", stdout);
           33         }
           34         fputs("<pre>\n", stdout);
           35 
           36         while ((linelen = getline(&line, &linesize, fp)) > 0) {
           37                 if (line[linelen - 1] == '\n')
           38                         line[--linelen] = '\0';
           39                 if (!parseline(line, fields))
           40                         break;
           41 
           42                 parsedtime = 0;
           43                 if (strtotime(fields[FieldUnixTimestamp], &parsedtime))
           44                         continue;
           45                 if (!(tm = localtime(&parsedtime)))
           46                         err(1, "localtime");
           47 
           48                 isnew = (parsedtime >= comparetime) ? 1 : 0;
           49                 islink = fields[FieldItemid][0] ? 1 : 0;
           50 
           51                 totalnew += isnew;
           52                 f->totalnew += isnew;
           53                 f->total++;
           54 
           55                 fprintf(stdout, "%04d-%02d-%02d&nbsp;%02d:%02d ",
           56                         tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
           57                         tm->tm_hour, tm->tm_min);
           58 
           59                 if (isnew)
           60                         fputs("<b><u>", stdout);
           61 
           62                 if (fields[FieldRetweetid][0]) {
           63                         fputs("<a href=\"https://mobile.twitter.com/", stdout);
           64                         xmlencode(fields[FieldItemUsername], stdout);
           65                         fputs("/status/", stdout);
           66                         xmlencode(fields[FieldRetweetid], stdout);
           67                         fputs("\">retweeted</a>", stdout);
           68                         fputs(" <a href=\"https://mobile.twitter.com/", stdout);
           69                         xmlencode(fields[FieldItemUsername], stdout);
           70                         fputs("\">@", stdout);
           71                         xmlencode(fields[FieldItemUsername], stdout);
           72                         fputs("</a> ", stdout);
           73                 }
           74 
           75                 if (islink) {
           76                         fputs("<a href=\"https://mobile.twitter.com/", stdout);
           77                         xmlencode(fields[FieldUsername], stdout);
           78                         fputs("/status/", stdout);
           79                         xmlencode(fields[FieldItemid], stdout);
           80                         fputs("\">", stdout);
           81                 }
           82                 xmlencode(fields[FieldText], stdout);
           83                 if (islink)
           84                         fputs("</a>", stdout);
           85                 if (isnew)
           86                         fputs("</u></b>", stdout);
           87 
           88                 fputs("\n", stdout);
           89         }
           90         fputs("</pre>\n", stdout);
           91 }
           92 
           93 int
           94 main(int argc, char *argv[])
           95 {
           96         struct feed *f;
           97         char *name;
           98         FILE *fp;
           99         int i;
          100 
          101         if (pledge(argc == 1 ? "stdio" : "stdio rpath", NULL) == -1)
          102                 err(1, "pledge");
          103 
          104         if (!(feeds = calloc(argc, sizeof(struct feed *))))
          105                 err(1, "calloc");
          106         if ((comparetime = time(NULL)) == -1)
          107                 err(1, "time");
          108         /* 1 day old is old news */
          109         comparetime -= 86400;
          110 
          111         fputs("<!DOCTYPE HTML>\n"
          112               "<html>\n"
          113               "\t<head>\n"
          114               "\t\t<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />\n"
          115               "\t\t<link rel=\"stylesheet\" type=\"text/css\" href=\"style.css\" />\n"
          116               "\t</head>\n"
          117               "\t<body class=\"noframe\">\n", stdout);
          118 
          119         showsidebar = (argc > 1);
          120         if (showsidebar)
          121                 fputs("\t\t<div id=\"items\">\n", stdout);
          122         else
          123                 fputs("\t\t<div id=\"items\" class=\"nosidebar\">\n", stdout);
          124 
          125         if (argc == 1) {
          126                 if (!(feeds[0] = calloc(1, sizeof(struct feed))))
          127                         err(1, "calloc");
          128                 feeds[0]->name = "";
          129                 printfeed(stdin, feeds[0]);
          130                 if (ferror(stdin))
          131                         err(1, "ferror: <stdin>:");
          132         } else {
          133                 for (i = 1; i < argc; i++) {
          134                         if (!(feeds[i - 1] = calloc(1, sizeof(struct feed))))
          135                                 err(1, "calloc");
          136                         name = ((name = strrchr(argv[i], '/'))) ? name + 1 : argv[i];
          137                         feeds[i - 1]->name = name;
          138                         if (!(fp = fopen(argv[i], "r")))
          139                                 err(1, "fopen: %s", argv[i]);
          140                         printfeed(fp, feeds[i - 1]);
          141                         if (ferror(fp))
          142                                 err(1, "ferror: %s", argv[i]);
          143                         fclose(fp);
          144                 }
          145         }
          146         fputs("</div>\n", stdout); /* div items */
          147 
          148         if (showsidebar) {
          149                 fputs("\t<div id=\"sidebar\">\n\t\t<ul>\n", stdout);
          150 
          151                 for (i = 1; i < argc; i++) {
          152                         f = feeds[i - 1];
          153                         if (f->totalnew > 0)
          154                                 fputs("<li class=\"n\"><a href=\"#", stdout);
          155                         else
          156                                 fputs("<li><a href=\"#", stdout);
          157                         xmlencode(f->name, stdout);
          158                         fputs("\">", stdout);
          159                         if (f->totalnew > 0)
          160                                 fputs("<b><u>", stdout);
          161                         xmlencode(f->name, stdout);
          162                         fprintf(stdout, " (%lu)", f->totalnew);
          163                         if (f->totalnew > 0)
          164                                 fputs("</u></b>", stdout);
          165                         fputs("</a></li>\n", stdout);
          166                 }
          167                 fputs("\t\t</ul>\n\t</div>\n", stdout);
          168         }
          169 
          170         fprintf(stdout, "\t</body>\n\t<title>Tweets (%lu)</title>\n</html>\n",
          171                 totalnew);
          172 
          173         return 0;
          174 }