Advertisement
Guest User

Untitled

a guest
Dec 16th, 2017
11
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.40 KB | None | 0 0
  1. #!/usr/local/bin/python3.6
  2.  
  3. import discord
  4. import asyncio
  5. import sys
  6. from datetime import datetime
  7. import feedparser
  8. import re
  9. import time
  10. import random
  11. import os
  12.  
  13. client = discord.Client()
  14.  
  15. latestNum = 0
  16. currentNum = 0
  17. link = 'blank'
  18. title = 'blank'
  19. creator = 'blank'
  20.  
  21. last = open("last.txt", "r")    #This file stores the last thread number so if the bot closes it doesn't post repeat threads
  22. latestNum = int(last.read())    #MAKE SURE TO SET THIS FILE PATH
  23. last.close()
  24.  
  25.  
  26. def setLastFile():
  27.     f = open("last.txt", "w")   #MAKE SURE TO SET THIS FILE PATH
  28.     f.write(str(latestNum))
  29.     f.close()
  30. def setCurr(val):
  31.     global currentNum
  32.     currentNum = val
  33. def setLatest(val):
  34.     global latestNum
  35.     latestNum = val
  36. def setLink(val):
  37.     global link
  38.     link = val
  39. def setTitle(val):
  40.     global title
  41.     title = val
  42. def setAuthor(val):
  43.     global creator
  44.     creator = val
  45. def setEqual(latest, curr):
  46.     global latestNum
  47.     global currentNum
  48.  
  49.     latestNum = currentNum
  50.  
  51.  
  52. def getForumThreads():
  53.     rss = feedparser.parse('https://example.net/forums/-/index.rss') #Get the RSS feed from the forums, only get the most recent thread
  54.  
  55.     title = rss.entries[0].title #Grab the title of the thread
  56.     link = rss.entries[0].link #Link to thread
  57.     creator = rss.entries[0].['dc_creator'] #Thread author
  58.     num = int(re.search(r'\d+', link).group()) #Thread id
  59.  
  60.     setCurr(num)
  61.     setLink(link)
  62.     setTitle(title)
  63.     setAuthor(creator)
  64.  
  65. @client.event
  66. async def on_ready():
  67.     print('Logged in as ' + client.user.name + ' @ {:%Y-%b-%d %H:%M:%S}'.format(datetime.now()))
  68.     print('--------------------------------------------')
  69.  
  70. async def rssChecker():
  71.     getForumThreads()
  72.     if(currentNum > latestNum): #Check if the thread that was just aquired is new or not
  73.         mess = 'Hey! **' + creator + '** has posted a thread titled **' + title + '**! Here is the link:' + link
  74.         await client.send_message(discord.Object(id='HIDDEN'), mess) #Set the channel ID
  75.         setEqual(latestNum, currentNum)
  76.         setLastFile()
  77.  
  78. async def background():
  79.     await client.wait_until_ready()
  80.     while not client.is_closed:
  81.         await rssChecker() #Run RSS Check
  82.         await asyncio.sleep(15) #Wait 900 seconds (15 minutes) - Set this number to what ever you want the refresh rate to be
  83.  
  84. client.loop.create_task(background())
  85. client.run('HIDDEN') #Set bot ID
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement