I want to improve this program so I want you to criticize the code, and tell me what else can I do, and where can I find information about what I have to do.
TODO:
Fix
wget
timeoutThe code should always check the returned value from any of the
malloc
family of functions to assure to memory allocation was successfulthe code needs to check the returned value from
fopen()
to assure the operation was successfulThe
wget
command can fail, so thewget
line should routestderr
to a file, then aftersystem(cmd)
should open/read that file to assure thewget
was successfulThe
main()
function will not compile cleanly because the declaration (as it should) indicates are int returned value, but no return (someint
) line exists in the execution pathsIn the
download()
function, what is the magic number '151' about? Better to #define that number, with a descriptive name and a commentEegarding the makefile:
- cc := /usr/lib/gcc
- Globbing all the compiles and the link into a single line is not a good nor flexible plan. Suggest breaking out into a compile rule from one .c to one .o and then using the list of .o files on a separate rule that performs the link.
- The 'all' and 'clean' do not produce a file with those names. It suggests adding the line: .PHONY: all clean
#include <stdio.h>
#include <stdlib.h> // for using system calls
#include <stdbool.h>
#include <string.h> // for strlen
char postBegin[] = "forum-post-body-content", postEnd[] = "p-comment-notes", img[] = "img src=";
int length1 = 23, length2 = 15, length3 = 8;
int pos1 = 0, pos2 = 0, pos3 = 0;
void downloadAndOpen (FILE **fp, int i, char *file);
bool search (char needle[], int length, char c, int *pos);
void download (FILE* *fp);
int main ()
{
bool inPost = false;
FILE *fp;
int c;
char file[20];
for ( int i = 22; i <= 151; i++ )
{
downloadAndOpen (&fp, i, file);
while ( (c = getc (fp)) != EOF ) {
if ( search (postBegin, length1, c, &pos1) )
inPost = true;
if (inPost) {
if ( search (postEnd, length2, c, &pos2) )
inPost = false;
if ( search (img, length3, c, &pos3) )
download (&fp);
}
}
fclose (fp);
remove (file);
}
}
void downloadAndOpen (FILE **fp, int i, char *file)
{
char cmd[200]={0};
// download web page
sprintf (cmd, "wget -q -O page%d.txt 'http://www.mtgsalvation.com/forums/creativity/artwork/340782-official-digital-rendering-thread?page=%d'", i, i);
system (cmd);
// open text file
sprintf (file, "page%d.txt", i);
*fp = fopen (file, "r");
}
bool search (char needle[], int length, char c, int *pos)
{
if (needle[*pos] == c)
{
if (*pos == length - 1)
{
return true;
*pos = -1;
}
(*pos)++;
}
else
{
if(*pos > 0)
*pos = 0;
}
return false;
}
void download (FILE **fp)
{
char url[300], cmd[300];
static int imageNumber = 496; // The image number where I left last time
int pos = 0, c, j;
bool found = false;
while (!found)
{
if ( (c = getc (*fp)) == EOF ) {
printf ("Image not found\n");
return;
}
printf ("%c", (char) c);
url[pos] = (char) c;
if ( url[pos-1] == '\"' && url[pos] == '\"' )
break;
if ( url[pos-1] == '\"' && url[pos] == '>' )
{
printf ("\n");
found = true;
}
++pos;
}
pos -= 2;
char url2[pos];
for ( j = 1; j < pos; j++ )
{
url2[j - 1] = url[j];
}
url2[j - 1] = '\0';
//http://joequery.me/code/snprintf-c/
// wget -q for quiet -nc, --no-clobber skip downloads that would download to existing files (no sobreescribir)
snprintf(cmd, 300, "wget -q -nc --timeout=10 -O /home/arturo/Dropbox/Digital_Renders/%d \'%s\'", imageNumber++, url2);
system(cmd);
pos = 0;
}
i=1; while [ $i -le 150 ]; do wget -q -O- http://www.mtgsalvation.com/forums/creativity/artwork/340782-official-digital-rendering-thread?page=$i | sed -n '{/forum-post-body-content/,/p-comment-notes/p}' | grep -Po 'src="\K[^"]+' | xargs wget -q -T 6 -t 1; rm page$i; i=$[$i+1]; done
\$\endgroup\$