Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- import tabula
- from datetime import datetime
- import csv
- import requests
- from pyunpack import Archive
- import os
- import io
- from pdfminer.converter import TextConverter
- from pdfminer.pdfinterp import PDFPageInterpreter
- from pdfminer.pdfinterp import PDFResourceManager
- from pdfminer.pdfpage import PDFPage
- from vk_api.longpoll import VkLongPoll, VkEventType
- import vk_api
- znam = 0
- # числитель свреху, знаменатель снизу # знаменатель = 1000
- spisok = [
- {'1': '', '2': 'Горбачева А. В. (Введение в специальность)','3': 'Горбачева Н. Ф. (Химия)', '4': 'Сорокина Д. А. (Биология)','5':'','6': ''},
- {'1': 'Солодкая Т. И. (Математика)', '2': 'Сычева В. Ю. (Астрономия)','3': 'Харитонова М. В. (Физическая культура)','4':'','5': '','6': ''},
- {'1': 'Попова С. А. (Литература)', '2': 'Сафарова Л. В. (Информатика)','3': 'Солодкая Т. И. (Математика)','4': '','5': '','6': ''},
- {'1': '', '2': 'Солодкая Т. И. (Математика)','3': 'Макаров А. А. (Обществознание)','4': 'Вьюгов/Алиева (Иностранный язык)','5': 'Попова С. А. (Литература)','6': ''},
- {'1': '', '2': '','3': 'Константинова Н. В. (Физика)','4': 'Макаров А. А. (Обществознание)','5': '','6': ''},
- {'1': '', '2': '','3': 'Спирина Н. В. (История)','4': 'Вьюгов/Алиева (Иностранный язык)','5': '','6': ''},
- {'1': '', '2': 'Горбачева А. В. (Введение в специальность)', '3': 'Горбачева Н. Ф. (Химия)','4': 'Сорокина Д. А. (Биология)', '5': 'Горбачева А. В. (Введение в специальность)', '6': ''},
- {'1': 'Солодкая Т. И. (Математика)', '2': 'Сычева В. Ю. (Астрономия)','3': 'Харитонова М. В. (Физическая культура)', '4': 'Попова С. А. (Литература)', '5': '', '6': ''},
- {'1': 'Попова С. А. (Литература)', '2': 'Сафарова Л. В. (Информатика)', '3': 'Солодкая Т. И. (Математика)', '4': '','5': '', '6': ''},
- {'1': '', '2': 'Солодкая Т. И. (Математика)', '3': 'Макаров А. А. (Обществознание)','4': 'Вьюгов/Алиева (Иностранный язык)', '5': 'Попова С. А. (Литература)', '6': ''},
- {'1': '', '2': '', '3': 'Константинова Н. В. (Физика)', '4': 'Харитонова М. В. (Физическая культура)', '5': '', '6': ''},
- {'1': '', '2': '', '3': 'Спирина Н. В. (История)', '4': 'Спирина Н. В. (История)', '5': '', '6': ''},
- ]
- # +6 надо
- print('Старт')
- def raspisanieOK():
- global raspis
- Archive('zameni.rar').extractall('')
- def extract_text_from_pdf(pdf_path):
- resource_manager = PDFResourceManager()
- fake_file_handle = io.StringIO()
- converter = TextConverter(resource_manager, fake_file_handle)
- page_interpreter = PDFPageInterpreter(resource_manager, converter)
- with open(pdf_path, 'rb') as fh:
- for page in PDFPage.get_pages(fh,
- caching=True,
- check_extractable=True):
- page_interpreter.process_page(page)
- text = fake_file_handle.getvalue()
- # close open handles
- converter.close()
- fake_file_handle.close()
- if text:
- return text
- if __name__=='__main__':
- text = (extract_text_from_pdf('Замены/' + os.listdir("Замены")[int(len(os.listdir("Замены"))) - 1]))
- if 'ЗНАМЕНАТЕЛЬ' in text:
- znam = 1
- # форматирование
- tabula.convert_into('Замены/' + os.listdir("Замены")[int(len(os.listdir("Замены"))) - 1], "zamen.csv",
- output_format="csv", pages='all')
- # сортировка пар
- date = datetime.today().weekday() + 1 # получение завтрашнего дня
- if znam==1: # если знаменатель то
- date + 6
- para1 = '1) ' + spisok[date]['1']
- para2 = '2) ' + spisok[date]['2']
- para3 = '3) ' + spisok[date]['3']
- para4 = '4) ' + spisok[date]['4']
- para5 = '5) ' + spisok[date]['5']
- para6 = '6) ' + spisok[date]['6']
- group = '12 г'+''
- with open('zamen.csv') as csv_file:
- csv_reader = csv.reader(csv_file, delimiter=',')
- line_count = 0
- for row in csv_reader:
- if group in row[1]:
- print('Пара', row[0], 'Заменить ', row[2], 'на', row[3])
- if row[0]=='I':
- para1 = '1) ' + row[3].replace("\n", " ")
- if row[0]=='II':
- para2 = '2) ' + row[3].replace("\n", " ")
- if row[0]=='III':
- para3 = '3) ' + row[3].replace("\n", " ")
- if row[0]=='IV':
- para4 = '4) ' + row[3].replace("\n", " ")
- if row[0]=='V':
- para5 = '5) ' + row[3].replace("\n", " ")
- if row[0]=='VI':
- para6 = '6) ' + row[3].replace("\n", " ")
- raspis = ('' + para1 + '\n' + para2 + '\n' + para3 + '\n' + para4 + '\n' + para5 + '\n' + para6)
- file1 = open(str(datetime.strftime(datetime.now(), "%d.%m.%Y"))+'.txt', "a",encoding='utf8')
- file1.write(raspis)
- file1.close()
- def raspisanie():
- # скачка
- API_ENDPOINT = 'https://cloud-api.yandex.net/v1/disk/public/resources/download?public_key={}'
- def _get_real_direct_link(sharing_link):
- pk_request = requests.get(API_ENDPOINT.format(sharing_link))
- return pk_request.json().get('href')
- def download_yadisk_link(sharing_link, filename):
- direct_link = _get_real_direct_link(sharing_link)
- if direct_link:
- # Try to recover the filename from the link
- download = requests.get(direct_link)
- with open(filename, 'wb') as out_file:
- out_file.write(download.content)
- raspisanieOK()
- else:
- write_msg(event.user_id, '⛔Замен ещё нет⛔')
- download_yadisk_link('https://disk.yandex.ru/d/FBIhy2KSHPDSng','zameni.rar')
- token = ""
- def write_msg(user_id, message):
- vk.method('messages.send', {'user_id': user_id, 'message': message,'random_id' : '0'})
- vk = vk_api.VkApi(token=token)
- longpoll = VkLongPoll(vk)
- from vk_api.longpoll import VkLongPoll, VkEventType
- for event in longpoll.listen():
- if event.type == VkEventType.MESSAGE_NEW and event.to_me and event.text:
- #Слушаем longpoll, если пришло сообщение то:
- if event.text == '/koh': #Если написали заданную фразу
- if event.from_user: #Если написали в ЛС
- write_msg(event.user_id, '✍🏻Думаю...')
- try:
- handle = open(str(datetime.strftime(datetime.now(), "%d.%m.%Y")) + '.txt', "r", encoding='utf8')
- data = handle.readlines() # read just one line
- handle.close()
- raspis = ''
- for i in data:
- raspis += i
- write_msg(event.user_id, '👿Расписание👿\n' + str(raspis))
- except Exception:
- raspisanie()
- write_msg(event.user_id, '👿Расписание👿\n' + str(raspis))
- else:
- write_msg(event.user_id, 'Что бы узнать рассписание напиши /koh')
Add Comment
Please, Sign In to add comment