Advertisement
Guest User

Scrape torrentleech.org for invite news and send email

a guest
Dec 11th, 2017
3,297
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.50 KB | None | 0 0
  1. #!/usr/bin/python3
  2. ## this script loads torrentleech.org website, checks for any news (usually invites) and if there's anything new,
  3. ## sends alert from your gmail address. Use python3 and install lxml library via pip3 install lxml
  4. ## author: ramirezzRK
  5.  
  6. from lxml import html
  7. from email.mime.text import MIMEText
  8. import requests
  9. import smtplib
  10.                                                         # initialising e-mail variables, please fill up with your information
  11. emailTo = "sendto@email.com"                            # where to send alert
  12. emailFrom = "your.email@gmail.com"
  13. emailSMTPServer = "smtp.gmail.com"
  14. emailUsername = "your.email@gmail.com"
  15. emailPassword = "yourPassword"
  16. newsBodyList = []                                       # initialising empty list of articles
  17.  
  18. page = requests.get('https://www.torrentleech.org/')    # parsing website for data
  19. tree = html.fromstring(page.content)                    # we create 2 lists, one for headlines and one for body content
  20. newsHeadline = tree.xpath('//*[@id="news"]/div[*]/h3/text()')   # creating headlines list
  21. newsCount = len(newsHeadline)
  22.  
  23. for x in range (0, newsCount):                          # creating body list
  24.                                                         # this populates newsBodyList with content. new entry for each article
  25.     newsBody =  tree.xpath('//*[@id="news"]/div[' + str(x + 1) + ']/p//text()')
  26.     newsBodyList.append("".join(newsBody))
  27.  
  28.     # print ()                                          # uncomment to print all Headlines and Article Body to stdout (screen)
  29.     # print (newsHeadline[x])
  30.     # print ('    ', end="")
  31.     # for y in newsBody:
  32.         # print (y, end="")
  33.     # print ()
  34.  
  35. try:
  36.     file = open('entries.dat', 'r')                     # open file with previous data
  37.     lines = file.read().splitlines()                    # read articles from file, split entries in list by newline
  38. except IOError:
  39.     file = open('entries.dat', 'x')                     # create file if it doesn't exist
  40.     lines = []
  41. file.close()
  42.  
  43. if lines != newsBodyList:                               # if there's new articles send email
  44.     file = open('entries.dat', 'w')                     # write all articles to file, just body, no headline
  45.     file.write("\n".join(newsBodyList))
  46.     file.close()
  47.     for item in lines:                                  # remove old articles from both Headline and Body lists
  48.         index = newsBodyList.index(item)
  49.         newsBodyList.pop(index)
  50.         newsHeadline.pop(index)
  51.  
  52.     s = smtplib.SMTP_SSL(emailSMTPServer)               # e-mail server setup
  53.     s.login(emailUsername, emailPassword)
  54.     for item in newsBodyList:                           # send e-mail with new articles
  55.         msg = MIMEText(item)
  56.         msg['Subject'] = newsHeadline[newsBodyList.index(item)]
  57.         msg['From'] = emailFrom
  58.         msg['To'] = emailTo
  59.         s.sendmail(emailFrom, emailTo, msg.as_string())
  60.     s.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement