ransbachm

Untitled

Nov 9th, 2021
18
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.61 KB | None | 0 0
  1. # * * * * *  cd <folder with project>; /usr/bin/python3 <folder with project>/main.py > <folder with project>/log.txt
  2. # Replace EVERY "<>" with the correct content (should be 3)
  3.  
  4. import asyncio
  5. from pyppeteer import launch
  6. import requests
  7.  
  8.  
  9. threadName = "<>"
  10. fileName = "store.txt"
  11. webhookUrl = "<>"
  12.  
  13.  
  14. async def getPostCount(threadName):
  15.     browser = await launch(headless=True)
  16.     page = await browser.newPage()
  17.     await page.goto('<>', {'waitUntil' : 'domcontentloaded'})
  18.  
  19.     await page.waitFor(2000) # wait for js
  20.  
  21.     namesAndComments = await page.evaluate('''() => {
  22.        namesAndComments = [...document.querySelector("table").querySelector("tbody").querySelectorAll("tr")].map(e => [e.children[0].innerText, e.children[4].innerText]);
  23.        return namesAndComments;
  24.            
  25.    }''')
  26.  
  27.     for pair in namesAndComments:
  28.         if pair[0] == threadName:
  29.             toReturn = pair[1]
  30.     await browser.close()
  31.     return toReturn
  32.  
  33. async def main():
  34.     numPosts = -1
  35.     try:
  36.         file = open(fileName, "r")
  37.         numPosts = int(file.read())
  38.         file.close()
  39.         file = open(fileName, "w")
  40.     except FileNotFoundError:
  41.         file = open(fileName, "w+")
  42.  
  43.     newNumPosts = int(await getPostCount(threadName))
  44.     if(newNumPosts > numPosts):
  45.         print("number changed")
  46.         data = {"content": f"New post(s) created! [{numPosts}] -> [{newNumPosts}]"}
  47.         numPosts = newNumPosts
  48.         requests.post(webhookUrl, data=data)
  49.  
  50.     print(numPosts)
  51.     file.write(str(numPosts))
  52.  
  53.     file.close()    
  54.  
  55. asyncio.get_event_loop().run_until_complete(main())
Advertisement
Add Comment
Please, Sign In to add comment