-
Notifications
You must be signed in to change notification settings - Fork 7
/
news_repo.py
26 lines (22 loc) · 1.1 KB
/
news_repo.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
import json
from google.cloud import bigquery
def get_random_news():
bq_client = bigquery.Client()
random_query = "SELECT an, title, Concat(snippet, body) as body, publication_date as publication_datetime, publisher_name \
FROM `dowjones-com.sample.news_archive` \
WHERE language_code = 'en' AND body is not null AND snippet is not null \
AND RAND() < 10/21086"
bq_job = bq_client.query(random_query)
bq_results = bq_job.to_dataframe()
return bq_results.to_json(orient='records')
def get_article_by_an(articlean):
bq_client = bigquery.Client()
random_query = "SELECT an, title, Concat(snippet, body) as body, publication_date as publication_datetime, publisher_name \
FROM `dowjones-com.sample.news_archive` \
WHERE an='{}'".format(articlean)
bq_job = bq_client.query(random_query)
bq_results = bq_job.to_dataframe()
if bq_results.shape[0] == 1:
single_article = json.loads(bq_results.to_json(orient='records'))
return json.dumps(single_article[0])
return None