125 行
2.8 KiB
C
125 行
2.8 KiB
C
#include <stdio.h>
|
|
#include <string.h>
|
|
#include <stdlib.h>
|
|
#include <curl/curl.h>
|
|
#include <unistd.h>
|
|
|
|
#include "stringex.h"
|
|
|
|
char* scanurl (char *url, int resnum) {
|
|
char *res;
|
|
char *pch;
|
|
int i = 0;
|
|
pch = strtok(url, ":/");
|
|
|
|
while (pch != NULL) {
|
|
i++;
|
|
if (i == resnum) res = pch;
|
|
pch = strtok(NULL, ":/");
|
|
}
|
|
|
|
return(res);
|
|
}
|
|
|
|
// TODO
|
|
// TODO: 画像等ファイルのダウンロード
|
|
// TODO: JS、動画等の削除
|
|
// TODO: 全部/usr/(local/)share/hozonsite/archiveに保存する
|
|
void scanpage (char *url, char *path) {
|
|
chdir(path);
|
|
FILE *file = fopen("index.html", "r");
|
|
char line[2000];
|
|
char *furl = url;
|
|
|
|
// URLの修正
|
|
char *rurl = scanurl(url, 2);
|
|
|
|
while (fgets(line, sizeof(line), file)) {
|
|
// TODO: JS
|
|
if (strstr(line, ".js")) {
|
|
printf("JS: %s\n", line);
|
|
}
|
|
if (strstr(line, "<script")) {
|
|
printf("スクリプト: %s\n", line);
|
|
}
|
|
|
|
// TODO: 画像
|
|
|
|
// CSS
|
|
if (strstr(line, ".css")) {
|
|
char* cut = str_split(line);
|
|
|
|
// パスの創作
|
|
char *link = malloc(strlen(url) + strlen("://") + strlen(rurl) + strlen(cut));
|
|
strcpy(link, url);
|
|
strcat(link, "://");
|
|
strcat(link, rurl);
|
|
strcat(link, cut);
|
|
|
|
// ファイル名
|
|
char *fn;
|
|
char *pchh;
|
|
pchh = strtok(cut, "/");
|
|
|
|
while (pchh != NULL) {
|
|
if (pchh != NULL) fn = pchh;
|
|
pchh = strtok(NULL, "/");
|
|
}
|
|
|
|
FILE *file2 = fopen(fn, "w");
|
|
CURL* curl = curl_easy_init();
|
|
CURLcode curlres;
|
|
if (curl) {
|
|
curl_easy_setopt(curl, CURLOPT_URL, link);
|
|
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
|
|
curl_easy_setopt(curl, CURLOPT_WRITEDATA, file2);
|
|
curlres = curl_easy_perform(curl);
|
|
curl_easy_cleanup(curl);
|
|
fclose(file2);
|
|
}
|
|
}
|
|
}
|
|
|
|
fclose(file);
|
|
}
|
|
|
|
int getpage (char *url, char *path) {
|
|
// フォルダに移動すると、index.htmlとして保存する
|
|
chdir(path);
|
|
FILE *file = fopen("index.html", "w");
|
|
if (file == NULL) {
|
|
// ファイルを開けなければ、エラーが出しますと、終了
|
|
perror("fopenエラー。");
|
|
return(-1);
|
|
}
|
|
|
|
CURL* curl = curl_easy_init();
|
|
CURLcode curlres;
|
|
|
|
// ウェブページを読み込む
|
|
if (curl) {
|
|
// CURLの実行
|
|
curl_easy_setopt(curl, CURLOPT_URL, url);
|
|
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
|
|
curl_easy_setopt(curl, CURLOPT_WRITEDATA, file);
|
|
|
|
// リターンコード
|
|
curlres = curl_easy_perform(curl);
|
|
|
|
// エラーの確認
|
|
if (curlres != CURLE_OK) {
|
|
fprintf(stderr, "curl_easy_perform()の実行に失敗しました。 %s\n", curl_easy_strerror(curlres));
|
|
return(-1);
|
|
}
|
|
|
|
// 掃除
|
|
curl_easy_cleanup(curl);
|
|
fclose(file);
|
|
|
|
// 性効!!あ、ごめん…成功!!
|
|
return(0);
|
|
}
|
|
|
|
return(-1);
|
|
}
|