repl.it
Nodejs

No description

fork
loading
Files
  • index.js
  • main.js
  • rx-4d5ce-517b9c7385a6.json
  • Packager files
  • package-lock.json
  • package.json
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
require('dotenv').config();
const request = require('request-promise');
const cheerio = require('cheerio');
const admin = require('firebase-admin');

let serviceAccount = require('./rx-4d5ce-517b9c7385a6.json');

admin.initializeApp({
  credential: admin.credential.cert(serviceAccount)
});

let db = admin.firestore();

async function parsePage(medicationId, regionId) {
  const base_url = process.env.BASE_URL;
  const host = process.env.HOST;
  const url = `${base_url}?period=60&soato=${regionId}&search=${medicationId}`;

  const response = await request({
    uri: url,
    headers: {
      'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
      'Accept-Encoding': 'gzip, deflate, br',
      'Accept-Language': 'en-US,en;q=0.9,fr;q=0.8,ro;q=0.7,ru;q=0.6,la;q=0.5,pt;q=0.4,de;q=0.3',
      'Cache-Control': 'max-age=0',
      'Connection': 'keep-alive',
      'Host': host,
      'Upgrade-Insecure-Requests': '1',
      'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36'
    },
    gzip: true
  }
  );

  // console.log(response)

  let $ = cheerio.load(response);

  let table = $('.kv-grid-table');
  let titles = [];
  let producers = [];
  let prices = [];
  let stores = [];

  for (let i = 0; i < table.length; i++) {
    let item = table.eq(i);

    item.find('td[data-col-seq="1"] small').each(function() {
      producers.push($(this).text().trim())
    });


    item.find('td[data-col-seq="1"]').each(function() {
      let el = $("small").remove()
      titles.push($(this).text().trim())
    });


    item.find('td[data-col-seq="2"]').each(function() {
      let price = $(this).text().trim();
      const regex = /([0-9])\w*/g
      price = parseInt(price.match(regex).join(""))
      prices.push(price)
    });

    item.find('td[data-col-seq="5"] a').each(function() {
      let href = $(this).attr("href");
      const regex = /([0-9])\w+/g
      href = href.match(regex)[0]
      stores.push(href)
    });


  }

  console.log(titles, producers, prices, stores)

  availableItems = []
  medications = []

  for(var i=0; i<stores.length; i++) {
    let availableItem = {
      title: titles[i],
      med_id: medicationId,
      producer: producers[i],
      price: prices[i],
      store_id: stores[i]
    }

    let newMedication = {
      value: titles[i],
      aa_id: medicationId,
      label: titles[i]
    }
    medications.push(newMedication)

    let docRef = db.collection('medications').doc(decimalToHexString(medicationId));

    let setMedDoc = docRef.set(newMedication, {merge: true});

    // availableItems.push(availableItem);
    docRef = db.collection('available_items').add(availableItem)

  }

  console.log(medications)

}


parsePage( medicationId=500, regionId=1726269)


// const rIds = [1726262,1726264,1726266,1726269,1726273,1726277,1726280,1726283,1726287,1726290,1726294];

// for (var m=1; m<10; m++) {
//   rIds.forEach(async function (r) {
//     await parsePage(m, r)
//   });  
// };



function decimalToHexString(number)
{
  if (number < 0)
  {
    number = 0xFFFFFFFF + number + 1;
  }

  return number.toString(16).toUpperCase();
}
?