-
Notifications
You must be signed in to change notification settings - Fork 0
/
sync.py
143 lines (120 loc) · 3.96 KB
/
sync.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
# Stdlib imports
import urllib3
import json
from datetime import datetime
# Third-party app imports
import requests
import certifi
from requests.packages.urllib3.exceptions import InsecureRequestWarning
from elasticsearch import Elasticsearch, helpers
from newspaper import Article
from pymongo import MongoClient
# Imports from app
from middleware.config import (
ELASTICSEARCH_USER,
ELASTICSEARCH_PASSWORD,
CONTEXT_API_USERNAME,
CONTEXT_API_PASSWORD,
)
# Removing requests warning
urllib3.disable_warnings()
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
# MongoDB setup
client = MongoClient(connect=False)
db = client.elastic_sync
articles_collection = db.articles
# Context Setup
base_url = 'https://context.newsai.org/api'
# Elasticsearch setup
es = Elasticsearch(
['https://search.newsai.org'],
http_auth=(ELASTICSEARCH_USER, ELASTICSEARCH_PASSWORD),
port=443,
use_ssl=True,
verify_certs=True,
ca_certs=certifi.where(),
)
def get_login_token():
headers = {
"content-type": "application/json",
"accept": "application/json"
}
payload = {
"username": CONTEXT_API_USERNAME,
"password": CONTEXT_API_PASSWORD,
}
r = requests.post(base_url + "/jwt-token/",
headers=headers, data=json.dumps(payload), verify=False)
data = json.loads(r.text)
token = data.get('token')
return token
def check_if_article_in_es(url):
articles = es.search(index='articles', q='url:"' + url + '"')
if articles['hits']['total'] is 1:
print 'Found!'
return True
return False
def sync_articles_es(articles):
if articles:
to_append = []
has_completed_article = False
for article in articles:
doc = None
if not check_if_article_in_es(article['url']):
if articles_collection.find_one({'url': article['url']}) is None:
article_data = Article(article['url'])
article_data.download()
article_data.parse()
doc = {
'_type': 'article',
'_index': 'articles',
'title': article_data.title,
'text': article_data.text,
'url': article['url']
}
articles_collection.insert_one(doc)
else:
doc = articles_collection.find_one({'url': article['url']})
doc['_index'] = 'articles'
if '_id' in doc:
del doc['_id']
print doc
to_append.append(doc)
else:
has_completed_article = True
res = helpers.bulk(es, to_append)
return (res, has_completed_article)
def get_articles():
token = get_login_token()
headers = {
"content-type": "application/json",
"accept": "application/json",
"authorization": "Bearer " + token
}
r = requests.get(base_url + '/articles/',
headers=headers, verify=False)
articles = r.json()
starting_number = 8000
offset = 100
for x in range(starting_number, articles['count'], offset):
print x
r = requests.get(base_url + '/articles/?limit=' + str(offset) + '&offset=' + str(x) + '&fields=url',
headers=headers, verify=False)
article = r.json()
res, has_completed_article = sync_articles_es(article['results'])
# if has_completed_article:
# return True
return True
def process_single_article(url):
articles = [{
'url': url
}]
res, has_completed_article = sync_articles_es(articles)
return (res, has_completed_article)
def deploy_new_update():
get_articles()
def reset_elastic():
es.indices.delete(index='articles', ignore=[400, 404])
es.indices.create(index='articles', ignore=[400, 404])
# reset_elastic()
deploy_new_update()