summaryrefslogtreecommitdiff
path: root/rskrf.lua
blob: e0ec180ad20aab43ab2e96fcb367152e2a10ee88 (plain)
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
#!/usr/bin/env lua
--rskrf.lua
--Copyright (C) 2020-2021  Aleksei Kovura

--This program is free software: you can redistribute it and/or modify
--it under the terms of the GNU General Public License as published by
--the Free Software Foundation, either version 3 of the License, or
--(at your option) any later version.

--This program is distributed in the hope that it will be useful,
--but WITHOUT ANY WARRANTY; without even the implied warranty of
--MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
--GNU General Public License for more details.

--You should have received a copy of the GNU General Public License
--along with this program.  If not, see <https://www.gnu.org/licenses/>.
local gumbo = require 'gumbo'
local inspect = require 'inspect'
local binser = require 'binser'
local http_request = require 'http.request'
trim = require 'string.trim'
json = require 'dkjson'.use_lpeg()

function url_to_tbl(url)
  headers, stream = assert(http_request.new_from_uri(url):go())
  response = assert(stream:get_body_as_string(), 'Can\'t fetch')
  if headers:get ':status' ~= '200' then
      error(response)
  end
  local out, pos, err = json.decode(response)
  if err then
    print ("Error:", err)
  else
    return out.response
  end
end
--Бакалея(32)/Рис круглозерный(5107)/Fine Life Крупа рисовая. рис шлифованный.
--первый сорт(3439343) = 5
-- hardcoded stuff
cats_block = {"Техника", "Обувь и аксессуары", "Одежда", "Цифровые продукты",
  "Товары для детей"}
for _, cat in ipairs(cats_block) do
  cats_block[cat]=1
end
--cats_block = {["Техника"] = 1, ["Обувь и аксессуары"] = 1, ["Одежда"] = 1, ["Цифровые продукты"] = 1, ["Товары для детей"] = 1}
subcats_block = {"Алкогольные", "Колбасные изделия", "Кондитерские изделия",
  "Рыба и морепродукты", "Соусы", "Овощи,  фрукты", "Косметика", "Консервы"}
for _, subcat in ipairs(subcats_block) do
  subcats_block[subcat]=1
end
prod_groups_allow = {"Мед", "Гречневая крупа", "Ряженка", "Хлеб Бородинский",
  "Кефир", "Творог 9% жирности", "Сыр Тильзитер", "Сметана", "Хлеб Дарницкий",
  "Масло сливочное 82,5% жирности", "Масса творожная", "Куриные яйца", "Квас",
  "Сыр «Российский»", "Креветки", "Филе цыпленка бройлера", "Молоко",
  "Куры бройлеры", "Минтай мороженый обезглавленный", "Филе минтая",
  "Лососи соленые в ломтиках", "Пресервы из сельди в масле", "Презервативы",
  "Нерафинированное подсолнечное масло", "Рафинированное подсолнечное масло",
  "Стиральные порошки", "Хлеб Дарницкий" }
for _, pg in ipairs(prod_groups_allow) do
  prod_groups_allow[pg]=1
end
-- /hardcoded stuff
groceries_ids = {} -- Продукты питания
burl = 'https://rskrf.ru/rest/1'
categories_raw = url_to_tbl(burl..'/catalog/categories/')
categories = {}
for c = 1,#categories_raw do
  cat = categories_raw[c]
  if not cats_block[cat.title] then
    categories[cat.title] = cat.id
  end
end
print('categories:\n'..inspect(categories))
subcats = {}
for _, c_id in pairs(categories) do
  subcats_raw = url_to_tbl(burl..'/catalog/categories/'..c_id)
  for s = 1,#subcats_raw do
    subcat = subcats_raw[s]
    if not subcats_block[subcat.title] then
      subcats[subcat.title] = subcat.id
    end
  end
end
print('subcats:\n'..inspect(subcats))
prod_groups = {}
for  _, subcat_id in pairs(subcats) do
  prod_groups_raw = url_to_tbl(burl..'/catalog/categories/'..subcat_id..
    '/productGroups/').productGroups
  for pg = 1,#prod_groups_raw do
    prod_group = prod_groups_raw[pg]
    pg_title = trim(prod_group.title)
    if prod_groups_allow[pg_title] then
    --if 1==1 then
      prod_groups[pg_title] = {}
      prod_groups[pg_title]['id'] = prod_group.id
    end
  end
end
print('prod_groups:\n'..inspect(prod_groups))
for pg_title, pg in pairs(prod_groups) do
  products_raw=url_to_tbl(burl..'/catalog/products/'..pg.id).products
  for p = 1,#products_raw do
    product=products_raw[p]
    if product.total_rating > 4.5 then
      --prod_groups[pg_title][product.title] = {}
      manuf = string.format('%q', product.manufacturer)
      prod_groups[pg_title][manuf] = {}
      prod_groups[pg_title][manuf]['rating'] = product.total_rating
      prod_groups[pg_title][manuf]['thumbnail'] = product.thumbnail
      prod_groups[pg_title][manuf]['id'] = product.id
    end
  end
end
print('prod_groups:\n'..inspect(prod_groups))
        --prod_groups[
        --print('productGroup.title='..productGroup.title..','..
          --'product.title= '..product.title..','..
          --'product.id= '..product.id..','..
          --'product.manufacturer='..
          --string.format('%q', product.manufacturer))
        --if product.manufacturer==nil then
          --print('product.manufacturer='..'nil')
        --else
          --print('product.manufacturer='..string.format('%q', product.manufacturer))
        --end
          --product.total_rating)
if not io.open(rskrf.dat, r) then
  print('file rskrf.dat doesn\'t exist, initializing')
  io.open(rskrf.dat, 'wb'):close()
  binser.writeFile(rskrf.dat, tbl)
  os.exit()
end