Advertisement
Guest User

Untitled

a guest
Dec 4th, 2016
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.21 KB | None | 0 0
  1. from lxml import html
  2. import requests
  3. import praw
  4. import time
  5.  
  6. def RSIAnnounce():
  7.  
  8. # Pulling announcement from RSI webpage.
  9. #######################################
  10. print "Pulling data from RSI..."
  11.  
  12. page = requests.get('https://www.robertsspaceindustries.com/')
  13. tree = html.fromstring(page.text)
  14.  
  15. # Ensuring that if there is no annoucement the program reacts accordingly.
  16.  
  17. try:
  18. caption = tree.xpath('//*[@id="global-notification"]/a/div[1]/div')[0].text_content() #HTML location of data
  19. announce_check = True
  20. except IndexError:
  21. print "No announcement!"
  22. announce_check = False
  23. caption = "No announcement"
  24.  
  25. try:
  26. link = tree.xpath('//*[@id="global-notification"]/a/@href')
  27. except IndexError:
  28. print "No link!"
  29. link = "https://www.robertsspaceindustries.com"
  30. string_link = ''.join(link)
  31.  
  32. ########################################
  33.  
  34.  
  35.  
  36. # Preping and downloading data to reddit wiki.
  37. ########################################
  38.  
  39. announcement = "[**From CIG**: %s](%s#announcebot) \n \n [](http://www.bot.com)" % (caption, string_link)
  40.  
  41. #logging into reddit and downloading sidebar.
  42.  
  43. r = praw.Reddit(user_agent = "Sidebar updater for /r/starcitzen by /u/Jumbify")
  44. r.login(username="USERNAME HERE", password = "PASSWORD HERE", disable_warning = True)
  45.  
  46. sub = "starcitizen"
  47. settings = r.get_settings(sub)
  48. sidebar_contents = settings['description']
  49.  
  50.  
  51. sidebar_split = sidebar_contents.split("[](http://www.bot.com)", 1)
  52. del sidebar_split[0]
  53. sidebar_contents = ''.join(sidebar_split)
  54.  
  55. if announce_check == True:
  56. final_string = announcement + sidebar_contents
  57. else:
  58. no_annoucement = "[](https://www. NO ANNOUNCEMENT CURRENTLY) \n \n [](http://www.bot.com)"
  59. final_string = no_annoucement + sidebar_contents
  60.  
  61.  
  62. r.update_settings(r.get_subreddit(sub), description=final_string)
  63.  
  64. ########################################
  65.  
  66. print "RSIAnnounce complete..."
  67.  
  68. if __name__ == "__main__":
  69. RSIAnnounce()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement