Skip to content

Commit d30b682

Browse files
committed
Updated
1 parent 1afd74a commit d30b682

File tree

16 files changed

+1394
-0
lines changed

16 files changed

+1394
-0
lines changed
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
from datetime import date, datetime, timezone, timedelta
2+
from newsapi import NewsApiClient
3+
import boto3
4+
from botocore.exceptions import ClientError
5+
import json
6+
7+
8+
def getAllNews () :
9+
try :
10+
print("get news for the run...")
11+
newsapi = NewsApiClient(api_key= "2b8bbd66c2e2403692a4e778e0697c21")
12+
from_date = datetime.now(timezone.utc) + timedelta(days=-3)
13+
end_time = datetime.now(timezone.utc) + timedelta(days=0)
14+
all_articles = newsapi.get_everything(q="Tenable Security",from_param=from_date,to=end_time,language="en",sort_by="relevancy")
15+
if all_articles :
16+
print("News APIs have returened..")
17+
# Dates
18+
dt = date.today()
19+
iso_date = dt.isoformat()
20+
all_articles.update({"UserName": "Rekhu Chinnarathod", "Email":"[email protected]", "ISODate": iso_date, "SearchString": "Tenable Security"})
21+
except ClientError as e:
22+
print(e.response["Error"]["Message"])
23+
else:
24+
print("News is retrieved as expected , hence retruning now"),
25+
return all_articles
26+
27+
28+
29+
# Get all news
30+
ListOfNews = getAllNews ()
31+
32+
'''
33+
#string to date conversion for sorting
34+
for article in ListOfNews['articles'] :
35+
datetime_object = datetime.strptime(article['publishedAt'], '%Y-%m-%dT%H:%M:%SZ')
36+
article['publishedAt'] = datetime_object
37+
'''
38+
39+
#Sort by date
40+
ListOfNews['articles'].sort(key=lambda item:item['publishedAt'], reverse=True)
41+
42+
# convert dictionary into string
43+
result = json.dumps(ListOfNews)
44+
finalresult = result.replace("null", "\"NoValue\"")
45+
46+
# Create a new SES resource and specify a region.
47+
client = boto3.client("ses",region_name="us-east-1")
48+
49+
# Create SES templates
50+
response = client.update_template(
51+
Template={
52+
"TemplateName": "NewAPIResultsMail",
53+
"SubjectPart": "News On {{ SearchString }} for Date : {{ISODate}}",
54+
"HtmlPart": """<html>
55+
<head>
56+
<style>
57+
table, td {
58+
border: 1px solid black;
59+
border-collapse: collapse;
60+
}
61+
62+
63+
th {
64+
border: 1px solid black;
65+
border-collapse: collapse;
66+
font-weight: bold
67+
}
68+
69+
70+
td, th {
71+
padding-left: 15px;
72+
text-align: left;
73+
}
74+
</style>
75+
</head>
76+
<body>
77+
<p style="font-family:'Futura Medium'">Hello {{ UserName }},</p>
78+
<p style="font-family:'Futura Medium'">Below are news on serach string {{ SearchString }} for Date : {{ISODate}}:</p>
79+
80+
<table style="width:100%">
81+
<col style="width:50%">
82+
<col style="width:50%">
83+
<tr bgcolor="yellow">
84+
<td>SourceId</td>
85+
<td>SourceName</td>
86+
<td>Author</td>
87+
<td>Title</td>
88+
<td>Description</td>
89+
<td>Url</td>
90+
<td>UrlToImage</td>
91+
<td>PublishedAt</td>
92+
<td>Content</td>
93+
</tr>
94+
{{#each articles}}
95+
<tr>
96+
<td>{{source.id}}</td>
97+
<td>{{source.name}}</td>
98+
<td>{{author}}</td>
99+
<td>{{title}}</td>
100+
<td>{{description}}</td>
101+
<td>{{url}}</td>
102+
<td>{{urlToImage}}</td>
103+
<td>{{publishedAt}}</td>
104+
<td>{{content}}</td>
105+
</tr>
106+
{{/each}}
107+
</table>
108+
109+
<p style="font-family:'Futura Medium'">Please check with [email protected] for any queries on the email</p>
110+
111+
<p style="font-family:'Futura Medium'">Best Regards,</p>
112+
<p style="font-family:'Futura Medium'">John Klacynski</p>
113+
</body>
114+
</html>
115+
""",
116+
"TextPart": """
117+
Hello {{ UserName }},
118+
119+
Below are news on serach string {{ SearchString }} for Date : {{ISODate}}:
120+
121+
{{#each articles}}
122+
{{source.id}}
123+
{{source.name}}
124+
{{author}}
125+
{{title}}
126+
{{description}}
127+
{{url}}
128+
{{urlToImage}}
129+
{{publishedAt}}
130+
{{content}}
131+
{{/each}}
132+
133+
Please check with [email protected] for any queries on the email
134+
135+
Best Regards,
136+
John Klacynski
137+
"""
138+
}
139+
)
140+
141+
142+
####### Send templated emails.
143+
Sendresponse = client.send_templated_email(
144+
145+
Destination={
146+
'ToAddresses': [
147+
148+
]
149+
},
150+
Template='NewAPIResultsMail',
151+
TemplateData= finalresult
152+
)
153+
154+
print(Sendresponse)
155+
156+
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
from datetime import date, datetime, timezone, timedelta
2+
from newsapi import NewsApiClient
3+
import boto3
4+
from botocore.exceptions import ClientError
5+
from json2html import *
6+
7+
8+
class NewsMails:
9+
def __init__(self):
10+
client = boto3.resource('dynamodb', region_name="us-east-1")
11+
self.table = client.Table('AllEmails')
12+
self.NewsApikey = '2b8bbd66c2e2403692a4e778e0697c21'
13+
self.ses_client = boto3.client('ses',region_name="us-east-1")
14+
15+
def getAllDynamoDBRecord (self):
16+
try :
17+
response = self.table.scan()
18+
emaildata = response['Items']
19+
while 'LastEvaluatedKey' in response:
20+
response = self.table.scan(ExclusiveStartKey=response['LastEvaluatedKey'])
21+
emaildata.extend(response['Items'])
22+
except ClientError as e:
23+
print(e.response['Error']['Message'])
24+
else:
25+
print("Dynamo DB records returned , hence retruning now"),
26+
return emaildata
27+
28+
def getAllNews (self , search) :
29+
try :
30+
print("get news for the run...")
31+
newsapi = NewsApiClient(api_key= self.NewsApikey)
32+
from_date = datetime.now(timezone.utc) + timedelta(days=-3)
33+
end_time = datetime.now(timezone.utc) + timedelta(days=0)
34+
all_articles = newsapi.get_everything(q=search,from_param=from_date,to=end_time,language='en',sort_by='relevancy')
35+
print(all_articles)
36+
except ClientError as e:
37+
print(e.response['Error']['Message'])
38+
else:
39+
print("News is retrieved as expected , hence retruning now"),
40+
return all_articles
41+
42+
def Email_News(self , SourceEmail, ToEmail, finalresult):
43+
try:
44+
Sendresponse = self.ses_client.send_templated_email(
45+
Source=SourceEmail,
46+
Destination={
47+
'ToAddresses': [
48+
ToEmail
49+
]
50+
},
51+
Template='NewAPIResultsMail',
52+
TemplateData= finalresult
53+
)
54+
except Exception as e:
55+
print(str(e))
56+
return str(e)
57+
if Sendresponse['ResponseMetadata']['HTTPStatusCode'] == 200:
58+
print('email sent successfully..')
59+
else :
60+
print('email sending failed..')

