-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontroller.py
112 lines (96 loc) · 3.49 KB
/
controller.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
from boto3 import resource
from boto3.dynamodb.conditions import Key
from flask import jsonify, make_response
import config
import uuid
import time
AWS_ACCESS_KEY_ID = config.AWS_ACCESS_KEY_ID
AWS_SECRET_ACCESS_KEY = config.AWS_SECRET_ACCESS_KEY
AWS_REGION = config.AWS_REGION
resource = resource('dynamodb',
aws_access_key_id=AWS_ACCESS_KEY_ID,
aws_secret_access_key=AWS_SECRET_ACCESS_KEY,
region_name=AWS_REGION
)
def read_news_for_user(user_id):
# check if user exists
user_table = resource.Table('users')
response = user_table.get_item(
Key={
'user_id': user_id
}
)
if 'Item' not in response:
return make_response(jsonify('User not found'), 404)
news_table = resource.Table('news_articles')
user_views_table = resource.Table('user_views')
response = user_views_table.query(
IndexName='user_id-index', # Assuming GSI on user_id if user_id is not the PK
KeyConditionExpression=Key('user_id').eq(user_id)
)
viewed_article_ids = [item['article_id'] for item in response['Items']]
#response = news_table.query(IndexName='published_datetime-index', Limit=20, ScanIndexForward=False)
response = news_table.scan()
# Filter articles not in viewed_article_ids and sort by Publish_datetime
articles = [item for item in response['Items'] if item['article_id'] not in viewed_article_ids]
news_articles = []
for item in articles:
article = {
'article_id': item['article_id'],
'published_datetime': item['published_datetime'],
'title': item['title'],
'summary': item['summary'],
'url': item['url'],
'image_url': item['image_url'],
'region': item['region'],
'article_type': item['article_type']
}
news_articles.append(article)
return make_response(jsonify(news_articles), 200)
def write_news_to_db(data):
print('writing to db: total=%s' % len(data))
table = resource.Table('news_articles')
for article in data:
response = table.put_item(
Item={
'article_id': str(article.article_id),
'published_datetime': str(article.published_datetime),
'title': article.article_title,
'summary': article.article_summary,
'url': article.url,
'image_url': article.image_url,
'region': article.article_region,
'article_type': article.article_type
}
)
return
def viewed_news(user, article_id):
# check if user exists
user_table = resource.Table('users')
response = user_table.get_item(
Key={
'user_id': user
}
)
if 'Item' not in response:
return make_response(jsonify('User not found'), 404)
table = resource.Table('user_views')
response = table.put_item(
Item={
'view_id': str(uuid.uuid4()), # Generate a unique view_id
'user_id': user,
'article_id': article_id,
'viewed_datetime': str(int(time.time()))
}
)
return make_response(jsonify('success'), 200)
def create_user(user_name):
table = resource.Table('users')
idd = str(uuid.uuid4())
response = table.put_item(
Item={
'user_id': idd, # Generate a unique user_id
'user_name': user_name,
}
)
return make_response(jsonify(idd), 200)