stare112.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. '''Created May 25, 2022 deeejas'''
  2. # coding: UTF-8
  3. import re
  4. from time import sleep
  5. import requests
  6. import gspread
  7. from gspread import BackOffHTTPClient
  8. from bs4 import BeautifulSoup
  9. # g_sheet = '1JW9MyYjZjyLMKtj4bwanq5VbUWaIF0RhZjbQQFd2YFQ' # MZK
  10. g_sheet = '1DUWx4q3MR6YlLkPhXSUWJb_7MjFia13ee5z0mXgq-X8' # FER
  11. # g_sheet = '1jBc-DottcmtttoDFRMdsVVp6nbEmq4ficd58XFy4x-o'
  12. gc = gspread.service_account(filename='Pysheet26134-2daf66659e50.json', http_client=BackOffHTTPClient) # MZK+FER
  13. # gc = gspread.service_account(filename='Pysheet26134-2daf66659e50.json', http_client=BackOffHTTPClient)
  14. gsh = gc.open_by_key(g_sheet)
  15. sh1 = gsh.worksheet('PJ')
  16. col_CUI = sh1.col_values(2)[3:]
  17. col_MESSAGE = sh1.col_values(10)[3:]
  18. col_INDEX = sh1.col_values(4)[3:]
  19. cond_range = zip(col_CUI, col_MESSAGE, col_INDEX)
  20. def state_decl(decl_id: str, vat: str):
  21. payload = {
  22. 'id': decl_id,
  23. 'cui': vat}
  24. headers = {
  25. 'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.5005.61 Safari/537.36',
  26. 'Accept-Encoding': 'gzip, deflate, br',
  27. 'Accept': '*/*',
  28. 'Connection': 'keep-alive'}
  29. url = 'https://www.anaf.ro/StareD112/vizualizareStare.do'
  30. resp = requests.post(url, data=payload, headers=headers)
  31. print(resp.status_code)
  32. html = resp.content
  33. soup = BeautifulSoup(html, 'html.parser')
  34. message = []
  35. for tag in soup.find_all('tr', align='left'):
  36. # print(tag.find_all('td'))
  37. for child in tag.find_all('td'):
  38. child = child.get_text().strip()
  39. message.append(child)
  40. start = message.index(decl_id)
  41. message = message[start:start + 6]
  42. search_str = soup.find_all('td', valign='top')[0].get_text()
  43. cui = re.search('(?<=cui: )[0-9]*', search_str).group()
  44. message.insert(0, cui)
  45. print(message)
  46. # sleep(1)
  47. return message
  48. if __name__ == '__main__':
  49. for cui, message, index_ in cond_range:
  50. # print(message)
  51. condition = cui != '' and message == 'F'
  52. print(condition)
  53. if condition:
  54. state = state_decl(decl_id=index_, vat=cui)
  55. sleep(1)
  56. cell = sh1.find(index_, in_column=4)
  57. sh1.update_cell(cell.row, col=10, value=state[3])
  58. sh1.update_cell(cell.row, col=5, value=state[2])
  59. sh1.update_cell(cell.row, col=7, value=state[6])