tf-eks-fargate-tmpl-master/.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.terraform
2+
.terraform.tfstate.lock.info
3+
terraform.tfstate
4+
terraform.tfstate.backup
5+
kubeconfig

tf-eks-fargate-tmpl-master/LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2020 finleap GmbH
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

tf-eks-fargate-tmpl-master/README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Terraform template for AWS EKS with Fargate profile
2+
3+
This terraform template can be used to setup the AWS infrastructure
4+
for a dockerized application running on EKS with a Fargate profile.
5+
6+
Ingress is based on https://docs.aws.amazon.com/eks/latest/userguide/alb-ingress.html
7+
and https://medium.com/@marcincuber/amazon-eks-with-oidc-provider-iam-roles-for-kubernetes-services-accounts-59015d15cb0c.
8+
9+
Due to the fact that EKS with Fargate profiles is not yet supported in all regions
10+
(https://docs.aws.amazon.com/eks/latest/userguide/fargate.html) this template uses
11+
the `eu-west-1 (Ireland)`region.
12+
13+
## Prerequisites
14+
This template requires `aws-iam-authenticator` and `openssl` to be installed
15+
16+
## Known limitations
17+
* Although the namespace `default` is set in the fargate profile (meaning
18+
pods will be executed on managed nodes), CoreDNS can currently only run
19+
on a fargate profile if the CoreDNS deployment is patched after the
20+
cluster is created (see https://github.com/terraform-providers/terraform-provider-aws/issues/11327
21+
or https://docs.aws.amazon.com/eks/latest/userguide/fargate-getting-started.html#fargate-gs-coredns
22+
for instructions). Therefore this template also creates a node-group for the `kube-system`
23+
namespace, which is also used for the Ingress controller.
24+
25+
* By default the `config` file for `kubectl` is created in `~/.kube` directory. If any
26+
configuration already exists there, it will be overwritten! To preserve any pre-existing
27+
configuration, change the `kubeconfig_path` variable.

0 commit comments

Comments
 (0)