2
\$\begingroup\$

UPDATED - 25-12-2021

Summary:

  • Renamed start.py --> bot.py
  • Modified a lot of code in bot.py
  • Modified code in sitemap.py
  • Created init.py Check the update code in the bottom of this question.

I have just started to learn Python and have developed a small bot for Telegram.

This goal is to download a sitemap.xml file (sample) and compare it for changes with urls_old.txt then send the results in a message to the Telegram channel.

OLD CODE

start.py

import requests, time, sitemap, textwrap

class Telegram:

  def __init__(self):
    self.api = "https://api.telegram.org/bot"
    self.token = "XX"
    self.connection = self.api + self.token

  def get_sitemap(self):
    self.sitemap = sitemap.Sitemap()
    self.sitemap.get_sitemap()
    self.sitemap.parse_sitemap_data()
    self.sitemap.create_file()
    self.sitemap.read_file()
    self.sitemap.update_file()
    self.sitemap.pages_deleted = self.sitemap.get_pages_deleted()
    self.sitemap.pages_created = self.sitemap.get_pages_created()
    if self.sitemap.pages_created == None and self.sitemap.pages_deleted == None:
      self.sm_changes = None
      pass
    elif not self.sitemap.pages_created and not self.sitemap.pages_deleted:
      self.sm_changes = None
      pass
    elif None == self.sitemap.pages_created or not self.sitemap.pages_created:
      self.sitemap.pages_deleted = "\n".join(self.sitemap.pages_deleted)
      self.sm_changes = textwrap.dedent("""
        🖐 Hi folks!

        Pages have been deleted:
        ♻️  - %s
        """ % (self.sitemap.pages_deleted))
    elif None == self.sitemap.pages_deleted or not self.sitemap.pages_deleted:
      self.sitemap.pages_created = "\n".join(self.sitemap.pages_created)
      self.sm_changes = textwrap.dedent("""
        🖐 Hi folks!

        Pages have been created:
        📃- %s
        """ % (self.sitemap.pages_created))
    elif None != self.sitemap.pages_created and None != self.sitemap.pages_deleted and len(self.sitemap.pages_created) != 0 and len(self.sitemap.pages_deleted) != 0:
      self.sitemap.pages_deleted = "\n".join(self.sitemap.pages_deleted)
      self.sitemap.pages_created = "\n".join(self.sitemap.pages_created)
      self.sm_changes = textwrap.dedent("""
        🖐 Hi folks!

        Pages have been created:
        📃 - %s
        Pages have veen deleted:
        ♻️  - %s
        """ % (self.sitemap.pages_created, self.sitemap.pages_deleted))
    else:
      self.sm_changes = None
      pass

  def send_message(self,chat_id):
    self.instance=self.get_sitemap()
    self.message = self.sm_changes
    if None == self.message:
      pass
    else:
      self.url = self.connection + "/sendMessage?chat_id=" + chat_id + '&text={}'.format(self.message)
      requests.get(self.url)

o = Telegram()

while True:
  time.sleep(30)
  o.send_message("XXX")

This is the Sitemap:

import requests, shutil
from bs4 import BeautifulSoup

class Sitemap:

  def __init__(self):
    self.sitemap_url = "https://echemosunbitstazo.es/sitemap.xml"
#    self.sitemap_url = "file:///home/sincorchetes/Projects/echemosunbitstazo-docs/sitemap.xml"
    self.url_old_fn = "urls_old.txt"
    self.url_new_fn = "urls_news.txt"
    self.new_pages = []
    self.old_pages = []
  def get_sitemap(self):
    try:
      self.sitemap_data = requests.get(self.sitemap_url)
      return self.sitemap_data
    except:
      print("Something was wrong")

  def parse_sitemap_data(self):
    self.sitemap_parse_data = BeautifulSoup(self.sitemap_data.text, 'html.parser')
    self.urls = self.sitemap_parse_data.find_all('loc')
    for url in self.urls:
      self.new_pages.append(url.get_text())

  def printp(self):
    print(self.pages_deleted)

  def create_file(self):
    with open(self.url_new_fn,"w") as f:
      for url in self.new_pages:
        f.write(url + "\n")
      f.close()

  def read_file(self):
    with open(self.url_old_fn) as f:
      for url in f.read().splitlines():
        self.old_pages.append(url)
      f.close()

  def update_file(self):
    try:
      shutil.copyfile(self.url_new_fn,self.url_old_fn)
    except:
      print("Something was wrong updating the file.")

  def get_pages_created(self):
    self.pages_created = [i for i in self.new_pages + self.old_pages if i not in self.old_pages]
    return self.pages_created

  def get_pages_deleted(self):
    self.pages_deleted = [i for i in self.new_pages + self.old_pages if i not in self.new_pages]
    return self.pages_deleted

if __name__ == "__main__":
  pass

MODIFIED CODE

sitemap.py

from requests import get as GetSitemap
from textwrap import dedent as FormatMessage
from shutil import copyfile
from bs4 import BeautifulSoup

