Advertisement
1nikitas

Untitled

Jun 9th, 2022
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.32 KB | None | 0 0
  1. import asyncio
  2. from CONFIG import token_id
  3. import os
  4. import openpyxl
  5. from aiogram import Bot, Dispatcher, types, executor
  6. from aiogram.dispatcher.filters import Command
  7. from selenium import webdriver
  8. from selenium.webdriver.common.by import By
  9. import shutil
  10. import schedule
  11. import time
  12. import datetime
  13.  
  14.  
  15.  
  16. bot = Bot(token=token_id)
  17. dp = Dispatcher(bot)
  18. a = [421048811, 582897416]
  19.  
  20.  
  21. """
  22. 1. Считывание файлы
  23. 2. Берем ссылки из файла
  24. 3. По ссылкам из файла начинаем парсить раз в день, записывая цены
  25. 3.1 две колонки:
  26.  
  27.  
  28. """
  29.  
  30.  
  31.  
  32.  
  33. @dp.message_handler(Command('admin1703'))
  34. async def admin(message: types.Message):
  35. a.append(message.from_user.id)
  36. print(a)
  37. await message.answer('ID добавлен: Вам доступны функции (загрузка файла и /getFile)')
  38. admins_ids = a
  39.  
  40.  
  41. @dp.message_handler(Command('my_id'))
  42. async def id(message: types.Message):
  43. await message.answer(message.from_user.id)
  44.  
  45. @dp.message_handler(Command('start'))
  46. async def start(message: types.Message):
  47. await message.answer("All Commands:\n/my_id\n/getFile")
  48.  
  49.  
  50. @dp.message_handler(content_types=['document'])
  51. async def doc(message: types.Message):
  52. if message.from_user.id in admins_ids:
  53. # os.mkdir('doc')
  54. path = os.path.abspath(os.path.dirname('doc'))
  55.  
  56. if os.path.exists(os.path.join(path, f'{message.from_user.id}')):
  57. print(f'{path}\{message.from_user.id}')
  58. # await message.document.download(f'{path}\{message.from_user.id}')
  59. # shutil.rmtree(os.path.join(path, f'{message.from_user.id}'))
  60. await message.document.download(os.path.join(path, f'{message.from_user.id}'))
  61.  
  62. else:
  63.  
  64. os.mkdir(f'{message.from_user.id}')
  65.  
  66. await message.document.download(os.path.join(path, f'{message.from_user.id}'))
  67. else:
  68. await message.answer("В доступе отказано!")
  69.  
  70. async def parser(url, time):
  71. options = webdriver.ChromeOptions()
  72. options.add_argument('--headless')
  73. browser = webdriver.Chrome('chromedriver', options=options)
  74. browser.get(url)
  75. book = openpyxl.Workbook()
  76. sheet = book.active
  77. sheet.cell(row=1, column=1).value = "URL"
  78. sheet.cell(row=1, column=2).value = "Name"
  79. sheet.cell(row=1, column=3).value = "Data"
  80. sheet.cell(row=2, column=3).value = "Price"
  81. n = 3
  82. # book.save("Result.xlsx")
  83. # print("Файл успешно сохранён!")
  84. # book.close()
  85. while True:
  86. await asyncio.sleep(time)
  87. try:
  88. browser.get(url)
  89. except Exception as err:
  90. pass
  91. # print(f"Get error {err=}, {type(err)=}\n")
  92. try:
  93. product = browser.find_element(By.TAG_NAME, 'h1')
  94. except Exception as err:
  95. product = ""
  96. # print(f"Find element TAG {err=}, {type(err)=}\n")
  97. try:
  98. price = browser.find_element(By.CLASS_NAME, 'new-price')
  99. except Exception as err:
  100. price = ""
  101. # print(f"Find element CLASS_NAME NEW {err=}, {type(err)=}\n")
  102. try:
  103. product = product.text
  104. except Exception as err:
  105. product = 'Not Found'
  106. # print(f"product.text {err=}, {type(err)=}\n")
  107. try:
  108. price1 = price.text.split(" ")[0]
  109. except Exception as err:
  110. price1 = '0'
  111. print(product, price1)
  112.  
  113. sheet.cell(row=n, column=4).value = price1
  114. sheet.cell(row=n, column=2).value = product
  115. sheet.cell(row=n, column=3).value = "Price"
  116. # sheet.cell(row=1, column=4).value = cur_date
  117. book.save("Result.xlsx")
  118. n += 1
  119.  
  120.  
  121. @dp.message_handler(Command('getFile'))
  122. async def get_file(message: types.Message):
  123. if message.from_user.id in admins_ids:
  124. msg = await message.answer("Началась обработка запроса!")
  125. #path = r'C:\Users\North\PycharmProjects\Torg_Otdel_Ulibka_06_05_2022\documents'
  126. #path = r'/home/daniil/Torg_Otdel_Ulibka_06_05_2022/documents'
  127. path = os.path.abspath(os.path.dirname('doc'))
  128. files = os.listdir(os.path.join(os.path.join(path,f'{message.from_user.id}\documents') ,f''))
  129. file = max(files)
  130. print(file)
  131. path = os.path.join(path, f'{message.from_user.id}\documents\{file}')
  132.  
  133.  
  134.  
  135.  
  136.  
  137. # exel_file.close()
  138. else:
  139. await message.answer("В доступе отказано!")
  140. #
  141. # def parser1(url)
  142.  
  143. # async def sheduled(path, days=0):
  144. # if "/documents" in path:
  145. # pass
  146. # else:
  147. # path = f'{path}/documents'
  148. # files = os.listdir(path)
  149. # exel_file = openpyxl.load_workbook(f'{path}/{files[0]}')
  150. # sheet = exel_file.active
  151. # days = sheet.cell(row=1, column=1).value
  152. # parser = sheet.cell(row=1, column=2).value
  153. # time = sheet.cell(row=1, column=3).value
  154. # print(time, days, parser)
  155. # for i in range(int(days)):
  156. # await schedule.every().day().at(time).do(sheduled(path))
  157.  
  158.  
  159.  
  160. if __name__ == "__main__":
  161. # need user_id + file to
  162. # loop = asyncio.get_event_loop()
  163. # # loop.create_task(sheduled("582897416"))
  164.  
  165. executor.start_polling(dp, skip_updates=True)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement