golang-database-try

This commit is contained in:
Alex Borisov 2024-01-27 21:44:51 +03:00
commit 730e7ceaf0
15 changed files with 897 additions and 0 deletions

5
.gitignore vendored Normal file
View File

@ -0,0 +1,5 @@
/venv
/Python
/.env
/data
/dockerfile

20
.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,20 @@
{
"cSpell.words": [
"телеграме",
"Экспуатация",
"Элжур",
"documize",
"Eljur",
"Fjournal",
"godotenv",
"htmldiff",
"joho",
"lgray",
"lightgreen",
"lightpink",
"lightskyblue",
"loginviewport",
"Sosiaka",
"Vasya"
]
}

31
Dockerfile Normal file
View File

@ -0,0 +1,31 @@
# Stage 1: Build the Rod application
FROM golang:1.21.2 AS builder
# Set the working directory inside the Docker image
WORKDIR /app
# Copy the go mod and sum files, and download dependencies
COPY go.mod go.sum /app/
RUN go mod download
# Copy the source code into the Docker image
COPY *.go /app/
# Build the Rod application
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o main .
# Stage 2: Use a slim image for the runnable container
FROM alpine:3.14 as runtime
# Set the working directory
WORKDIR /root/
# Install browser
RUN apk add chromium
# Copy the binary from the builder stage
COPY --from=builder /app/main .
COPY Golang/ .
# Set the binary as the default command to run when starting the container
CMD ["./main"]

View File

BIN
Golang/data/sad.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 62 KiB

0
Golang/data/table.html Normal file
View File

201
LICENSE Normal file
View File

@ -0,0 +1,201 @@
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.
"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:
(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and
(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.
You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS
APPENDIX: How to apply the Apache License to your work.
To apply the Apache License to your work, attach the following
boilerplate notice, with the fields enclosed by brackets "[]"
replaced with your own identifying information. (Don't include
the brackets!) The text should be enclosed in the appropriate
comment syntax for the file format. We also recommend that a
file or class name and description of purpose be included on the
same "printed page" as the copyright notice for easier
identification within third-party archives.
Copyright 2023 Alex Borisov
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

108
README.md Normal file
View File

@ -0,0 +1,108 @@
## Идея для нового метода обработки объявлений
- [ ] Обрабатывать не как текст, а в виде объекта структуры объявления
- [ ] Сохранять объявления в виде байт кода массива объявлений
- [ ] Снова переделать функцию поиска различий для новых структур данных
### Структура объявления `board-item`
- `active` -- *bool* -- из названия класса
- `dates` -- *string pair* -- дата-дата в формате *ДД.ММ.ГГГГ* (в будущем отправлять только с флажком "удалено раньше времени")
- `title` -- *string* -- заголовок из класса `board-item__title`
- `content` -- *string* -- текст из объявления. Ознакомьтесь с [поддерживаемыми тегами](https://core.telegram.org/bots/api#html-style) и на тег `br`. Класс `typography typography--links`
- `attachments` -- *array* из структур `outline` -- если есть вложения, класс `button`
- `creator` -- *string* -- из класса `b-href`
- `receivers` -- *string* -- строку собираем из всех получателей с разделителем ` · `
- `edit_pattern` -- *[]int* -- 6 значений `0` или `1` по изменениям
- `unique` -- *bool* -- уникально ли объявление относительно нового / старого списка
> `edit pattern` и `unique` заполняются после получения новых объявлений отдельной функцией, вызываемой во время генерации сообщения
### Структура вложения `outline`
> Берем класс `button` на разбор
- `title` -- *string* -- заголовок
- `link` -- *string* -- ссылка на контент
- `type` -- *string* -- название вложенного класса по тегу `i`
### Изменения в экспорте содержимого страницы `get_content`
- [x] С помощью указателей `rod` найти все объявления как указатели на объекты JS
- [ ] отправить текст каждого объекта в функцию `parse_post`, принимающую текст и возвращающую объект структуры объявления с заполненными полями.
- [x] Собрать все структуры объявлений в массив и вернуть его
### Изменения в обработке содержимого `scape`
- [x] Адаптация под новый `get_content`
- [x] Теперь возвращает массив
### Изменения в `get_difference`
- Теперь мы сравниваем два массива, формируем сообщение в формате *string* с html форматированием
- Появился новый заголовок == новое объявление ->> целиком под заголовком `появились объявления`
- Изменилось что-то в объявлении ->> заголовок + "изменилось <слой>" + новый слой, под заголовком `изменились объявления`
- Пропал заголовок объявления раньше времени ->> целиком под заголовком `удалено вручную`
### Изменения в работе с файлами `get_file` и `update_file`
- Добавить вызов функции для работы с байткодом
- Сохранить полученный байткод в файл
## Бонус
- Переименовать функции под стандарты *golang*
- Пример: `get_difference` ->> `GetDifference`
- Написать комментарии к коду
# Комментарии [ТУТ](http://git.soaska.ru/sosiska/eljur/-/snippets/6#note_40)!
## Элжур бот
Предназначен для получения объявлений в телеграме в виде скриншота и html файла,
а также присутствует возможность узнать какие объявления были удалены.
- [Быстрая установка и запуск с помощью Docker](https://git.soaska.ru/sosiska/eljur/-/wikis/home)
### Экспуатация проекта
##### Скачивание проекта
```bash
git clone https://git.soaska.ru/sosiska/eljur
cd eljur
```
##### Создание .env файла
Используйте [sample.env](https://git.soaska.ru/sosiska/eljur/-/blob/main/sample.env?ref_type=heads), чтобы создать свой .env файл.
[env.example](https://git.soaska.ru/sosiska/eljur/-/blob/main/env.example?ref_type=heads) - пример, как может выглядеть файл .env
```
ELJUR_LOGIN=Vasya2005
ELJUR_PASSWORD=password_example
ELJUR_DOMAIN=2007
TG_TOKEN=123045678:ABCD_Uj3dQwUpDrf6e2-iCmI34v2SEGdZz0
TG_ID=1234567321
TG_API_URL=https://api.telegram.org/
```
- [Как получить данные телеграм](https://git.soaska.ru/sosiska/eljur/-/wikis/How-to-get-telegram-data)
#### Подготовка к запуску проекта
1) Установите библиотеки:
```bash
go mod download
```
2) Перенесите папку data:
```bash
cp -r Golang/data .
```
3) Запустите проект:
```bash
go run .
```
> **Примечание:** \
Скомпилированный бинарник можно взять [здесь](https://git.soaska.ru/sosiska/eljur/-/packages/2). Используйте Эту команду как п.3 в случае использования скомпилированного бинарника:
```bash
curl https://git.soaska.ru/api/v4/projects/13/packages/generic/bin/2.0B/eljur-bot -o eljur-bot
./eljur-bot
```

7
env.example Normal file
View File

@ -0,0 +1,7 @@
ELJUR_LOGIN=Vasya2005
ELJUR_PASSWORD=password_example
ELJUR_DOMAIN=2007
TG_TOKEN=123045678:ABCD_Uj3dQwUpDrf6e2-iCmI34v2SEGdZz0
TG_ID=1234567321
TG_API_URL=https://api.telegram.org/

28
go.mod Normal file
View File

@ -0,0 +1,28 @@
module git.soaska.ru/sosiska/eljur-bot
go 1.21.2
require (
github.com/Sosiaka/htmldiff v0.0.0-20231014152201-9444409b44c3 // indirect
github.com/anaskhan96/soup v1.2.5 // indirect
github.com/aryann/difflib v0.0.0-20210328193216-ff5ff6dc229b // indirect
github.com/danwakefield/fnmatch v0.0.0-20160403171240-cbb64ac3d964 // indirect
github.com/documize/html-diff v0.0.0-20160503140253-f61c192c7796 // indirect
github.com/go-jose/go-jose/v3 v3.0.0 // indirect
github.com/go-rod/rod v0.114.4 // indirect
github.com/go-stack/stack v1.8.1 // indirect
github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1 // indirect
github.com/joho/godotenv v1.5.1 // indirect
github.com/mb0/diff v0.0.0-20131118162322-d8d9a906c24d // indirect
github.com/myENA/html-diff v0.0.0-20200616131349-b5478371670f // indirect
github.com/playwright-community/playwright-go v0.3800.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/ysmood/fetchup v0.2.3 // indirect
github.com/ysmood/goob v0.4.0 // indirect
github.com/ysmood/got v0.34.1 // indirect
github.com/ysmood/gson v0.7.3 // indirect
github.com/ysmood/leakless v0.8.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/net v0.17.0 // indirect
golang.org/x/text v0.13.0 // indirect
)

64
go.sum Normal file
View File

@ -0,0 +1,64 @@
github.com/Sosiaka/html-diff v0.0.0-20231014151826-7a46f9715518 h1:2sFP+caHJJO1XUWoF4yUzm/w6V/2zY16NKc9HM/qVF4=
github.com/Sosiaka/html-diff v0.0.0-20231014151826-7a46f9715518/go.mod h1:FdPp57hpkPZTPd76i5TmQ40Kz7XQFRoZmP2c6srgQfY=
github.com/Sosiaka/htmldiff v0.0.0-20231014152201-9444409b44c3 h1:Jdj/LV+0ddFb2U6n1Ohg5uCCn/bLR1qZobqGRNArz/U=
github.com/Sosiaka/htmldiff v0.0.0-20231014152201-9444409b44c3/go.mod h1:xxmvkoUBGtrPNIiQaHV3+PvAVZIUcuWqSOibvPZwf90=
github.com/anaskhan96/soup v1.2.5 h1:V/FHiusdTrPrdF4iA1YkVxsOpdNcgvqT1hG+YtcZ5hM=
github.com/anaskhan96/soup v1.2.5/go.mod h1:6YnEp9A2yywlYdM4EgDz9NEHclocMepEtku7wg6Cq3s=
github.com/aryann/difflib v0.0.0-20210328193216-ff5ff6dc229b h1:uUXgbcPDK3KpW29o4iy7GtuappbWT0l5NaMo9H9pJDw=
github.com/aryann/difflib v0.0.0-20210328193216-ff5ff6dc229b/go.mod h1:DAHtR1m6lCRdSC2Tm3DSWRPvIPr6xNKyeHdqDQSQT+A=
github.com/danwakefield/fnmatch v0.0.0-20160403171240-cbb64ac3d964 h1:y5HC9v93H5EPKqaS1UYVg1uYah5Xf51mBfIoWehClUQ=
github.com/danwakefield/fnmatch v0.0.0-20160403171240-cbb64ac3d964/go.mod h1:Xd9hchkHSWYkEqJwUGisez3G1QY8Ryz0sdWrLPMGjLk=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/documize/html-diff v0.0.0-20160503140253-f61c192c7796 h1:CuipXymSP8DiNHYVGWak4cF2IbYFQeL5CZ37Y6aDVq4=
github.com/documize/html-diff v0.0.0-20160503140253-f61c192c7796/go.mod h1:GTEVMy1JkyV+k/j8hLGRGHVs/IHJS4s7AtJJ9LSYjRQ=
github.com/go-jose/go-jose/v3 v3.0.0 h1:s6rrhirfEP/CGIoc6p+PZAeogN2SxKav6Wp7+dyMWVo=
github.com/go-jose/go-jose/v3 v3.0.0/go.mod h1:RNkWWRld676jZEYoV3+XK8L2ZnNSvIsxFMht0mSX+u8=
github.com/go-rod/rod v0.114.4 h1:FpkNFukjCuZLwnoLs+S9aCL95o/EMec6M+41UmvQay8=
github.com/go-rod/rod v0.114.4/go.mod h1:aiedSEFg5DwG/fnNbUOTPMTTWX3MRj6vIs/a684Mthw=
github.com/go-stack/stack v1.8.1 h1:ntEHSVwIt7PNXNpgPmVfMrNhLtgjlmnZha2kOpuRiDw=
github.com/go-stack/stack v1.8.1/go.mod h1:dcoOX6HbPZSZptuspn9bctJ+N/CnF5gGygcUP3XYfe4=
github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1 h1:wG8n/XJQ07TmjbITcGiUaOtXxdrINDz1b0J1w0SzqDc=
github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1/go.mod h1:A2S0CWkNylc2phvKXWBBdD3K0iGnDBGbzRpISP2zBl8=
github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/mb0/diff v0.0.0-20131118162322-d8d9a906c24d h1:eAS2t2Vy+6psf9LZ4T5WXWsbkBt3Tu5PWekJy5AGyEU=
github.com/mb0/diff v0.0.0-20131118162322-d8d9a906c24d/go.mod h1:3YMHqrw2Qu3Liy82v4QdAG17e9k91HZ7w3hqlpWqhDo=
github.com/myENA/html-diff v0.0.0-20200616131349-b5478371670f h1:8AfNa+QS4jUxg/oaAQn0rb7JCF39bfP8jyxHzZnWpgo=
github.com/myENA/html-diff v0.0.0-20200616131349-b5478371670f/go.mod h1:B4c7Jsz+iqBoliHTdFpFsH/CJOl0kCF4fGyFuemjfgU=
github.com/playwright-community/playwright-go v0.3800.0 h1:9ATUzVh8Hio6W1LvfjPX76PSR9jPc5YwyOzNzMzQV6w=
github.com/playwright-community/playwright-go v0.3800.0/go.mod h1:mbNzMqt04IVRdhVfXWqmCxd81gCdL3BA5hj6/pVAIqM=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/ysmood/fetchup v0.2.3 h1:ulX+SonA0Vma5zUFXtv52Kzip/xe7aj4vqT5AJwQ+ZQ=
github.com/ysmood/fetchup v0.2.3/go.mod h1:xhibcRKziSvol0H1/pj33dnKrYyI2ebIvz5cOOkYGns=
github.com/ysmood/goob v0.4.0 h1:HsxXhyLBeGzWXnqVKtmT9qM7EuVs/XOgkX7T6r1o1AQ=
github.com/ysmood/goob v0.4.0/go.mod h1:u6yx7ZhS4Exf2MwciFr6nIM8knHQIE22lFpWHnfql18=
github.com/ysmood/gop v0.0.2/go.mod h1:rr5z2z27oGEbyB787hpEcx4ab8cCiPnKxn0SUHt6xzk=
github.com/ysmood/got v0.34.1 h1:IrV2uWLs45VXNvZqhJ6g2nIhY+pgIG1CUoOcqfXFl1s=
github.com/ysmood/got v0.34.1/go.mod h1:yddyjq/PmAf08RMLSwDjPyCvHvYed+WjHnQxpH851LM=
github.com/ysmood/gotrace v0.6.0/go.mod h1:TzhIG7nHDry5//eYZDYcTzuJLYQIkykJzCRIo4/dzQM=
github.com/ysmood/gson v0.7.3 h1:QFkWbTH8MxyUTKPkVWAENJhxqdBa4lYTQWqZCiLG6kE=
github.com/ysmood/gson v0.7.3/go.mod h1:3Kzs5zDl21g5F/BlLTNcuAGAYLKt2lV5G8D1zF3RNmg=
github.com/ysmood/leakless v0.8.0 h1:BzLrVoiwxikpgEQR0Lk8NyBN5Cit2b1z+u0mgL4ZJak=
github.com/ysmood/leakless v0.8.0/go.mod h1:R8iAXPRaG97QJwqxs74RdwzcRHT1SWCGTNqY8q0JvMQ=
go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=
go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20190911031432-227b76d455e7/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20200114155413-6afb5195e5aa h1:F+8P+gmewFQYRk6JoLQLwjBCTu3mcIURZfNkVweuRKA=
golang.org/x/net v0.0.0-20200114155413-6afb5195e5aa/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.17.0 h1:pVaXccu2ozPjCXewfr1S7xza/zcXTity9cCdXQYSjIM=
golang.org/x/net v0.17.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.13.0 h1:ablQoSUd0tRdKxZewP80B+BaqeKJuVhuRxj/dkrun3k=
golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

123
main.go Normal file
View File

@ -0,0 +1,123 @@
package main
import (
"time"
"github.com/go-rod/rod"
"github.com/go-rod/rod/lib/launcher"
)
// Старый вариант взятия разницы
//func get_difference(old_string, new_string string) {
// difference := " "
//
// var cfg = &htmldiff.Config{
// Granularity: 5,
// InsertedSpan: []htmldiff.Attribute{{Key: "style", Val: "background-color: lightgreen;"}},
// DeletedSpan: []htmldiff.Attribute{{Key: "style", Val: "background-color: lightpink;"}},
// ReplacedSpan: []htmldiff.Attribute{{Key: "style", Val: "background-color: lightskyblue;"}},
// CleanTags: []string{""},
// }
// res, err := cfg.HTMLdiff([]string{old_string, new_string})
// if err != nil {
// log.Fatal("cant make difference html body:", err)
// } else {
// difference = res[0]
// }
// update_file(difference, "data/difference.html")
//}
func item_status(item board_item, table []board_item) (bool, []int) {
is_new := true
edit_pattern := []int{0, 0, 0, 0, 0, 0}
i := 0
for i < len(table) {
if table[i].title == item.title {
is_new = false
if table[i].active != item.active {
edit_pattern[0] = i
}
if table[i].dates != item.dates {
edit_pattern[1] = i
}
if table[i].content != item.content {
edit_pattern[2] = i
}
if len(table[i].attachments) != len(item.attachments) {
edit_pattern[3] = i
} else {
j := 0
for j < len(item.attachments) {
if item.attachments[j] != table[i].attachments[j] {
edit_pattern[3] = i
}
j++
}
if table[i].creator != item.creator {
edit_pattern[4] = i
}
if table[i].receivers != item.receivers {
edit_pattern[5] = i
}
}
i++
}
}
return is_new, edit_pattern
}
func update_patterns(old_table, new_table []board_item) ([]board_item, []board_item) {
i := 0
for i < len(new_table) {
new_table[i].unique, new_table[i].edit_pattern = item_status(new_table[i], old_table)
}
i = 0
for i < len(old_table) {
old_table[i].unique, old_table[i].edit_pattern = item_status(old_table[i], new_table)
}
return old_table, new_table
}
func generate_message(old_table, new_table []board_item) {
// message := "Новые объявления \n"
old_table, new_table = update_patterns(old_table, new_table)
// for table := range new_table {
//
// }
}
func run(browser *rod.Browser, LOGIN, PASSWORD, ELJUR_URL, TELEGRAM_CHAT_ID, TELEGRAM_API_URL string) {
new_table := scape(browser, LOGIN, PASSWORD, ELJUR_URL)
// Старые методы
// old_table := get_file("data/table.html")
// old_last_post := ""
// if strings.Contains(old_table, `<div class="board-item__footer">`) {
// old_last_post = old_table[strings.Index(old_table, `<h1 class="page-title">Объявления</h1>`):strings.Index(old_table, `<div class="board-item__footer">`)]
// }
// if new_table != " " && new_table != old_table {
// update_file(new_table, "data/table.html")
// if new_last_post != old_last_post {
// send_photo("data/last_post.png", TELEGRAM_API_URL, TELEGRAM_CHAT_ID)
// } else {
// send_photo("data/sad.png", TELEGRAM_API_URL, TELEGRAM_CHAT_ID)
// }
// get_difference(old_table, new_table)
// send_document("data/difference.html", TELEGRAM_API_URL, TELEGRAM_CHAT_ID)
// }
println(new_table)
}
func main() {
LOGIN, PASSWORD, ELJUR_URL, TELEGRAM_CHAT_ID, TELEGRAM_API_URL := load_env_vars()
path, _ := launcher.LookPath()
launcher := launcher.New().Bin(path).MustLaunch()
browser := rod.New().ControlURL(launcher).MustConnect()
for {
run(browser, LOGIN, PASSWORD, ELJUR_URL, TELEGRAM_CHAT_ID, TELEGRAM_API_URL)
time.Sleep(400 * time.Second)
}
}

7
sample.env Normal file
View File

@ -0,0 +1,7 @@
ELJUR_LOGIN='your login'
ELJUR_PASSWORD='your pass'
ELJUR_DOMAIN='your school number'
TG_TOKEN='telegram bot token'
TG_ID='your id in telegram'
TG_API_URL='your telegram API server url'

99
scaper.go Normal file
View File

@ -0,0 +1,99 @@
package main
import (
"fmt"
"log"
"net/http"
"time"
"github.com/go-rod/rod"
)
func scape(browser *rod.Browser, LOGIN, PASSWORD, ELJUR_URL string) []board_item {
response, err := http.Get(ELJUR_URL + "journal-board-action")
if err != nil {
log.Fatal("Error while request:", err)
} else if response.StatusCode != 200 {
log.Fatalln("Eljur status:", response.Status)
} else {
content := get_content(browser, LOGIN, PASSWORD, ELJUR_URL)
log.Println("Content got")
return content
// Старый метод
//// Проверка на возможность среза
//if strings.Contains(content, `<span class="control-label lgray">`) {
// all_posts := content[strings.Index(content, `<h1 class="page-title">Объявления</h1>`):strings.Index(content, `<span class="control-label lgray">`)]
// // Почему-то в начале страницы около 108 открывающихся тегов <br>
// all_posts = strings.ReplaceAll(all_posts, `<br>`, ``)
// last_post := content[strings.Index(content, `<h1 class="page-title">Объявления</h1>`):strings.Index(content, `<div class="board-item__footer">`)]
//
// return all_posts, last_post
//}
}
return []board_item{}
}
func parse(page_HTML string) board_item {
// println(page_HTML) // тест
// Тут будет обрабатываться объявление
var page_item board_item
return page_item
}
// wait всегда ли работает?
func get_content(browser *rod.Browser, LOGIN, PASSWORD, ELJUR_URL string) []board_item {
t := 500 * time.Millisecond // максимальное время выполнения каждой функции chromium
item_table := []board_item{}
// Создание новой вкладки
page := browser.MustPage(ELJUR_URL + "journal-board-action")
page.WaitLoad()
time.Sleep(t)
log.Println(page.MustInfo().URL)
// Авторизация
if page.MustInfo().URL == ELJUR_URL+`authorize?return_uri=%2Fjournal-board-action` {
page.MustElementX("/html/body/div[1]/div/main/div/div/div/div/form/div[1]/div[1]/div/input").MustInput(LOGIN)
page.MustElementX("/html/body/div[1]/div/main/div/div/div/div/form/div[1]/div[2]/div/input").MustInput(PASSWORD)
page.MustElement("#loginviewport > div > div > form > div.ej-form__footer > button").MustClick()
// Загрузка сайта после входа
page.WaitLoad()
time.Sleep(t)
log.Println(page.MustInfo().URL)
}
// Проверка входа
if page.MustInfo().URL == ELJUR_URL+`authorize?return_uri=%2Fjournal-board-action` {
log.Fatal("Wrong eljur credentials")
// os.Exit(127) -- вдруг wait неверный
return []board_item{}
} else if page.MustInfo().URL != ELJUR_URL+"journal-board-action" {
page.Navigate(ELJUR_URL + "journal-board-action")
page.WaitLoad()
time.Sleep(t)
log.Println(page.MustInfo().URL)
}
// Сбор информации о объявлениях
item_number := 1
for {
item_selector, err := page.Timeout(t).Element(fmt.Sprintf("#board-items > div:nth-child(%d)", item_number))
if err != nil {
break
}
item_table = append(item_table, parse(item_selector.MustHTML()))
// Точно плохая идея!
// item_selector.MustScreenshot(fmt.Sprintf("data/screenshot%d.png", item_number))
item_number++
}
println("Hmm")
// Старые методы
// content := page.MustHTML()
// page.MustElement("#board-items > div:nth-child(1)").MustScreenshot("data/last_post.png")
page.Close()
return item_table
}

204
worker.go Normal file
View File

@ -0,0 +1,204 @@
package main
import (
"bytes"
"fmt"
"io"
"log"
"mime/multipart"
"net/http"
"os"
)
type board_item struct {
active bool
dates date_pair
title string
content string
attachments []outline
creator string
receivers string
edit_pattern []int
unique bool
}
type date_pair struct {
begin string
end string
}
type outline struct {
title string
link string
doctype string
}
func load_env_vars() (string, string, string, string, string) {
LOGIN := os.Getenv("ELJUR_LOGIN")
PASSWORD := os.Getenv("ELJUR_PASSWORD")
DOMAIN := os.Getenv("ELJUR_DOMAIN")
TG_TOKEN := os.Getenv("TG_TOKEN")
TG_ID := os.Getenv("TG_ID")
TG_API_URL := os.Getenv("TG_API_URL")
return LOGIN, PASSWORD, fmt.Sprintf("https://%s.eljur.ru/", DOMAIN), TG_ID, TG_API_URL + "bot" + TG_TOKEN + `/`
}
func update_file(val, path string) {
var file, err = os.OpenFile(path, os.O_RDWR, 0644)
if err != nil {
log.Fatal("cant update ", path, " : ", err)
os.Exit(1)
} else {
file.Truncate(0)
file.Write([]byte(val))
file.Close()
log.Println(path, "updated")
}
defer file.Close()
}
func get_file(path string) string {
var val, err = os.ReadFile(path)
if err != nil {
log.Fatal("cant get", path, ":", err)
return " "
} else {
log.Println("data/table.html got")
return string(val)
}
}
// Send photo & docs
func send_document(path, TELEGRAM_API_URL, TELEGRAM_CHAT_ID string) {
url := TELEGRAM_API_URL + "sendDocument"
var b bytes.Buffer
w := multipart.NewWriter(&b)
// Open the file
f, err := os.Open(path)
if err != nil {
log.Fatalf("cannot open file: %v", err)
}
defer f.Close()
// Add file as stream of bytes
fw, err := w.CreateFormFile("document", f.Name())
if err != nil {
log.Fatalf("cannot create form file: %v", err)
}
_, err = io.Copy(fw, f)
if err != nil {
log.Fatalf("cannot write file to form file: %v", err)
}
// Add other fields
err = w.WriteField("chat_id", TELEGRAM_CHAT_ID)
if err != nil {
log.Fatalf("cannot write chat_id to form: %v", err)
}
// Close the writer
err = w.Close()
if err != nil {
log.Fatalf("cannot close writer: %v", err)
}
// Create a client
client := &http.Client{}
// Create a new request
req, err := http.NewRequest("POST", url, &b)
if err != nil {
log.Fatalf("cannot create request: %v", err)
}
// Set the content type, this is important
req.Header.Set("Content-Type", w.FormDataContentType())
// Submit the request
res, err := client.Do(req)
if err != nil {
log.Fatalf("HTTP error occurred: %v", err)
}
defer res.Body.Close()
// Check the response
if res.StatusCode == http.StatusOK {
log.Println("photo sent")
} else {
body, err := io.ReadAll(res.Body)
if err != nil {
log.Fatalf("cannot read response body: %v", err)
}
log.Printf("can't send photo: %d - %s", res.StatusCode, string(body))
}
}
func send_photo(path, TELEGRAM_API_URL, TELEGRAM_CHAT_ID string) {
url := TELEGRAM_API_URL + "sendPhoto"
var b bytes.Buffer
w := multipart.NewWriter(&b)
// Open the file
f, err := os.Open(path)
if err != nil {
log.Fatalf("cannot open file: %v", err)
}
defer f.Close()
// Add file as stream of bytes
fw, err := w.CreateFormFile("photo", f.Name())
if err != nil {
log.Fatalf("cannot create form file: %v", err)
}
_, err = io.Copy(fw, f)
if err != nil {
log.Fatalf("cannot write file to form file: %v", err)
}
// Add other fields
err = w.WriteField("chat_id", TELEGRAM_CHAT_ID)
if err != nil {
log.Fatalf("cannot write chat_id to form: %v", err)
}
// Close the writer
err = w.Close()
if err != nil {
log.Fatalf("cannot close writer: %v", err)
}
// Create a client
client := &http.Client{}
// Create a new request
req, err := http.NewRequest("POST", url, &b)
if err != nil {
log.Fatalf("cannot create request: %v", err)
}
// Set the content type, this is important
req.Header.Set("Content-Type", w.FormDataContentType())
// Submit the request
res, err := client.Do(req)
if err != nil {
log.Fatalf("HTTP error occurred: %v", err)
}
defer res.Body.Close()
// Check the response
if res.StatusCode == http.StatusOK {
log.Println("photo sent")
} else {
body, err := io.ReadAll(res.Body)
if err != nil {
log.Fatalf("cannot read response body: %v", err)
}
log.Printf("can't send photo: %d - %s", res.StatusCode, string(body))
}
}