Skip to content

Commit

Permalink
Merge pull request #7 from ShahnurIslam/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
ShahnurIslam authored Nov 12, 2020
2 parents 8f0c872 + 5d05b6b commit 900248e
Show file tree
Hide file tree
Showing 19 changed files with 623 additions and 3 deletions.
4 changes: 4 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.vscode/
build
node_modules
.env
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# Dependencies
node_modules/
build/
build/
.vscode/
.env
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
To compile code into build
tsc

To run code we use `npm start`

To build the docker container we use
`docker build -t ps5_scraper .`

To run we use
`docker run --env-file .env node_test5`

Gcloud deployment
`tsc`
`cd build`
`gcloud functions deploy runBot --trigger-topic scrapr --runtime nodejs10 --env-vars-file .env.yaml`
7 changes: 7 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"url":{
"Amazon": "https://www.amazon.co.uk/dp/B08H95Y452/",
"ShopTo": "https://www.shopto.net/en/ps5hw01-playstation-5-console-p191472/",
"Game": "https://www.game.co.uk/en/playstation-5-console-2826338"
}
}
22 changes: 22 additions & 0 deletions deployment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
---
apiVersion: "apps/v1"
kind: "Deployment"
metadata:
name: "ps5-scraper"
namespace: "default"
labels:
app: "ps5-scraper"
spec:
replicas: 1
selector:
matchLabels:
app: "ps5-scraper"
template:
metadata:
labels:
app: "ps5-scraper"
spec:
containers:
- name: "ps5-scraper-sha256-1"
image: "eu.gcr.io/e-resolver-294514/ps5_scraper@sha256:5057c299a63ba09f2395a6db3b401f92bd0ab21bae0da790ffc6a23e2227d4cf"

16 changes: 16 additions & 0 deletions dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
FROM node:alpine

# Create our working directory
WORKDIR /usr/ps5_scraper

# Copy over the package.json to this directory
COPY package.json .

#Install our packages
RUN npm install

# Add the current contents of our code to this directory
ADD . /usr/ps5_scraper/

# Start the code
CMD [ "npm", "start" ]
17 changes: 16 additions & 1 deletion index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,16 @@
console.log("hello")
import {Bot} from "./src/Bot"
import {Logger,LogLevel} from './src/Logger';

// Start our Bot
// new Bot(new Logger(LogLevel.Info)).start();




function bot(){
new Bot(new Logger(LogLevel.Info)).start()
};

exports.runBot = bot;

// bot()
171 changes: 171 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "ps5_scraper",
"version": "1.0.0",
"description": "To compile code into build tsc",
"main": "index.js",
"scripts": {
"start": "ts-node index.ts"
},
"author": "Shan Islam",
"license": "ISC",
"devDependencies": {
"ts-node": "^9.0.0",
"typescript": "^4.0.5"
},
"dependencies": {
"@types/axios": "^0.14.0",
"axios": "^0.20.0",
"cheerio": "^1.0.0-rc.3",
"nodemailer": "^6.4.13"
}
}
71 changes: 71 additions & 0 deletions src/Bot.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import {Logger,LogLevel} from "./Logger"
import { AmazonUK } from "./Crawlers/AmazonUk"
import { ShopTo } from "./Crawlers/ShopTo"
import { Game } from "./Crawlers/Game"
import { Crawler } from './Crawlers/Crawler'
import { Email } from './Notifications/Mailer'

export class Bot {
stock_dict: any
email: Email
msg: string

constructor(private readonly logger: Logger){
this.stock_dict = {}
this.email = new Email
this.msg = ""
}

private async scrape_site(cr:Crawler){
this.logger.info(`Starting Crawler on site ${cr.getRetailerName()}`)
this.stock_dict[cr.getRetailerName()] = await cr.getStock(this.logger)
// this.msg += '\n' + cr.getRetailerName() + ' has stock at url: ' + cr.getUrl()
if (this.stock_dict[cr.getRetailerName()] === true){
if (this.msg == ""){
this.msg = cr.getRetailerName() + ' has stock <a href="' + cr.getUrl() + '">here</a><br/>'
} else {
this.msg += cr.getRetailerName() + ' has stock <a href="' + cr.getUrl() + '">here</a><br/>'
}

}
}

private async scrape_site2(cr:Crawler){
this.logger.info(`Starting Crawler on site ${cr.getRetailerName()}`)
this.stock_dict[cr.getRetailerName()] = cr
}

check_stock(st_dict){
var filtered = Object.keys(st_dict).reduce(function (filtered, key) {
if (st_dict[key] === true) filtered[key] = st_dict[key];
return filtered;
}, {});
const len = Object.keys(filtered).length
return len
}

email_notification(st_dict){
if(this.check_stock(st_dict) > 0){
var filtered: { [characterName: string]: boolean} = Object.keys(st_dict).reduce(function (filtered, key) {
if (st_dict[key] === true) filtered[key] = st_dict[key];
return filtered;
}, {});

this.email.main(this.msg)

} else{
this.logger.info("No stock anywhere")
}
}

public async start(){
this.logger.info("Starting bot scraper")
await this.scrape_site(new AmazonUK)
await this.scrape_site(new ShopTo)
await this.scrape_site(new Game)
// this.stock_dict['test'] = true
console.log(this.stock_dict)
this.email_notification(this.stock_dict)
}

}
20 changes: 20 additions & 0 deletions src/Crawler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@



function resolveAfter2Seconds() {
return new Promise(resolve => {
setTimeout(() => {
resolve('resolved');
}, 10000);
});
}



async function crawler() {
console.log('calling');
const result = await resolveAfter2Seconds();
console.log(result);
// expected output: "resolved"
}

Loading

0 comments on commit 900248e

Please sign in to comment.