1
1
'use strict' ;
2
2
3
3
const functions = require ( './functions/index' ) ;
4
- let { hello, todaysPrice, getBrokersFromDb} = functions
4
+ let {
5
+ hello,
6
+ todaysPrice,
7
+ getBrokersFromDb,
8
+ brokers,
9
+ getTodaysPriceFromDb
10
+ } = functions
5
11
6
12
const connectToDatabase = require ( './db' ) ;
7
13
const brokerModel = require ( './api/schemas/broker.js' ) ;
@@ -10,91 +16,7 @@ const brokerModel = require('./api/schemas/broker.js');
10
16
module . exports . hello = hello ;
11
17
module . exports . todaysPrice = todaysPrice ;
12
18
module . exports . getBrokersFromDb = getBrokersFromDb ;
19
+ module . exports . brokers = brokers ;
20
+ module . exports . getTodaysPriceFromDb = getTodaysPriceFromDb
13
21
14
- var mongoose = require ( "mongoose" ) ;
15
- mongoose . connect ( 'mongodb+srv://lambdaUser:[email protected] /test?retryWrites=true&w=majority' ,
16
- { useNewUrlParser : true } ) ;
17
22
18
-
19
-
20
- module . exports . brokers = ( event , context , callback ) => {
21
- context . callbackWaitsForEmptyEventLoop = false ;
22
- const db = mongoose . connection
23
- db . once ( 'open' , _ => {
24
- console . log ( 'Database connected:' )
25
- } )
26
- db . on ( 'error' , err => {
27
- console . error ( 'connection error:' , err )
28
- callback ( null , 'failure' )
29
- } )
30
-
31
-
32
- const request = require ( 'request' ) ;
33
- const cheerio = require ( 'cheerio' ) ;
34
- try {
35
- request ( 'http://www.nepalstock.com/brokers' , async ( error , response , html ) => {
36
- if ( ! error && response . statusCode == 200 ) {
37
- const $ = cheerio . load ( html ) ;
38
- const pager = $ ( '.pager a' ) . first ( ) . text ( ) ;
39
- const totalPages = pager . split ( '/' ) [ 1 ]
40
- let brokerList = [ ]
41
- const iterableArray = [ ...Array ( + totalPages ) . keys ( ) ]
42
- const promises = iterableArray . map ( ( i , value ) => {
43
- return new Promise ( ( resolve , reject ) => {
44
- request ( `http://www.nepalstock.com/brokers/index/${ i + 1 } /` ,
45
- ( error , response , html ) => {
46
- if ( ! error && response . statusCode == 200 ) {
47
- const $ = cheerio . load ( html ) ;
48
- const brokers = $ ( '.content' ) ;
49
- brokers . each ( ( i , el ) => {
50
- const name = $ ( el ) . children ( 'h4' ) . text ( ) ;
51
- const info = $ ( el ) . find ( '.row-content' ) . text ( ) ;
52
- let details = info . trim ( ) . split ( '\n' ) ;
53
- details = details . map ( detail => detail . trim ( ) )
54
- let [ address , code , telephone , email , website , person ] = details
55
- brokerList . push ( { name, address, code, telephone, email, website, person } ) ;
56
- } )
57
- resolve ( html )
58
- } else {
59
- reject ( error )
60
- }
61
- } )
62
- } )
63
- } )
64
- await Promise . all ( promises )
65
- const brokerPromise = brokerList . map ( ( brokerinfo ) => {
66
- return new Promise ( ( resolve , reject ) => {
67
- let query = { code : brokerinfo . code } ;
68
- let update = brokerinfo ;
69
- let options = { upsert : true , new : true , setDefaultsOnInsert : true } ;
70
- brokerModel . findOneAndUpdate ( query , update , options ) . then ( ( data ) => {
71
- resolve ( data )
72
- } ) . catch ( ( ) => {
73
- reject ( err )
74
- } )
75
- } )
76
- } )
77
- await Promise . all ( brokerPromise )
78
- mongoose . connection . close ( ) ;
79
- const response = {
80
- statusCode : 200 ,
81
- headers :{
82
- "Access-Control-Allow-Origin" : "*"
83
- } ,
84
- body : JSON . stringify (
85
- {
86
- status : "success" ,
87
- } ) ,
88
- } ;
89
- callback ( null , response ) ;
90
- }
91
- } ) ;
92
-
93
- }
94
- catch ( error ) {
95
- //callback(error)
96
- }
97
-
98
-
99
-
100
- }
0 commit comments