Skip to content
This repository has been archived by the owner on Sep 24, 2024. It is now read-only.

Update to v4 #62

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 0 additions & 64 deletions .env

This file was deleted.

28 changes: 0 additions & 28 deletions .github/ISSUE_TEMPLATE/bug_report.md

This file was deleted.

13 changes: 5 additions & 8 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
FROM python:3.10-alpine
FROM python:3.11-alpine
WORKDIR /code

COPY requirements.txt ./
COPY ./requirements.txt ./

RUN apk update &&\
apk add --no-cache --virtual .build-deps \
build-base \
gcc &&\
apk add --no-cache --virtual .build-deps build-base &&\
pip install --user -r requirements.txt &&\
apk del .build-deps &&\
rm -rf /root/* /tmp/*

COPY . .
ADD vktgbot .
COPY ./vktgbot ./vktgbot

CMD [ "python", "vktgbot" ]
CMD python -m vktgbot
93 changes: 93 additions & 0 deletions config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
section_name:
# Link or ID of the Telegram channel.
# for example:
# tg_channel = "@durov"
# tg_channel = -1234567890987
# Don't forget to add bot to this channel as an administrator!
tg_channel: "@aaaa"

# Token for your Telegram bot.
# You can get it here: https://t.me/BotFather
tg_bot_token: "1234567890:AAA-AaA1aaa1AAaaAa1a1AAAAA-a1aa1-Aa"

# Personal token for your VK profile.
# You can get it here:
# https://github.com/alcortazzo/vktgbot/wiki/How-to-get-personal-access-token
vk_token: "00a0a0ab00f0a0ab00f0a6ab0c00000b0f000f000f0a0ab0a00b000000dd00000000de0"

# Part of the link (after vk.com/) to the VK channel
# for example:
# if link is vk.com/>>>example<<<
# vk_domain: "example"
vk_domain: "bbbb"

# Number of posts to send to Telegram.
# Min value = 2
# Max value = 100
req_count: 3

# Filter to apply:
# "owner" — posts by the wall owner;
# "others" — posts by someone else;
# "all" — posts by the wall owner and others
# "postponed" — timed posts (only available for calls with an access_token)
# "suggests" — suggested posts on a community wall
req_filter: owner

# If True bot will stop after first pass through the loop.
single_start: false

# Waiting time (in seconds) between cycle passes.
time_to_sleep: 120

# Set True if you want to skip sponsored posts
skip_ads_posts: true

# Set True if you want to skip posts with specified Copyright
skip_copyrighted_posts: false

# Set True if you don't want to parse reposts
skip_reposts: false

# Words whitelist.
# Bot will repost posts only containing words in whitelist.
# Useful for hashtags.
whitelist:
# Words blacklist.
# If post contains a blacklisted word, the post will be skipped.
blacklist:
# for example:
# whitelist:
# - "#music"
# - "new"
# blacklist:
# - "rap"
# - "dubstep"
# This configuration will keep posts only with music hashtag
# and word "new" excluding posts with words "rap" and "dubstep".

# ID of the last post that was sent to Telegram.
last_kwown_post_id: 0


# If you want to add more channels, just uncomment and edit this section or add new one.

# any_name_you_want:
# tg_channel: "@aaaa"
# tg_bot_token: "1234567890:AAA-AaA1aaa1AAaaAa1a1AAAAA-a1aa1-Aa"
# vk_token: "00a0a0ab00f0a0ab00f0a6ab0c00000b0f000f000f0a0ab0a00b000000dd00000000de0"
# vk_domain: "bbbb"

# req_count: 5
# req_filter: owner

# single_start: false
# time_to_sleep: 120
# skip_ads_posts: true
# skip_copyrighted_posts: false
# skip_reposts: false
# whitelist:
# - "example text"
# blacklist:
# - "another example text"
# last_kwown_post_id: 0
8 changes: 8 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
version: "3"

services:
app:
build: .
volumes:
- ./logs:/code/logs
- ./config.yaml:/code/config.yaml
8 changes: 0 additions & 8 deletions docker/docker-compose.yml

This file was deleted.

1 change: 0 additions & 1 deletion last_id.txt

This file was deleted.

7 changes: 4 additions & 3 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
aiogram
requests
aiogram>=3.0.0
aiohttp
loguru
python-dotenv
pydantic
ruamel.yaml
63 changes: 42 additions & 21 deletions vktgbot/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,44 +2,65 @@
Telegram Bot for automated reposting from VKontakte community pages
to Telegram channels.

v3.1
v4.0
by @alcortazzo
"""

import time
import asyncio
from asyncio import CancelledError, Task

from loguru import logger

from config import SINGLE_START, TIME_TO_SLEEP
from start_script import start_script
from tools import prepare_temp_folder
from vktgbot import tools
from vktgbot.config import Config
from vktgbot.start_script import start_bot_instance

logger.add(
"./logs/debug.log",
format="{time} {level} {message}",
level="DEBUG",
rotation="1 week",
retention="1 month",
compression="zip",
)

logger.info("Script is started.")


@logger.catch
def main():
start_script()
prepare_temp_folder()
@logger.catch(reraise=True)
async def main():
"""
Main function that creates tasks of start_script
function for each section in config.
"""
try:
config = Config()
tools.prepare_folder("temp")
async_tasks: list[Task] = []

# Create tasks for each section in config
for config_name in config.config.keys():
logger.info(f"Bot '{config_name}' is started.")
async_task = asyncio.create_task(coro=start_bot_instance(config_name), name=config_name)
async_tasks.append(async_task)

while True:
try:
main()
if SINGLE_START:
logger.info("Script has successfully completed its execution")
exit()
else:
logger.info(f"Script went to sleep for {TIME_TO_SLEEP} seconds.")
time.sleep(TIME_TO_SLEEP)
except KeyboardInterrupt:
logger.info("Script is stopped by the user.")
exit()
# Wait for tasks to finish and remove them from the list.
# Some tasks will never finish because they are infinite loops.
while True:
await asyncio.sleep(1)
if not len(async_tasks):
break

for async_task in async_tasks[:]:
if async_task.done():
async_task.result()
logger.info(f"Bot '{async_task.get_name()}' is stopped.")
async_tasks.remove(async_task)

except (KeyboardInterrupt, CancelledError):
logger.info("Script is stopped by user.")
logger.info("Script is stopped.")


if __name__ == "__main__":
asyncio.run(main())
Loading