These are all the APIs that will be used in the EmergenZ app project. The API is made to run these functions :
- Handle register and login for user
- Handle user profile data in the app
- Handle the news/article in the app
- Handle the news recommendation for users based on what they have read.
This API used to create users account
URL: register/
Method: POST
Request Body :
{
username,
email,
password
}
Success Response :
{
"error": false,
"message": "User Created"
}
This API used to validate user login attempt
URL: login/
Method: POST
Request Body:
{
email,
password
}
Success Response:
{
"error": false,
"message": "{{email}} login success",
"data": {
"userId": "User Id",
"name": "Username",
"token": "Token for authorization"
}
}
This API used to create user profile in the app
URL: add_detail_user/
Method: POST
Headers:
Content-Type: multipart/form-data
Authorization: Bearer <token>
Request Body:
{
email,
name,
nik,
gender,
province,
city,
address,
image
}
Success Response:
{
"error": false,
"message": "{{name}} data has been created",
"link": "link KTP image if needed"
}
This API used to get user profile data
URL: user_data/{{email}}
Method: GET
Headers:
Authorization: Bearer <token>
Success Response:
{
"error": false,
"message": "{{email}} found",
"data":
{
"email": "user email",
"name": "user fullname",
"gender": "user gender",
"nik": "user ID number",
"province": "user province",
"city": "user city",
"address": "user address",
"image_url": "user ID image url"
}
}
This API used to give the user news/article recommendation based on the article they have seen.
URL: news/{{email}}
Method: GET
Headers:
Authorization: Bearer <token>
Response:
{
"error": false,
"message": "News for {{email}}",
"data":
[
{
"news_id": news id,
"title": "news title",
"author": "news author",
"category": "news category",
"image": "news image url",
},
{
"news_id": news id,
"title": "news title",
"author": "news author",
"category": "news category",
"image": "news image url",
},
...
]
}
This API used to give the content of the news that user pick and also add that access to history for the recommendation
URL: news/{{news_id}}
Method: POST
Headers:
Authorization: Bearer <token>
Request Body:
{
email
}
Response:
{
"error": false,
"message": "News history record added and news retrieved",
"data":
[
{
"news_id": news id,
"title": "news title",
"author": "news author",
"content": "news content",
"category": "news category",
"image": "news image url",
"upload_date": "news upload date",
}
]
}
To deploy this API, we need use some services in the Cloud (for this, we deploy it in Google Cloud Platform)
Cloud SQL with MySQL. This service will be used to store all app data in the database using MySQL. You can check it in here.App Engine. This service will be used to deploy our Node.js app. The Node.js app is the main app for the API. This is optional and you also can deploy it with cloud run. You can check it in here.Cloud Run. This service will be used to deploy our Flask app where we deploy the machine learning model for the recommendation system. You can check it in hereCloud Storage. This service will be used to store all image related to the app, such as user image and news image. You can check it in here
The database consists of 4 tables :
accounttable. It is used to store user account information, like email and password. it has 4 fields:- user_id VARCHAR(255)
- email VARCHAR(255)
- username VARCHAR(255)
- password VARCHAR(255)
usertable. It is used to store user profile information, such as name, gender, nik, etc. It has 9 fields:- user_id INT(11)
- email VARCHAR(255)
- name VARCHAR(255)
- gender VARCHAR(1)
- nik VARCHAR(16)
- province VARCHAR(255)
- city VARCHAR(255)
- address VARCHAR(255)
- image_url VARCHAR(255)
newstable. It is used to store all the news data for the app. it has 7 fields:- news_id INT(11)
- title TEXT
- author VARCHAR(255)
- content TEXT
- category VARCHAR(255)
- image TEXT
- upload_date DATETIME
historytable. It is used to store user news history. The data in this table will be used for the news recommendation. it has 3 fields:- email VARCHAR(255)
- news_id INT(11)
- time TIMESTAMP
To create the table, we can use sql query below, or run the sql file in this repository
CREATE TABLE account(
user_id varchar(255) NOT NULL primary key,
email varchar(255) not null unique,
username varchar(255) not null,
password varchar(255) not null
);
create table user(
user_id int primary key auto_increment,
email varchar(255) not null unique,
name varchar(255) not null,
gender varchar(1) not null,
nik varchar(16) not null unique,
province varchar(255) not null,
city varchar(255) not null,
address varchar(255) not null,
image_url varchar(255) not null,
foreign key(email) references account(email)
);
create table history(
email varchar(255),
news_id int,
time timestamp default current_timestamp(),
foreign key(email) references user(email),
foreign key(news_id) references news(news_id)
);
create table news(
news_id int primary key auto_increment,
title text not null,
author varchar(255) default "Admin",
content text not null,
category varchar(255) not null,
image text not null,
upload_date DATETIME DEFAULT current_timestamp()
);
The app architecture in the cloud will be like this :
Register: User register account with API → save the account data in Cloud SQL → User send the profile data → ID (KTP) image stored in Cloud SQL → user profile stored in Cloud SQL.Login: User input email and password → the Cloud SQL will check the email if exist → the App Engine will validate if the password is right → If right, the user will be directed to homepage.News Recommendation: User login → the cloud SQL will collect the user news history → App Engine will send it to the Cloud Run to get the user news recommendation → Cloud Run response with the recommendation → App Engine sends it to Cloud SQL to find the recommended news → app engine will send the news back to user.Access news: User click on news → New record will be added to news access history →Cloud SQL send the news content to App Engine → The App Engine will sends the content back to user.
This bucket will be used to save the image that has been uploaded by the user.
- Open your Google Cloud console
- Go to cloud storage and make a storage bucket
- Make sure the bucket is accessible through the internet so we won't have problem when we want to get the image
- Make a service account in IAM that has permission to store file in the bucket
- Make a key for the service account and then store it as json. The key will be used in the node.js
- Make sure you already install the Node.js.
- Create the project using
npm initinside the project directory. - run
npm install <package name>for these package:- @google-cloud/storage
- bcrypt
- body-parser
- dotenv
- express
- jsonwebtoken
- multer
- mysql
- nodemon (optional)
- Make the
server.jsandroutes.jsfor the app. You can see the example in this repository. - Make a javascript code to handle the image upload. You can see the example in the
imgUpload.jsin folder modules in this repository. - Don't forget to add the service account key and
.envfile to the Node.js project. The service account will be used to upload the image and the.envwill be used to store access token for the authorization. - After finished, we can add
"start"script in the package.json. The script will be like this"start": "node server.js". - Now we can run the app by using command
npm run start.
Flask will be used to deploy the machine learning model that give us the news recommendation for the user.
- Make sure you already install Flask in your computer. You can see how to do it here.
- After that, create the python file as the main app for the project. We can called it
main.py. - We also need to install Tensorflow and nltk. We can do this by using
pip install tensorflowandpip install nltk. - After that, we need to download stopwords for the text processsing inside the app. We can download it from the code, for example
nltk.download('popular'), or download it manually from here and then store it inside a folder name nltk. - We also need to store the keywords that will be used in the text processing. The keywords can be stored inside a python file, like
keywords.py, and then to use it we only need to import it. - Inside the
main.py, we can create the route or url for the API. You can see the example in the API-Flask folder. - After finished, we can run the app by using
flask --app <python file> runcommand. For example, if the file called main, then the command will beflask --app main run

