本地CodeForces

#include <stdio.h>
#include <string.h>

#define MAXCMDLEN    200
#define MAXLINELEN    10000

int  LastDiv,NewDiv,MaxPage;
int  Div,IndexCnt;
char Cmd[MAXCMDLEN],Line[MAXLINELEN],Name[5],Png[MAXCMDLEN];
int  i,j,k,l,p,Len;
FILE *fIndex,*fTemp,*fExist,*fWrite;

void AddToIndex(FILE *file,int i,char *str)
{
    fprintf(file,"\t\t<td><a href=problemset/%d%s>%d%s</a></td>\n",i,str,i,str);
    ++IndexCnt;
    if((IndexCnt&7)==0)
    {
        fprintf(file,"\t</tr><tr>\n");
    }
}

void MakeHtml(int i,char *str)
{
    sprintf(Cmd,"problemset/%d%s",i,str);
    fWrite=fopen(Cmd,"w");
    sprintf(Cmd,"grep -n \"ttypography\\\"\" temp > linenum");
    system(Cmd);
    fTemp=fopen("linenum","r");
    fscanf(fTemp,"%d",&Len);
    fclose(fTemp);
    fTemp=fopen("temp","r");
    fprintf(fWrite,"<html>\n\t<body>\n<LINK charset=\"utf-8\" href=\"src/style.css\" rel=\"stylesheet\" type=\"text/css\"/>\n\n");
    for(l=0;l<Len;++l)
    {
        fgets(Line,MAXLINELEN,fTemp);
    }
    Len=strlen(Line);
    for(l=0;l<Len;++l)
    {
        if(l<Len-30)
        {
            if(Line[l]=='s'&&Line[l+1]=='r'&&Line[l+2]=='c')
            {
                fprintf(fWrite,"src=\"");
                l+=26;
                for(p=0;Line[l]!='"';++l,++p)
                {
                    Png[p]=Line[l];
                }
                Png[p]='\0';
                fprintf(fWrite,"%s\"",Png);
                sprintf(Cmd,"wget -q http://codeforces.ru/%s -O problemset/%s",Png,Png);
                system(Cmd);
            }
            else
            {
                fprintf(fWrite,"%c",Line[l]);
            }
        }
    }
    fprintf(fWrite,"\n");
    fprintf(fWrite,"\t</body>\n</html>");
    fclose(fWrite);
    fclose(fTemp);
}

int ProcessName(int i,char *str)
{
    sprintf(Cmd,"problemset/%d%s",i,str);
    fExist=fopen(Cmd,"r");
    if(fExist==NULL)
    {
        printf("Try to get %d%s.........",i,str);
        sprintf(Cmd,"curl -I -s http://codeforces.com/problemset/problem/%d/%s | grep Content-Length > temp",i,str);
        system(Cmd);
        fTemp=fopen("temp","r");
        fscanf(fTemp,"%*s %d",&Len);
        fclose(fTemp);
        if(Len!=0)
        {
            sprintf(Cmd,"wget -q http://codeforces.com/problemset/problem/%d/%s -O temp",i,str);
            system(Cmd);
            MakeHtml(i,str);
            AddToIndex(fIndex,i,str);
            printf("OK!\n");        
        }
        else
        {
            printf("Error,It's not exist!\n");
            return -1;
        }
    }
    else
    {
        AddToIndex(fIndex,i,str);
        fclose(fExist);
    }
    return 0;
}

void MakeProblemSet()
{
    system("touch ProblemSetIndex && rm ProblemSetIndex");
    fIndex=fopen("ProblemSetIndex","w");
    fprintf(fIndex,"<html>\n\t<body>\n\n<h1 align=center>CodeForces</h1><p>\n<table align=center width=\"90%\">\n\t<tr>\n");
    for(i=LastDiv;i<=NewDiv;++i)
    {
        if(i==177)
        {
            for(j=0;j<7;++j)
            {
                for(k=1;k<=2;++k)
                {
                    sprintf(Name,"%c%d\0",j+'A',k);
                    ProcessName(i,Name);
                }
            }
        }
        else if(i==178)
        {
            for(j=0;j<6;++j)
            {
                for(k=1;k<=3;++k)
                {
                    sprintf(Name,"%c%d\0",j+'A',k);
                    ProcessName(i,Name);
                }
            }
        }
        else if(i==207)
        {
            for(j=0;j<4;++j)
            {
                for(k=1;k<=3;++k)
                {
                    sprintf(Name,"%c%d\0",j+'A',k);
                    ProcessName(i,Name);
                }
            }
            for(k=4;k<=9;++k)
            {
                sprintf(Name,"%c%d\0",'D',k);
                ProcessName(i,Name);
            }
        }
        else
        {
            for(j=0;j<26;++j)
            {
                sprintf(Name,"%c\0",j+'A');
                if(ProcessName(i,Name)==-1)
                {
                    break;
                }
            }
        }
    }
    fprintf(fIndex,"\t</tr>\n</table>\n\n\t</body>\n</html>");
    fclose(fIndex);
}

