-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadd_news_elasticsearch.py
147 lines (107 loc) · 4.33 KB
/
add_news_elasticsearch.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
import re
import csv
import time
import requests
import schedule
from bs4 import BeautifulSoup
from datetime import datetime
import urllib.parse as change_url
from mysql.connector import connect
from elasticsearch import Elasticsearch
from news_scraping.irna import irna_class
from news_scraping.sena import sena_class
from news_scraping.boursepress import bourse_press
from news_scraping.eghtesadnews import eghtesad_news
es = Elasticsearch()
active_sources = list()
cnx = connect(user="mohammadali", password="M0h@mmadali",
host="localhost", database="bourse_news")
cursor = cnx.cursor()
query = ("SELECT id, is_active FROM news_sources")
cursor.execute(query)
for id, is_active in cursor:
if is_active == 1:
active_sources.append(id)
cursor.close()
cnx.close()
test_news_urls = list()
news_url_take = es.search(index="news_contents", body=
{
"query": {"match_all": {}}
}
)
if len(news_url_take['hits']['hits']) > 0:
for hit in news_url_take['hits']['hits']:
test_news_urls.append("%(news_url)s" % hit["_source"])
old_news_urls = list()
for old_url in test_news_urls:
old_news_urls.append(change_url.unquote(old_url))
boursepress = bourse_press.output()
irna = irna_class.output()
sena = sena_class.output()
eghtesadnews = eghtesad_news.output()
confirmed_date_times = list()
confirmed_titles = list()
confirmed_images = list()
confirmed_leads = list()
confirmed_contents = list()
confirmed_urls = list()
confirmed_sources = list()
if 1 in active_sources:
for accepted in range(len(boursepress[-1])):
if change_url.unquote(boursepress[-1][accepted]) not in old_news_urls:
confirmed_date_times.append(boursepress[0][accepted])
confirmed_titles.append(boursepress[1][accepted])
confirmed_images.append(boursepress[2][accepted])
confirmed_leads.append(boursepress[3][accepted])
confirmed_contents.append(boursepress[4][accepted])
confirmed_urls.append(change_url.unquote(boursepress[-1][accepted]))
confirmed_sources.append("بورس پرس")
if 2 in active_sources:
for accepted in range(len(irna[-1])):
if change_url.unquote(irna[-1][accepted]) not in old_news_urls:
confirmed_date_times.append(irna[0][accepted])
confirmed_titles.append(irna[1][accepted])
confirmed_images.append(irna[2][accepted])
confirmed_leads.append(irna[3][accepted])
confirmed_contents.append(irna[4][accepted])
confirmed_urls.append(change_url.unquote(irna[-1][accepted]))
confirmed_sources.append("ایرنا")
if 3 in active_sources:
for accepted in range(len(sena[-1])):
if change_url.unquote(sena[-1][accepted]) not in old_news_urls:
confirmed_date_times.append(sena[0][accepted])
confirmed_titles.append(sena[1][accepted])
confirmed_images.append(sena[2][accepted])
confirmed_leads.append(sena[3][accepted])
confirmed_contents.append(sena[4][accepted])
confirmed_urls.append(change_url.unquote(sena[-1][accepted]))
confirmed_sources.append("سنا")
if 4 in active_sources:
for accepted in range(len(eghtesadnews[-1])):
if change_url.unquote(eghtesadnews[-1][accepted]) not in old_news_urls:
confirmed_date_times.append(eghtesadnews[0][accepted])
confirmed_titles.append(eghtesadnews[1][accepted])
confirmed_images.append(eghtesadnews[2][accepted])
confirmed_leads.append(eghtesadnews[3][accepted])
confirmed_contents.append(eghtesadnews[4][accepted])
confirmed_urls.append(change_url.unquote(eghtesadnews[-1][accepted]))
confirmed_sources.append("اقتصاد نیوز")
for i in range(len(confirmed_titles)):
data = {
"news_source":str(confirmed_sources[i]),
"news_url":str(confirmed_urls[i]),
"news_date_time":str(confirmed_date_times[i]),
"news_title":str(confirmed_titles[i]),
"news_image":str(confirmed_images[i]),
"news_lead":str(confirmed_leads[i]),
"news_contents":str(confirmed_contents[i]),
"is_duplicate":False,
"is_disable":False
}
add_data = es.index(index="news_contents", body=data)
res = es.search(index="news_contents", body=
{
"query": {"match_all": {}}
}
)