Wednesday, November 30, 2022

Read, Send and Delete Gmails using Gmail API

 After Generating Client ID and Client Secret

1) Generate Authorization Code (One Time)

https://accounts.google.com/o/oauth2/auth?client_id=XXXXXXXX&redirect_uri=http://localhost&response_type=code&scope=https://mail.google.com/&access_type=offline

2) Generate Refresh Token (One Time)

curl --request POST --data "code=XXXXXXXX&client_id=XXXXXXXX&client_secret=XXXXXXXX&redirect_uri=http://localhost&grant_type=authorization_code" https://oauth2.googleapis.com/token

3) Generate Access token from Refresh Token (Every Time)

curl --request POST --data "client_id=XXXXXXXX&client_secret=XXXXXXXX&refresh_token=XXXXXXXX&grant_type=refresh_token" https://oauth2.googleapis.com/token

4) Read Email

List Message IDs of all emails
curl -X GET \
  'https://gmail.googleapis.com/gmail/v1/users/me/messages?q=label:inbox' \
  -H 'Authorization: Bearer XXXXXXXXXXXXXXXXXXXXXXXX'

List Content of Specific email
curl -sX GET   'https://gmail.googleapis.com/gmail/v1/users/me/messages/<msg-id>'   -H 'Authorization: Bearer XXXXXXXXXXXXXXXXXXXXXXXX' | jq -r '.payload.parts[0].body.data' | base64 --decode

5) Send Email
Mail Details in Plain Text

 
echo "From: corecodejs@gmail.com
To: corecodejs@gmail.com
Subject: Test Email Subject

This is the body of the email." > email.txt

cat email.txt

File encoded in base64

base64 email.txt | tr -d '\n' | tr '+/' '-_' > email_encoded.txt
cat email_encoded.txt

Send email
curl -X POST \
  'https://gmail.googleapis.com/gmail/v1/users/me/messages/send' \
  -H 'Authorization: Bearer XXXXXXXXXXXXXXXXXXXXXXXX' \
  -H 'Content-Type: application/json' \
  -d '{
    "raw": "'$(cat email_encoded.txt)'"
}'

6) Delete Email

curl -X DELETE \
  'https://gmail.googleapis.com/gmail/v1/users/me/messages/<msg-id>' \
  -H 'Authorization: Bearer XXXXXXXXXXXXXXXXXXXXXXXX'

Sunday, November 27, 2022

IBM / MAX Text Sentiment Analysis

Run an already trained model instance

git clone https://github.com/IBM/MAX-Text-Sentiment-Classifier.git
cd MAX-Text-Sentiment-Classifier
docker build -t max-text-sentiment-classifier .
docker run -it -p 5000:5000 max-text-sentiment-classifier
It will create a build in a docker container. We can check the application on port 5000.
http://localhost:5000

Open it by a domain / subdomain or it can be accessed by http://ip:5000
It will open a web interface. Since its model is already trained and it gives you output as positive and negative.

We need to pass the sentence and it will tell you, either the sentence is positive or negative. Once docker container is running, we can send the request using curl and collect the output in response.

curl -d "{ \"text\": [ \"The Model Asset Exchange is a crucial element of a developer's toolkit.\" ]}" -X POST "http://localhost:5000/model/predict" -H "Content-Type: application/json"

Response :

{
  "status": "ok",
  "predictions": [
    [
      {
        "positive": 0.9977352619171143,
        "negative": 0.0022646968718618155
      }
    ]
  ]
}

To Train the Model :

We are already in the folder MAX-Text-Sentiment-Classifier, cd into training folder. Create a virtual env.

cd training/
pip install -r requirements.txt
python setup_max_model_training.py max-text-classifier-training-config.yaml
Now this command will need account set up in IBM Cloud.  How much the output is accurate, it depends on the training tsv file.
https://cloud.ibm.com/login
Go On Manage > Access > API Keys > generate a key
Download the file apikey.json, copy on the server and Provide your key path.

Now run the docker instance, it will be launched based on this training.
docker build -t max-text-sentiment-classifier --build-arg use_pre_trained_model=false .
docker run -it -p 5000:5000 max-text-sentiment-classifier
Open url
http://ip:5000
Now it will give us the response with neutral output too as it has been trained based on the given tsv input.

To reset everything on cloud, delete Created service and Created storage in the cloud IBM account.
Remove all keys Entries from /etc/environment, exit or close the terminal, open and login again and run

python setup_max_model_training.py max-text-classifier-training-config.yaml
Note :

https://github.com/IBM/MAX-Text-Sentiment-Classifier


Saturday, November 19, 2022

Bash Sleep Sort

#!/bin/bash

function f() {
sleep "$1"
echo "$1"

}
while [ -n "$1" ]
do
f "$1" &
shift
done
wait

 Run the script.

./file.sh 6 1 3 7 8 4 2

Output :

It will sort the numbers and print them after 'printed number' seconds.

ex- 7 will be printed after 7 seconds.

Thursday, November 10, 2022

Logrotate is not running automatically in Redhat or CentOS

When you run logrotate command manually, it creates archived gz files successfully in the directory which you mentioned in the config file /etc/logrotate.d/custom-config but it does not run this command using daily cron automatically.

This is the most common problem of logrotate generally which you do not get in Ubuntu. Even if you want to run logrotate for your default log directory i.e. /var/log in Redhat or CentOS, you might not get this issue. You are getting this issue because you want to run logrotate automatically for your non-default directory like /opt/httpd/logs, /opt/odoo/logs or /opt/tomcat/logs etc.

Anacron runs logrotate using cron.daily. The issue is SELinux does not provide access to run logrotate using anacron for non-default log directories. It needs to mention explicitly to run logrotate for non default log directories.

There are two solutions of this issue.

Solution : 1

Do not depend on anacron. Create your own cron using root user. Log into the terminal using root user and set daily cron.

00 05 * * * /usr/sbin/logrotate -f /etc/logrotate.conf

Solution : 2

Set SELinux to run logrotate using anacron for non-default directories.

Follow this solution.

https://access.redhat.com/solutions/39006

Run commands as root user.

semanage fcontext -a -t var_log_t '/opt/httpd/logs(/.*)?'
cat /etc/selinux/targeted/contexts/files/file_contexts.local
restorecon -Frvv /opt/httpd/logs

It will solve your problem to run logrotate using cron automatically.

 

VScode cannot be loaded because running scripts is disabled on this system

Terminal is not opening in VSCode. It gives the error.

VScode cannot be loaded because running scripts is disabled on this system
Open VSCode.

Go to
File > preferences > settings > extension > edit settings.json

Add following snippet at the start below first curly brace {


"terminal.integrated.profiles.windows": {
  "PowerShell": {
    "source": "PowerShell",
    "icon": "terminal-powershell",
    "args": ["-ExecutionPolicy", "Bypass"]
  }
},
"terminal.integrated.defaultProfile.windows": "PowerShell",

Now restart VS Code, close the Terminal and open it again.

Git Bash: "Unable to get local issuer certificate"

If you are getting above error while cloning the repository, add following flag with the command and you will not get the error.

 git -c http.sslVerify=false clone https://bitbucket.org/username/reponame.git