char *strrev(char *str)
{
    char *p1,*p2;
    for(p1=str,p2=str+strlen(str)-1;p2>p1;++p1,--p2)
    {
        *p1^=*p2;
        *p2^=*p1;
        *p1^=*p2;
    }
    return str;
}

void MakeForNoSoul()
{
    IndexCnt=0;
    system("touch ForNoSoul && rm ForNoSoul");
    fIndex=fopen("ForNoSoul","w");
    fprintf(fIndex,"<html>\n\t<body>\n\n<h1 align=center>CodeForces For NoSoul</h1>\n");
    fprintf(fIndex,"<table align=center width=\"90%\">\n\t<tr>\n");
    for(i=1;i<=MaxPage;++i)
    {
        sprintf(Cmd,"wget -q http://codeforces.com/problemset/page/%d?order=BY_SOLVED_DESC -O temp",i);
        system(Cmd);
        fTemp=fopen("temp","r");
        for(j=0;j<700;++j)
        {
            fgets(Line,MAXLINELEN,fTemp);
        }
        while(fgets(Line,MAXLINELEN,fTemp)!=NULL)
        {
            Len=strlen(Line);
            if(Len>=15)
            {
                if(Line[Len-6]=='i'&&Line[Len-5]=='d'&&Line[Len-3]=='>')
                {
                    fgets(Line,MAXLINELEN,fTemp);
                    fgets(Line,MAXLINELEN,fTemp);
                    Len=strlen(Line);
                    for(k=0,j=Len-3;Line[j]!=' ';++k,--j)
                    {
                        Name[k]=Line[j];
                    }
                    Name[k]='\0';
                    strrev(Name);
                    sprintf(Cmd,"/home/nosoul/NoSoul.Love/MyProjects/MyCode/CodeForces/%s.c",Name);
                    fExist=fopen(Cmd,"r");
                    if(fExist==NULL)
                    {
                        printf("Add %s to NoSoul Index.\n",Name);    
                        sscanf(Name,"%d%s",&Div,Name);
                        AddToIndex(fIndex,Div,Name);
                    }
                }
            }
        }
        fclose(fTemp);
    }
    fprintf(fIndex,"\t</tr>\n</table>\n\n\t</body>\n</html>");
    fclose(fIndex);
}

int main(int argc,char *argv[])
{
    fExist=fopen("problemset/src/style.css","r");
    if(fExist==NULL)
    {
        system("mkdir problemset && mkdir problemset/src problemset/renderer");
        system("wget -q http://codeforces.ru/static/combine/ec1325e741305c9a1a85151d0f3e0565_9a4e89dff55c2bd439f2378c20e3710f.css -O problemset/src/style.css");
    }
    else
    {
        fclose(fExist);
    }
    int i;
    if(argc>1)
    {
        for(i=1;i<argc;++i)
        {
            if(strcmp(argv[i],"Set")==0)
            {
                LastDiv=atoi(argv[i+1]);
                NewDiv=atoi(argv[i+2]);
                printf("Start Make ProblemSet...\n");
                MakeProblemSet();
                system("rm linenum");
                printf("End Make ProblemSet.\n");
                i+=2;
            }
            else if(strcmp(argv[i],"NoSoul")==0)
            {
                MaxPage=atoi(argv[i+1]);
                printf("Start Make For NoSoul...\n");
                MakeForNoSoul();
                printf("End Make For NoSoul.\n");
                ++i;
            }
        }
    }
    system("rm temp");
    return 0;
}

 repo:https://github.com/NoSoul/LocalCodeForces

原文地址:https://www.cnblogs.com/NoSoul/p/3013067.html