Skip to content

Instantly share code, notes, and snippets.

@dtak1114
Created January 31, 2014 08:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dtak1114/8728453 to your computer and use it in GitHub Desktop.
Save dtak1114/8728453 to your computer and use it in GitHub Desktop.
#include "time.h"
#include <stdio.h>
#define EXPIRATION 3600*24
#define WAIT_TIME 10
typedef struct _url_list
{
char *url,*host;
int port;
time_t access_time;
struct _url_list *prev,*next;
} url_list_t;
/*s の先頭に iを連結して、リストの先頭のポインタを返す。*/
url_list_t *unshift_url_list(url_list_t *s, url_list_t **i);
/*s の先頭を切り離してポインタを返す。切り離されたリストの先頭はsに書き込まれる。*/
url_list_t *shift_url_list(url_list_t **s);
/* HTTP通信を行なって、t を収集してfdに格納。失敗すると0 を返す。*/
int get_an_url(const url_list_t *t, int fd);
/*t で指定されたURL からファイル名を生成してオープンし、fd を返す。*/
int store_file_open(const url_list_t *t);
/*fd に格納されたHTMLデータを解析してリンクのリストを返す。tは解析するHTMLデータのURLである。*/
url_list_t *parse_html(const url_list_t *t, int fd);
/*n で指定されたファイルを開いてURLを読み込みリストを返す。*/
url_list_t *file_to_queue(const char *n);
/*s で指定された秒数だけプログラムの動作を停止させる。*/
unsigned int sleep(unsigned int s);
/*t に現在時刻を格納する。*/
time_t time(time_t *t);
int main(int argc, char const *argv[])
{
url_list_t *queue = NULL, *history = NULL, *link = NULL, *item;
int fd;
time_t now,time_start,time_end;
/* ------ 課題ここから ------ */
queue = file_to_queue(argv[1]); /*シードをファイルから読み込み*/
while ((item = shift_url_list(&queue))) {
time(&time_start); // 収集速度制御用
/*itemが最近訪れたページだったら次のitem*/
for(url_list_t *u = history; u != NULL; u = u->next){
time(&now);
/*historyが空でない && historyのURLとitemのurlが同じ && EXPIRATION経ってない*/
if ((u != NULL) && (u->url == item->url) && (now - u->access_time < EXPIRATION)) break;
}
/* URLにアクセスを試みてfdに格納 */
if(get_an_url(item,fd)){
fd = store_file_open(item);
/* リンクをぶっこ抜いてqueueに足す */
link = parse_html(item,fd);
unshift_url_list(link,&queue);
/* historyに記録 */
if(history == NULL){
history = item;
}else{
history->next = item;
}
/*収集速度制御用*/
time(&time_end);
/*収集速度が 1/WAIT_TIME ページ/秒 を越えない様に制御する事。*/
/* == 1ページあたりWAIT_TIME秒より早くしない*/
if(time_end - time_start < WAIT_TIME)
sleep(WAIT_TIME-(time_end-time_start));
}
}
/* ------ 課題ここまで ------ */
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment