Created
September 11, 2018 00:59
-
-
Save nothings/5b23137414501066f986c5fd3cfdc4f5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define STB_DEFINE | |
#include "stb.h" | |
char *header; | |
typedef struct | |
{ | |
char *id; | |
char *html; | |
char *commentary; | |
char *category; | |
int rtcount; | |
int fvcount; | |
float preference; | |
} Tweet; | |
enum | |
{ | |
O_reverse_chronological, | |
O_chronological, | |
O_retweets, | |
O_likes, | |
O_preference, | |
O_commentary, | |
O__count | |
}; | |
Tweet *tweets; | |
void sort(int mode) | |
{ | |
int i,n; | |
n = stb_arr_len(tweets); | |
switch (mode) { | |
case O_chronological: | |
case O_commentary: | |
case O_reverse_chronological: qsort(tweets, n, sizeof(tweets[0]), stb_qsort_strcmp(offsetof(Tweet,id))); break; | |
case O_retweets: qsort(tweets, n, sizeof(tweets[0]), stb_intcmp(offsetof(Tweet,rtcount))); break; | |
case O_likes: qsort(tweets, n, sizeof(tweets[0]), stb_intcmp(offsetof(Tweet,fvcount))); break; | |
case O_preference: qsort(tweets, n, sizeof(tweets[0]), stb_floatcmp(offsetof(Tweet,preference))); break; | |
} | |
if (mode == O_chronological || mode == O_commentary) | |
return; | |
for (i=0; i < n/2; ++i) { | |
Tweet t = tweets[i]; | |
tweets[i] = tweets[n-1-i]; | |
tweets[n-1-i] = t; | |
} | |
} | |
char *tab_name[] = | |
{ | |
"Likes", | |
"Retweets", | |
"Chronological", | |
"movie_goofs' top 20", | |
"Commentary", | |
}; | |
char *mode_name[] = | |
{ | |
"Date\xe2\x86\x91", | |
"Date\xe2\x86\x93", | |
"Retweets", | |
"Likes", | |
"movie_goofs' top 20", | |
"Commentary", | |
}; | |
char *mode_url[] = | |
{ | |
"index.html", | |
"all_by_oldest.html", | |
"all_by_retweets.html", | |
"all_by_likes.html", | |
"all_by_preferred.html", | |
"commentary.html", | |
}; | |
static int tab_for_mode_table[] = { | |
0,0, 2,1,3, 4 | |
}; | |
int tab_for_mode(int m) | |
{ | |
return tab_for_mode_table[m]; | |
} | |
int mode_for_tab(int t) | |
{ | |
int i; | |
for (i=0; i < 6; ++i) | |
if (tab_for_mode_table[i] == t) | |
return i; | |
return 0; | |
} | |
void write_file(int mode) | |
{ | |
int i,tab = tab_for_mode(mode); | |
FILE *f; | |
// someday! | |
if (mode == O_commentary) | |
return; | |
f = fopen(mode_url[mode], "w"); | |
sort(mode); | |
fputs(header, f); | |
// write the tab header | |
//fprintf(f, "<div width='100%%'>\n"); | |
fprintf(f, "<div align=center style='max-width:500px;margin:auto'>\n"); | |
fprintf(f, "<div align=center>Sort by</div>\n"); | |
fprintf(f, "<table cellspacing=0 cellpadding=2 border=1 width='100%%'><tr>\n"); | |
for (i=0; i < 4; ++i) { | |
int m = mode_for_tab(i); | |
if (i == 0 && tab == 0) // reverse mode for hotlink | |
m = (O_chronological + O_reverse_chronological) - mode; | |
// don't hotlink if it's the current tab | |
if (i == tab && (i != 0)) | |
fprintf(f, "<td align=center>%s</td>\n", mode_name[m]); | |
else | |
fprintf(f, "<td align=center><a style=\"text-decoration:none\" href=\"%s\">%s</a></td>\n", mode_url[m], mode_name[m]); | |
} | |
fprintf(f, "</tr></table>\n"); | |
//fprintf(f, "</div>\n"); | |
if (mode == O_preference) | |
fprintf(f, "<p>My opinion is always changing, but at the moment, here's my twenty favorites.</p>\n"); | |
for (i=0; i < stb_arr_len(tweets); ++i) { | |
if (mode == O_commentary) { | |
if (tweets[i].commentary == NULL) continue; | |
if (tweets[i].commentary[0] == 0) continue; | |
} | |
fprintf(f, "<p>%s\n", tweets[i].html); | |
if (mode == O_commentary) { | |
fprintf(f, "%s\n<hr><p>", tweets[i].commentary); | |
} | |
if (mode == O_preference) | |
if (i >= 20) | |
break; | |
} | |
fprintf(f, "</div>\n"); | |
fprintf(f, "</body>\n</html>\n"); | |
fclose(f); | |
} | |
Tweet *find_tweet(char *id) | |
{ | |
static Tweet dummy; | |
int i; | |
for (i=0; i < stb_arr_len(tweets); ++i) | |
if (0==strcmp(id, tweets[i].id)) | |
return &tweets[i]; | |
return 0; | |
} | |
int main(int argc, char **argv) | |
{ | |
int i,n; | |
// read the source files | |
char **list = stb_stringfile("goof_tweets.csv", &n); | |
for (i=0; i < n; ++i) { | |
Tweet t = { 0 }; | |
char *s = list[i]; | |
char *a = s; | |
char *b = strchr(a, ',') + 1; | |
char *c = strchr(b, ',') + 1; | |
b[-1] = 0; | |
c[-1] = 0; | |
// a = id | |
// b = user_id | |
// c = body text | |
t.id = a; | |
if (0==strcmp(t.id, "1036025365334917120")) | |
continue; | |
t.html = stb_file(stb_sprintf("embed/%s.html", a), 0); | |
if (t.html) | |
stb_arr_push(tweets, t); | |
else | |
printf("Missing html file for %s\n", t.id); | |
} | |
// not actually comma-separated since it was easier to parse in Go with spaces | |
list = stb_stringfile("goof_stats.csv", &n); | |
for (i=0; i < n; ++i) { | |
char id[500]; | |
int fav,rt; | |
char *s = list[i]; | |
if (3==sscanf(s, "%s %d %d", id, &fav, &rt)) { | |
Tweet *t = find_tweet(id); | |
if (t) { | |
t->fvcount = fav; | |
t->rtcount = rt; | |
} | |
} | |
} | |
list = stb_stringfile("goofs.csv", &n); | |
for (i=2; i < n; ++i) { | |
char *s = list[i]; | |
if (isdigit(s[0])) { | |
Tweet *t; | |
int k; | |
char **tok = stb_tokens_quoted(s, ",", &k); | |
t = find_tweet(tok[0]); | |
if (t) { | |
t->category = strdup(tok[2]); | |
t->preference = (float) atof(tok[1]); | |
t->commentary = strdup(tok[3]); | |
} | |
//printf("%s - %s - %s - %s\n", tok[0], tok[1], tok[2], tok[4]); | |
free(tok); | |
} | |
} | |
header = stb_file("header.html", 0); | |
for (i=0; i < 6; ++i) | |
write_file(i); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment