-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadd_news_mysql.py
141 lines (100 loc) · 4.48 KB
/
add_news_mysql.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
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 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
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()
old_news_urls = list()
# scraping bourse press news and check news to not in the database and add new news to database
cnx = connect(user='mohammadali', password='M0h@mmadali',
host="localhost", database="bourse_news")
cursor = cnx.cursor()
query = ("SELECT news_url FROM news_contents")
cursor.execute(query)
for news_url in cursor:
old_news_urls.append(change_url.unquote(news_url[0]))
cursor.close()
cnx.close()
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("اقتصاد نیوز")
cnx = connect(user='mohammadali', password='M0h@mmadali',
host="localhost", database="bourse_news")
for news in range(len(confirmed_titles)):
cursor = cnx.cursor()
news_data = (confirmed_date_times[news], confirmed_titles[news], confirmed_images[news], confirmed_leads[news], confirmed_contents[news], confirmed_urls[news], confirmed_sources[news])
add_news = ("""INSERT INTO news_contents
(news_date_time, news_title, news_image, news_lead, news_content, news_url, news_source)
VALUES (%s, %s, %s, %s, %s, %s, %s)""")
# Insert new news
cursor.execute(add_news, news_data)
# Make sure data is committed to the database
cnx.commit()
cursor.close()
cnx.close()