-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathScrapper.c
227 lines (198 loc) · 7 KB
/
Scrapper.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/* <DESC>
* Very simple HTTP GET
* </DESC>
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#include <ctype.h>
#include <curl/curl.h>
#include <unistd.h>
#include <sys/wait.h>
struct MemoryStruct {
char *memory;
size_t size;
};
static size_t WriteMemoryCallback(void *contents, size_t size, size_t nmemb, void *userp);
char * find_Ingredients(char * response);
char * find_Title(char * response);
//******MAIN******//
int main(int argc, char **argv)
{
CURL *curl;
CURLcode res;
struct MemoryStruct chunk;
chunk.memory = malloc(1); /* will be grown as needed by the realloc at the WriteMemoryCallback function*/
chunk.size = 0; /* no data at this point */
curl = curl_easy_init();
char * formated_Title;
char * ingredients_List;
char * url;
char opt;
bool Uses_file = false;
pid_t parent_pid;
FILE * in;
/* Catch Arguments to determine whether a single url will be used or a file with urls */
while((opt = getopt(argc, argv,":f:")) != -1)
{
switch (opt)
{
case 'f':
Uses_file = true;
in = fopen(optarg,"r");
break;
case '?':
printf("Usage: ./EverList [-f Text file with all the URLS, separated by new lines | single url]\n");
exit(2);
break;
}
}
/* if the option to use a file was chosen then it reads a line, creates a child process and waits for the child to finish */
if (Uses_file == true)
{
int child_index = 1;
url = malloc(255);
while (fgets(url,255,in) != NULL)
{
printf("Child %d:\n",child_index++);
url[strlen(url)-1] = '\0';
if (fork() == 0)
break;
else
parent_pid = wait(NULL);
}
}
else
{
url = malloc(strlen(argv[argc -1])+1);
strcpy(url,argv[argc -1]);
}
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, url); // Sets the url to the last given argument
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L); // Follows Redirects
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION,WriteMemoryCallback); // Sets the callback function to a function which always increases the memory allocated to our buffer
curl_easy_setopt(curl, CURLOPT_WRITEDATA,(void*) &chunk); // sets the buffer to which everything is returned to, equal to &chunk instead of stdout
curl_easy_setopt(curl, CURLOPT_USERAGENT, "libcurl-agent/1.0"); // Gives our request a user agent (some servers prefer it)
/* Perform the request, res will get the return code */
res = curl_easy_perform(curl);
/* Check for errors */
if(res != CURLE_OK){
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
return 1;
}
printf("%lu bytes retrieved\n",(unsigned long)chunk.size);
char * recipe_Title = find_Title(chunk.memory);
ingredients_List = find_Ingredients(chunk.memory);
formated_Title = malloc(strlen(recipe_Title) + strlen(url) + 128);
// sprintf(formated_Title,"<div class=\"para\"><span style=\"font-size: 20px;\" data-fontsize=\"true\"><a href=\"%s\" rev=\"en_rl_none\" class=\"en-link\"><u>%s</u></a></span></div>\n",argv[argc -1], recipe_Title);
sprintf(formated_Title,"<div><span style=\"font-size: 20px; data-fontsize=true\"><a href=\"%s\" rev=\"en_rl_none class=en-link\"><u>%s</u></a></span></div>\n",url, recipe_Title);
/* always cleanup */
curl_easy_cleanup(curl);
curl_global_cleanup();
free(chunk.memory);
free(recipe_Title);
}
printf("\n");
char * args[] = {"/opt/homebrew/bin/python3","./EvernotePy/Add_to_evernote.py",formated_Title,ingredients_List, NULL};
execv("/opt/homebrew/bin/python3",args);
free(ingredients_List);
free(formated_Title);
return 0;
}
static size_t
WriteMemoryCallback(void *contents, size_t size, size_t nmemb, void *userp)
{
size_t realsize = size * nmemb;
struct MemoryStruct *mem = (struct MemoryStruct *)userp;
char *ptr = realloc(mem->memory, mem->size + realsize + 1);
if(!ptr) {
/* out of memory! */
printf("not enough memory (realloc returned NULL)\n");
return 0;
}
mem->memory = ptr;
memcpy(&(mem->memory[mem->size]), contents, realsize);
mem->size += realsize;
mem->memory[mem->size] = 0;
return realsize;
}
char * find_Ingredients(char * response)
{
char string_to_search_for[] = "recipeIngredient";
int size = strlen(string_to_search_for);
char * response_Ingredients = strstr(response,string_to_search_for);
char * response_Ingredients_end = response_Ingredients;
char *ingredients_list = malloc(1);
int ingredients_list_size = 0;
char * html_div = malloc(256);
if (response_Ingredients != NULL){
printf("Found ingredients!\n");
response_Ingredients += size + 3;
while (1)
{
response_Ingredients_end++;
if(*response_Ingredients_end == ']')
break;
}
int len = response_Ingredients_end - response_Ingredients;
char *response_Ingredients_copy = (char*)malloc(sizeof(char)*(len + 1));
strncpy(response_Ingredients_copy,response_Ingredients,len);
response_Ingredients_copy[len] = '\0';
const char s[2] = "\"";
char *token;
/* get the first token */
token = strtok(response_Ingredients_copy, s);
int token_size = strlen(token) + 103;
ingredients_list_size += token_size;
ingredients_list = realloc(ingredients_list,ingredients_list_size);
ingredients_list[0] = '\0';
/* walk through other tokens */
while( token != NULL ) {
ingredients_list_size += strlen(token) + 72;
ingredients_list = realloc(ingredients_list,ingredients_list_size);
if (!strcmp(token,","))
strcat(ingredients_list,"\n");
else
{
// sprintf(html_div,"<div class=\"para\"><input type=\"checkbox\" checked=\"false\" class=\"en-todo\" contenteditable=\"false\">%s</div>", token);
sprintf(html_div,"<div><en-todo></en-todo><span style=\"font-size: 16px;\">%s</span></div>", token);
strcat(ingredients_list,html_div);
}
token = strtok(NULL, s);
}
strcat(ingredients_list,"\n");
free(response_Ingredients_copy);
free(html_div);
return ingredients_list;
}
printf("Couldn't find Ingredients\n");
exit(1);
}
char * find_Title(char * response)
{
char string_to_search_for[] = "<title>";
int size = strlen(string_to_search_for);
char * recipe_Title = strstr(response,string_to_search_for);
char * recipe_Title_end = recipe_Title; // As of now, the recipe_Title_end is the same as recipe Title. later it will parse the string
char * recipe_Title_copy; // and find where it ends
if (recipe_Title != NULL){
printf("Found Title!\n");
recipe_Title += size;
while (1)
{
recipe_Title_end++;
if(*recipe_Title_end == '<' || *recipe_Title_end == '|')// || strcmp(recipe_Title, "recipe") == 0 || strcmp(recipe_Title, "Recipe") == 0)
break;
}
int len = recipe_Title_end - recipe_Title;
recipe_Title_copy = (char*)malloc(sizeof(char)*(len+1));
strncpy(recipe_Title_copy, recipe_Title, len);
recipe_Title_copy[len] = '\0';
return recipe_Title_copy;
}
else
printf("Didn't Find the Title!\n");
exit(2);
}