class Sitemap:

  def __init__(self):
    self.sitemap_url = "https://echemosunbitstazo.es/sitemap.xml"
    self.url_old_fn = "urls_old.txt"
    self.url_new_fn = "urls_news.txt"
    self.new_pages = []
    self.old_pages = []
    self.get_sitemap()
    self.parse_sitemap_data()
    self.create_file()
    self.read_file()
    self.update_file()
    self.pages_deleted = self.get_pages_deleted()
    self.pages_created = self.get_pages_created()

    if self.pages_created == None and self.pages_deleted == None:
      self.sm_changes = None
      pass
    elif not self.pages_created and not self.pages_deleted:
      self.sm_changes = None
      pass
    elif None == self.pages_created or not self.pages_created:
      self.pages_deleted = "\n".join(self.pages_deleted)
      self.sm_changes = FormatMessage("""
        🖐 Hi folks!

        Pages have been deleted:
        ♻️  - %s
        """ % (self.pages_deleted))
    elif None == self.pages_deleted or not self.pages_deleted:
      self.pages_created = "\n".join(self.pages_created)
      self.sm_changes = FormatMessage("""
        🖐 Hi folks!

        Pages have been created:
        📃- %s
        """ % (self.pages_created))
    elif None != self.pages_created and None != self.pages_deleted and len(self.pages_created) != 0 and len(self.pages_deleted) != 0:
      self.pages_deleted = "\n".join(self.pages_deleted)
      self.pages_created = "\n".join(self.pages_created)
      self.sm_changes = FormatMessage("""
        🖐 Hi folks!

        Pages have been created:
        📃 - %s
        Pages have veen deleted:
        ♻️  - %s
        """ % (self.pages_created, self.pages_deleted))
    else:
      self.sm_changes = None
      pass

  def get_sitemap(self):
    try:
      self.sitemap_data = GetSitemap(self.sitemap_url)
      return self.sitemap_data
    except:
      print("Something was wrong")

  def parse_sitemap_data(self):
    self.sitemap_parse_data = BeautifulSoup(self.sitemap_data.text, 'html.parser')
    self.urls = self.sitemap_parse_data.find_all('loc')
    for url in self.urls:
      self.new_pages.append(url.get_text())

  def printp(self):
    print(self.pages_deleted)

  def create_file(self):
    with open(self.url_new_fn,"w") as f:
      for url in self.new_pages:
        f.write(url + "\n")
      f.close()

  def read_file(self):
    with open(self.url_old_fn) as f:
      for url in f.read().splitlines():
        self.old_pages.append(url)
      f.close()

  def update_file(self):
    try:
      copyfile(self.url_new_fn,self.url_old_fn)
    except:
      print("Something was wrong updating the file.")

  def get_pages_created(self):
    self.pages_created = [i for i in self.new_pages + self.old_pages if i not in self.old_pages]
    return self.pages_created

  def get_pages_deleted(self):
    self.pages_deleted = [i for i in self.new_pages + self.old_pages if i not in self.new_pages]
    return self.pages_deleted

if __name__ == "__main__":
  pass

bot.py

from sitemap import Sitemap
from requests import get as PostMessage

class Tbot:

  def __init__(self):
    self.api = "https://api.telegram.org/bot"
    self.token = "XX:XX"
    self.connection = self.api + self.token

  def get_sitemap(self):
    self.sitemap = Sitemap()
    return self.sitemap

  def send_message(self,chat_id):
    self.instance=self.get_sitemap()
    self.message = self.sitemap.sm_changes
    if None == self.message:
      pass
    else:
      self.url = self.connection + "/sendMessage?chat_id=" + chat_id + '&text={}'.format(self.message)
      PostMessage(self.url)


if __name__ == "__main__":
  pass

init.py

import bot
from time import sleep

tbot = bot.Tbot()
while True:
  tbot.send_message("-N")
  sleep(30)

Looks better?

\$\endgroup\$
6
  • 4
    \$\begingroup\$ What is the contents of urls_old.txt and what is a sample message your bot might send, just to get an idea of input & output \$\endgroup\$
    – Greedo
    Dec 22, 2021 at 13:29
  • \$\begingroup\$ Hi Greddo! The content of urls_old.txt is a sitemap old copy. The first time, I don't have a urls_old.txt so It's created with sitemap.xml info. When it's trying to compare once time, sitemap.xml does not have changes, so, urls_old.txt does not have changes too. If I create a new entry or delete an entry in sitemap.xml. when the send_message method is run, detect the changes and send the data changed. After, urls_old.txt will be replaced by new sitemap.info \$\endgroup\$ Dec 22, 2021 at 16:18
  • 1
    \$\begingroup\$ Off-topic, but 25-12-2021 is backwards and hurts my soul. \$\endgroup\$
    – Reinderien
    Dec 27, 2021 at 4:20
  • \$\begingroup\$ What's the mean @Reinderien? \$\endgroup\$ Dec 29, 2021 at 8:58
  • \$\begingroup\$ en.wikipedia.org/wiki/ISO_8601 \$\endgroup\$
    – Reinderien
    Dec 29, 2021 at 18:07

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.