-
Notifications
You must be signed in to change notification settings - Fork 0
/
image_hunter.py
4016 lines (3097 loc) · 123 KB
/
image_hunter.py
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: UTF-8 -*-
#!/usr/bin/env python
import configparser
import mimetypes # needed for download functionality
import os
import shutil # to save it locally
import time
# Importing Necessary Modules
import urllib.request
import uuid
from distutils.command.config import dump_file
from platform import python_branch
from random import random
from re import I
from sqlite3 import Error
import nltk
import requests # to get image from the web
from bs4 import BeautifulSoup
from googlesearch import search
# nltk.download('stopwords')
from nltk.corpus import stopwords
from nturl2path import url2pathname
from selenium import webdriver as driver
# Session url and img global_memory
urls_global_session_memory = []
images_session_memory = []
current_root_url = ""
current_url = ""
current_title = ""
current_html = ""
current_text = ""
current_preview = ""
current_img_keywords = ""
current_http_src = ""
current_img_src = ""
current_query = ""
current_patterns_hits = ""
current_freq = []
current_img_size = 0
current_description = ""
current_image_alt_text_test = ""
current_long_keywords = ""
current_query_preview = ""
current_frequency_score = 0
current_nb_words_in_text = 0
current_logo = "http://thewowstyle.com/wp-content/uploads/2014/10/2620.jpg"
proxies = {
'http': '192.168.0.58:8080',
'https': '192.168.0.58:443',
}
media_files = "psd,apng,avif,bmp,gif,ico,cur,tif,tiff,jpg,jpeg,jfif,pjpeg,pjp,png,svg,webp,webm,ogg,tiff,ico,jpg,gif,png,bmp"
html_file_name = str(uuid.uuid4())
html_img_counter = 0
img_count = 0
url_count = 0
save_count = 0
spidered_site_count = 0
approch_counter = 0
query_size = 0
current_quality_score = 0
spam_score = 0
bonus_score = 0
# Program url buffers
urls_buffer = [] # long-term buffer =
urls_visited = [] # visited_links
urls_images = []
urls_problems = []
proxies_list = []
proxy_good = False
# search urls from google
search_urls = []
# SORRY STILL WORKING
# IN DEVELOPPEMENT
# Ideas to cheat intelligence
# adding specific keywords under the hood
# to get better searching and matching
search_boost_data = "photo image picture gallery wallpaper art digital svg jpg jpeg"
match_boost_data = "account capture image art blowup canvas capture film cartoon close-up design doodle drawing duplicate icon illustrate impression lens lookalike microfilm mug negative painting panorama photoengrave photograph pic picture pix portrait print replica image picture gallery pic art digital svg jpg jpeg web ogg gif png bmp camera capture carousel close-up digital camera zoom enlarge enlarger exposure filter fisheye focal image holographic holography kinetoscope low-resolution microfiche microfilm optical overexpose panorama pap paparazzi paparazzo photobomb photobombing photocall photogenic photograph photographer photographic photographically photography photojournalism photojournalist photoshoot pin-up snapshot retrovectors typeslab picsplosion debzebooks designschool.canva generatepress images.google inkscape openclipart pixabay pixnio pngtree publicdomainvectors rachelrofe unsplash visualhunt vwo wordmark cleanpng clipartmax dafont fontsquirrel instagram myfonts photopea pixsy"
search_boost = []
match_boost = []
match_boost_with_query = ""
pattern_memory = []
# keywords container for query
query_history = []
# Learning component
dictionary = []
synonyms = []
learned_relations = []
phrase_dictionary = []
spam_filter = []
code_filter = []
# Learning array for text the system reads
learned_keywords = []
from fake_useragent import UserAgent
# ua = UserAgent()
# Fake user agent
user_profil = []
# stop words and urls
stop_words = []
stop_urls = []
spidered_site_count = 0
configuration = configparser.ConfigParser()
configuration.read('config.env')
CLEAN_START = str(configuration.get('CONFIG', 'CLEAN_START'))
TIME_LOCK = float(configuration.get('CONFIG', 'TIME_LOCK'))
QUALITY_SCORE = float(configuration.get('CONFIG', 'QUALITY_SCORE'))
FREQ_SCORE = float(configuration.get('CONFIG', 'FREQ_SCORE'))
KEYWORDS_IN_LINK = str(configuration.get('CONFIG', 'KEYWORDS_IN_LINK'))
NUMBER_OF_KEYWORDS_IN_LINK = int(configuration.get(
'CONFIG', 'NUMBER_OF_KEYWORDS_IN_LINK'))
MIN_URL_SIZE = int(configuration.get('CONFIG', 'MIN_URL_SIZE'))
MAX_URL_SIZE = int(configuration.get('CONFIG', 'MAX_URL_SIZE'))
MAX_WORD_SIZE = int(configuration.get('CONFIG', 'MAX_WORD_SIZE'))
MIN_WORD_SIZE = int(configuration.get('CONFIG', 'MIN_WORD_SIZE'))
MIN_STEM_SIZE = int(configuration.get('CONFIG', 'MIN_STEM_SIZE'))
LONG_KEYWORD_SIZE = int(configuration.get('CONFIG', 'LONG_KEYWORD_SIZE'))
MIN_PATTERN_SIZE = int(configuration.get('CONFIG', 'MIN_PATTERN_SIZE'))
FREQUENCY_MIN_WORDS = int(configuration.get('CONFIG', 'FREQUENCY_MIN_WORDS'))
NB_OF_SITE_TO_SPIDER = int(configuration.get('CONFIG', 'NB_OF_SITE_TO_SPIDER'))
IMG_ALT_VERIFICATION = str(configuration.get('CONFIG', 'IMG_ALT_VERIFICATION'))
IMG_ALT_QS = int(configuration.get('CONFIG', 'IMG_ALT_QS'))
STEP_SCAN = int(configuration.get('CONFIG', 'STEP_SCAN'))
LEARNING_TFQST = int(configuration.get('CONFIG', 'LEARNING_TFQST'))
LEARN_TO_BLOCK_URL = int(configuration.get('CONFIG', 'LEARN_TO_BLOCK_URL'))
DOWNLOAD_EVERYTHING = str(configuration.get('CONFIG', 'DOWNLOAD_EVERYTHING'))
DEEP_ANALYSIS = str(configuration.get('CONFIG', 'DEEP_ANALYSIS'))
ANALYSIS_MAX_WORDS = int(configuration.get('CONFIG', 'ANALYSIS_MAX_WORDS'))
SPIDER_ALL_IMAGES = str(configuration.get('CONFIG', 'SPIDER_ALL_IMAGES'))
SEARCH_BOOST = str(configuration.get('CONFIG', 'SEARCH_BOOST'))
MATCH_BOOST = str(configuration.get('CONFIG', 'MATCH_BOOST'))
QUERY_BOOST = str(configuration.get('CONFIG', 'QUERY_BOOST'))
PREVIEW_SIZE = int(configuration.get('CONFIG', 'PREVIEW_SIZE'))
DOWNLOAD_HTML = str(configuration.get('CONFIG', 'DOWNLOAD_HTML'))
HTML_IMAGE_PER_PAGE = int(configuration.get('CONFIG', 'HTML_IMAGE_PER_PAGE'))
SAVE_CYCLE = int(configuration.get('CONFIG', 'SAVE_CYCLE'))
MIN_IMAGE_SIZE = int(configuration.get('CONFIG', 'MIN_IMAGE_SIZE'))
MAX_FILE_NAME_SIZE = int(configuration.get('CONFIG', 'MAX_FILE_NAME_SIZE'))
DEBUG_CONSOLE = str(configuration.get('CONFIG', 'DEBUG_CONSOLE'))
DEBUG_LOG = str(configuration.get('CONFIG', 'DEBUG_LOG'))
MAX_WORKSPACE_SIZE = int(configuration.get('CONFIG', 'MAX_WORKSPACE_SIZE'))
MIN_QUALITY_IMAGE_SIZE = int(configuration.get(
'CONFIG', 'MIN_QUALITY_IMAGE_SIZE'))
URL_LIMIT_CLEANING = int(configuration.get('CONFIG', 'URL_LIMIT_CLEANING'))
SAVE_SUBJECT_KEYWORDS = str(configuration.get(
'CONFIG', 'SAVE_SUBJECT_KEYWORDS'))
RANDOM_START = int(configuration.get('CONFIG', 'RANDOM_START'))
DIG_FOR_URLS = str(configuration.get('CONFIG', 'DIG_FOR_URLS'))
FREELY_GRAB_URLS = str(configuration.get('CONFIG', 'FREELY_GRAB_URLS'))
PROXY_ACTIVATED = 'OFF'
PROGRAM_PATH = str(configuration.get('CONFIG', 'PROGRAM_PATH'))
def init_program(query):
global query_size
global query_history
global current_query
# Query global_memory self-learning object
load_memory()
# User system-interaction
x_print("WELCOME TO Image-Miner")
x_print("Powered by Python")
#x_print("In memory of META-CRAWLER 1990")
x_print("Image-Miner is connected to Google, Yahoo and Bing")
x_print("Thank you for using Image-Miner")
user_input = 'n'
# clean urls lists
if (CLEAN_START == 'ON'):
#user_input = input("\nClean start? (y/n)")
user_input = 'y'
save_media()
# User system-interaction
x_print("SETUP PROCESS")
x_print("Starting setup process and cleanup")
setup_process(user_input)
if (query == ""):
query = input("What type of images you want to spider?")
query_size = str(len(query.split(" ")))
# THIS IS A PERSONAL CHOICE AND A TRICK TO GET
# MORE IMAGES IN THE SEARCH ITS NOT COMPLICATED
current_query = query
query = process_boost(query)
#Create HTML interface files
first_dump(current_query)
query_history = load_list("./intelligence/query_history", query_history)
query_history.insert(0, query)
query_history = save_list(query_history, "./intelligence/query_history")
x_print("\nSEARCH MODULE\n")
x_print("Connecting to Google, Yahoo and Bing")
x_print("This may take a while")
# MULTIPLE QUERIES & QUERY BOOST
if QUERY_BOOST == "ON":
search_urls = multiple_search_prototype(query)
else:
search_urls = get_search_urls(query)
search_urls = insert_in_buffer(search_urls)
def save_media():
global query_history
global current_query
try:
query_foler = current_query.replace(" ", "-")
if len(query_foler) < MIN_WORD_SIZE:
return None
MAX_FOLDER_LENGHT = 200
if len(query_foler) > MAX_FOLDER_LENGHT:
query_foler = query_foler[0:MAX_WORD_SIZE*3]
# Directory
directory = query_foler
# Parent Directory path
parent_dir = "./archives/media/"
# Path
path = os.path.join(parent_dir, directory)
# Create the directory
# 'GeeksForGeeks' in
isFile = os.path.isdir(path)
print(isFile)
if isFile == False:
os.mkdir(path)
except Exception as e:
print(e)
print("Exception saving archive")
pass
try:
save_to_archive("./media/", "./archives/media/" +
str(query_foler) + "/")
save_to_archive("./interface/", "./archives/interface/")
except Exception as e:
print(e)
print("Exception saving archive")
pass
print("SAVING IMAGES")
x_print("Images and media saved\n")
def extract_info_from_url(url):
x_print("\nSTARTING URL EXTRACTION PROCESS\n")
info = ""
url_clean = url.replace(".", " ")
url_clean = url_clean.replace("_", " ")
url_clean = url_clean.replace(",", "_")
url_clean = clean_url_html_tags(url_clean)
url_clean = clean_url(url_clean)
for i in url_clean.split("/"):
if len(i) == 0:
continue
i = clean_word(i)
i = clean_numbers(i)
i = check_stop_words(i)
info = info + " " + i
info = clean_text(info)
info = str(trim(info))
x_print("Concepts found in url ")
x_print(info)
return info
def process_boost(query):
global current_query
global match_boost
global search_boost
global match_boost_data
global search_boost_data
if SEARCH_BOOST == "ON":
query = query + " " + clean_text(search_boost_data)
search_boost.append(query)
search_boost = save_list(search_boost, "./intelligence/search_boost")
print("Just added search boost")
if MATCH_BOOST == "ON":
# CRITICAL ELEMENT FOR MATCHING
global match_boost_with_query
match_boost_data = clean_text(match_boost_data)
match_boost_with_query = query + " " + match_boost_data
match_boost.append(match_boost_with_query)
match_boost = save_list(match_boost, "./intelligence/match_boost")
print("Just added match boost")
print("Cleaning query")
query = clean_text(query)
return query
def clean_query(query):
items = [" ", ",", ".",
"-", "+", "{", "}", "[", "]", "(", ")"]
for item in items:
while query.find(item) >= 0:
query = query.replace(item, " ")
clean_query = ""
for word in query.split(" "):
word = clean_word(word)
clean_query = clean_query + " " + word
return clean_query
# EXPERIMENTAL MULTIPLE SEARCH_BOOST
# ADDING IMAGE RELATED GENERAL KEYWORDS
# TO GET MORE SEARCH RESULTS
def multiple_search_prototype(query):
# UNDER
# STILL NOT READY
# CURRENTLY TESTING AND DEBUGING
# The code is not optimize yet
# It dirty code so please dont
# be to hard on me...
# Normalize the query string
original_q = query.lower()
prototype_rq = query.lower()
query = query.lower()
# Prepare query with extra image termes
query = clean_query(query)
x_print("\nQUERY_BOOST ACTIVATED\n")
x_print("\nSending multiple queries")
x_print("with additional general image keywors\n")
x_print("THIS IS A LONG PROCESS")
x_print("PLEASE BE PATIENT\n")
# PROTOTYPE EXTRA Random QUERY 1 and 2
prototype_rq1 = " digital picture "
prototype_t = str(prototype_rq1).split(' ')
prototype_x = ""
import random
random.shuffle(prototype_t)
memory = []
for i in prototype_t:
if (not i in memory):
prototype_x = prototype_x + " " + i
memory.append(i)
prototype_rq1 = prototype_x + " " + query
query_history.append(prototype_rq1)
prototype_rq2 = " image "
prototype_t = str(prototype_rq2).split(' ')
prototype_x = ""
import random
random.shuffle(prototype_t)
memory = []
for i in prototype_t:
if (not i in memory):
prototype_x = prototype_x + " " + i
memory.append(i)
prototype_rq2 = query + " " + prototype_x
query_history.append(prototype_rq2)
prototype_rq3 = " picture "
prototype_t = str(prototype_rq3).split(' ')
prototype_x = ""
import random
random.shuffle(prototype_t)
memory = []
for i in prototype_t:
if (not i in memory):
prototype_x = prototype_x + " " + i
memory.append(i)
prototype_rq3 = prototype_x + " " + query
query_history.append(prototype_rq3)
prototype_rq4 = " photo "
prototype_t = str(prototype_rq4).split(' ')
prototype_x = ""
import random
random.shuffle(prototype_t)
memory = []
for i in prototype_t:
if (not i in memory):
prototype_x = prototype_x + " " + i
memory.append(i)
prototype_rq4 = query + " " + prototype_x
query_history.append(prototype_rq4)
# Add extra image related search terms to enhance
# the search results the list is in config.env
query = prepare_query(query)
search_urls = get_search_urls(trim_url(query))
print("QUERY: " + str(query))
query = prepare_query(prototype_rq1)
search_urls = get_search_urls(trim(prototype_rq1))
print("QUERY 2: " + str(prototype_rq1))
query = prepare_query(prototype_rq2)
search_urls = get_search_urls(trim(prototype_rq2))
print("QUERY 3: " + str(prototype_rq2))
query = prepare_query(prototype_rq3)
search_urls = get_search_urls(trim(prototype_rq3))
print("QUERY 4: " + str(prototype_rq3))
query = prepare_query(prototype_rq4)
search_urls = get_search_urls(trim(prototype_rq4))
print("QUERY 5: " + str(prototype_rq4))
# need to be properly programmed
original_q = original_q + " wallpaper"
query = prepare_query(original_q)
search_urls = get_search_urls(trim(original_q))
print("LAST QUERY: " + str(original_q))
relax(TIME_LOCK*5)
memory = []
#list_rq1 = check_search_urls(list_rq1)
#list_rq2 = check_search_urls(list_rq2)
#list_rq3 = check_search_urls(list_rq3)
#list_rq4 = check_search_urls(list_rq4)
#list_original = check_search_urls(list_original)
search_urls = check_search_urls(search_urls)
# for i in list_rq1:
# i = urllib.parse.unquote(i)
# insert_search_urls(i, prototype_rq1)
# print("inserting in DB: " + str(i))
# for i in list_rq2:
# i = urllib.parse.unquote(i)
# insert_search_urls(i, prototype_rq2)
# print("inserting in DB: " + str(i))
# for i in list_rq3:
# i = urllib.parse.unquote(i)
# insert_search_urls(i, prototype_rq3)
# print("inserting in DB: " + str(i))
# for i in list_rq4:
# i = urllib.parse.unquote(i)
# insert_search_urls(i, prototype_rq4)
# print("inserting in DB: " + str(i))
for i in search_urls:
i = urllib.parse.unquote(i)
insert_search_urls(i, original_q)
print("inserting in DB: " + str(i))
#print("Adding to search buffer")
#earch_urls = add_list_to_list(search_urls, list_rq1)
#search_urls = add_list_to_list(search_urls, list_rq2)
#search_urls = add_list_to_list(search_urls, list_rq3)
#search_urls = add_list_to_list(search_urls, list_rq4)
#search_urls = add_list_to_list(search_urls, list_original)
#print("Urls added to search buffer")
# System interaction with user
x_print("We got: " + str(len(search_urls)) + " urls to mine\n")
x_print("Thank you Google, Yahoo and Bing...")
# Inserting urls at the beginning of the url list
x_print("\nAdding the search urls to the begining of search buffer...")
#import random
# random.shuffle(search_urls)
# random.shuffle(search_urls)
return search_urls
def prepare_query(query):
query = query.replace(" ", "+")
return query
def check_critical_amount_of_urls(list_of_urls):
for url in list_of_urls:
result = verify_keywords_in_link(url)
if result == False:
list_of_urls.remove(url)
print("removing url: " + str(url))
return list_of_urls
def check_search_urls(list_of_urls):
for url in list_of_urls:
result = first_check(url)
if result == False:
list_of_urls.remove(url)
print("removing url: " + str(url))
return list_of_urls
def add_list_to_list(list_to_grow, list_to_add):
result_counter = 0
for i in list_to_add:
if not i in list_to_grow:
list_to_grow.append(i)
print(str(result_counter) + " : " + i)
result_counter = result_counter + 1
return list_to_grow
def insert_in_buffer(urls):
# Main url buffer
global urls_buffer
for i in urls:
if (len(i) < MIN_URL_SIZE):
continue
if (len(i) > MAX_URL_SIZE):
continue
if not i.find("http") >= 0:
continue
if not i in urls_buffer:
urls_buffer.insert(0, str(i))
urls_buffer = save_list(urls_buffer, "./intelligence/data/urls")
return urls_buffer
# save_memory()
# Self learning object to gain ai from
# user interaction - Prototype
def save_memory():
global urls_buffer
global urls_visited
global urls_images
global stop_urls
global dictionary
# shuffle_list(urls_buffer)
x_print("Saving memory")
urls_buffer = save_list(urls_buffer, "./intelligence/data/urls")
x_print("Visited sites memory saved")
urls_visited = save_list(urls_visited, "./intelligence/data/urls_visited")
x_print("Visited downloaded images memory saved")
urls_images = save_list(urls_images, "./intelligence/data/urls_images")
stop_urls = save_list(stop_urls, "./intelligence/stop_urls")
x_print("stop_urls saved")
x_print("dictionary saved")
x_print("learned_relations saved")
def load_memory():
global urls_buffer
global urls_images
global urls_visited
global query_history
global search_boost
global dictionary
global learned_relations
global learned_keywords, phrase_dictionary
global stop_urls
x_print("Loading memory")
urls_buffer = load_list("./intelligence/data/urls", urls_buffer)
x_print("Url memory loaded")
urls_visited = load_list("./intelligence/data/urls_visited", urls_visited)
x_print("Visited sites memory loaded")
urls_images = load_list("./intelligence/data/urls_images", urls_images)
x_print("Downloaded images memory loaded")
stop_urls = load_list("./intelligence/stop_urls", stop_urls)
dictionary = load_list("./intelligence/dictionary", dictionary)
learned_relations = load_list("./intelligence/learned_relations", learned_relations)
learned_keywords = load_list("./intelligence/learned_keywords", learned_keywords)
phrase_dictionary = load_list("./intelligence/phrase_dictionary", phrase_dictionary)
x_print("stop_urls loaded")
x_print("dictionary loaded")
x_print("learned_relations loaded")
x_print("learned_keywords loaded")
x_print("phrase_dictionary loaded")
def insert_url(url):
import sqlite3
conn = sqlite3.connect("./archives/data/data-miner.db")
c = conn.cursor()
c.execute("INSERT INTO urls (url) VALUES (?)", (url,))
conn.commit()
conn.close()
def insert_search_urls(url, query):
try:
import sqlite3
db_file = "./archives/data/data-miner.db"
conn = sqlite3.connect(db_file)
c = conn.cursor()
c.execute("INSERT INTO search (url, query) VALUES (?,?)",
(url, query))
conn.commit()
conn.close()
except Exception as e:
print(e)
def check_keywords_in_urls(url):
global query_history
for key in query_history[0].split(" "):
key = str(key).lower()
url = str(url).lower()
if len(key) >= MIN_WORD_SIZE:
if url.find(key) >= 0:
return True
return False
def insert_url_data(url, title, short_text):
global current_query
title = clean_text(title)
short_text = clean_text(short_text)
# To be able to search in the memory the
# query will be the key for the data
x_print("Learning keywords in the TITLE")
# key -> data
try:
db_file = "./archives/data/data-miner.db"
import sqlite3
conn = sqlite3.connect(db_file)
c = conn.cursor()
c.execute("INSERT INTO url (url, title, text) VALUES (?,?,?)",
(url, title, short_text))
conn.commit()
conn.close()
except Exception as e:
print(e)
def check_url(url):
if (url == None):
return False
if url.find(str(".html")) >= 0:
return True
if url.find(str(".pdf")) >= 0:
return False
if url.find(str(".php")) >= 0:
return True
if url.find(str(".asp")) >= 0:
return True
if url.find(str(".aspx")) >= 0:
return True
if url.find(str(".xml")) >= 0:
#download(url)
return False
if url.find(str(".xls")) >= 0:
#download(url)
return False
if url.find(str(".htm")) >= 0:
return True
if url.find(str(".zip")) >= 0:
#download(url)
return False
if url.find(str(".js")) >= 0:
return False
# Download intresting files
if url.find(str(".mp3")) >= 0:
#download(url)
return False
if url.find(str(".mp4")) >= 0:
# download_x(url)
return False
if url.find(str(".m3u")) >= 0:
# download_x(url)
return False
if url.find(str(".mkv")) >= 0:
# download_x(url)
return False
return True
def check_url_img(url):
if (url == None):
return False
if url.find(str(".html")) >= 0:
return False
if url.find(str(".pdf")) >= 0:
return False
if url.find(str(".php")) >= 0:
return False
if url.find(str(".asp")) >= 0:
return False
if url.find(str(".aspx")) >= 0:
return False
if url.find(str(".xml")) >= 0:
return False
if url.find(str(".xls")) >= 0:
return False
if url.find(str(".htm")) >= 0:
return False
if url.find(str(".zip")) >= 0:
return False
if url.find(str(".js")) >= 0:
return False
if url.find(str(".mp3")) >= 0:
download(url)
return False
return True
def check_media(url):
if url == None:
return False
check_url = url.lower()
if check_url.find(str(".html")) >= 0:
return False
if check_url.find(str(".pdf")) >= 0:
return False
if check_url.find(str(".php")) >= 0:
return False
if check_url.find(str(".asp")) >= 0:
return False
if check_url.find(str(".aspx")) >= 0:
return False
if check_url.find(str(".xml")) >= 0:
return False
if check_url.find(str(".xls")) >= 0:
return False
if check_url.find(str(".htm")) >= 0:
return False
if check_url.find(str(".zip")) >= 0:
return False
if check_url.find(str(".docx")) >= 0:
return False
if check_url.find(str(".doc")) >= 0:
return False
# There is a small bug with the end of the file
# this function should clean de url
for media_ext in media_files.split(","):
if check_url.find(str("."+media_ext.strip())) >= 0:
download(url)
return True
if DOWNLOAD_EVERYTHING == 'ON':
download(url)
return False
def clean_filename(file_name):
if (file_name.find("?") >= 0):
end = file_name.find("?")
file_name = file_name[0:end]
return file_name
def download(url):
global urls_global_session_memory
global img_count
global url_count
global current_quality_score
global current_url
global current_img_src
global current_img_keywords
global current_patterns_hits
save_path = './media/'
ext = url.split(".")[-1]
file = url.split("/")[-1]
file = clean_filename(file)
target = str(save_path) + str(file)
if url not in urls_global_session_memory:
current_url = url
try:
current_quality_score = 0
current_img_keywords = ""
current_patterns_hits = ""
x_print("First test the image URL")
x_print("Url: " + current_url)
result = url__img_verification(url)
if (result):
x_print("VERIFICATION PASSED...")
x_print("Second url test for image source")
#result = verify_keywords_in_link(url)
# if (not result):
# x_print("VERIFICATION Failed...")
# return None
x_print("Second test for image URL")
x_print("VERIFICATION PASSED...")
# NEED TO VERIFY KEYWORDS
if (result):
try:
target = get_image(url)
except Exception as e:
x_print(e)
try:
relax(TIME_LOCK)
import wget
wget.download(str(url), str(target))
except Exception as e:
x_print(e)
print("Exception with the requests...")
x_print("Download ERROR...\n")
return None
# Set the image source
current_img_src = url
x_print("For security reasons pacing at \n" +
str(TIME_LOCK) + " secondes...")
relax(TIME_LOCK)
else:
x_print("DOWNLOAD AI TESTS FAILED...\n")
return None
except Exception as e:
x_print(e)
x_print("Exception downloading")
x_print("URL causing error is: " + str(url) + "\n")
x_print("File download location: " + str(target) + "\n")
relax(TIME_LOCK)
if(1 < img_count):
img_count = img_count - 1
return None
# What ever site must be
# inserted in the images buffer
if (url in urls_buffer):
urls_buffer.remove(url)
img_count = img_count + 1
verify_image_size(str(target), str(url))
return True
def download_x(url):
save_path = './downloads/'
file = url.split("/")[-1]
file = clean_filename(file)
target = str(save_path) + str(file)
try:
target = get_image(url)
except Exception as e:
print(e)
x_print("Requests error...")
try:
import wget
wget.download(str(url), str(target))
except Exception as e:
x_print(e)
x_print("Exception backup wget downloading")
return False
return True
def extract_keywords(filename):
if filename == None:
return None
keywords = filename.replace("-", " ")
keywords = keywords.replace("/", " ")
keywords = keywords.replace(".", " ")
keywords = keywords.replace("http:", " ")
items = [" ", "+", "%", "&", "*", "_", "!", "@", "#", "$",
"(", ")", ".", "-", ".", "[", "]", "jpg", "jpeg", "gif", "bmp", "svg", "png"]
for item in items:
while keywords.find(item) >= 0:
keywords = keywords.replace(item, " ")
buffer = ""
for word in keywords.split(" "):
if word == None:
continue
if len(word) > MAX_WORD_SIZE:
continue
if len(word) < MIN_WORD_SIZE:
continue
if (word.isnumeric()):
continue
buffer = buffer + " " + word
buffer = clean_text(buffer)
keywords = str(buffer)
return keywords
def get_image(url):
if first_check(url) == False:
return None
# file name creating with timestamp
global current_img_keywords