dekyos

destinyscraper

Apr 3rd, 2017 (edited)
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.27 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. """
  3. Destiny Weekly Reset Scraper
  4. @author: MadMerlyn
  5. """
  6. from urllib.request import urlopen
  7. import sys
  8. import re
  9. from bs4 import BeautifulSoup as web
  10.  
  11. ORIGINAL = sys.stdout
  12. HTML = urlopen('http://www.bungie.net/en/Activities')
  13. SOUP = web(HTML, 'lxml')
  14.  
  15. def div_id(obj, _id):
  16.     """BS4 find 'div' objects of the selected class"""
  17.     result = obj.findAll('div', {'class':_id})
  18.     return result
  19.  
  20. def tagwrap(obj, _tag):
  21.     """Wrap 'obj' in html formatted tags of 'Tag'"""
  22.     result = '<{}>'.format(_tag)+obj+'</{}>'.format(_tag)
  23.     return result
  24.  
  25. WEEK = SOUP.find('div', {'data-categoryid':'WEEKLY_ACTIVITIES'})
  26. WEEK_LIST = div_id(WEEK, 'destiny-item-details')
  27.  
  28. def mods_list(title, modlist):
  29.     """Create the list of activity modifiers in HTML format"""
  30.     print('  <div class=\"col-sm-6\">', '<br />', sep='\n    ')
  31.     print('    ', title, sep='')
  32.     print('    <ul>')
  33.     mods = div_id(modlist, 'standardTitle')
  34.     for item in mods:
  35.         mod = item.get_text()
  36.         burn = re.search('Burn', mod)
  37.         arms = re.search('Specialist|Brawler|Small Arms', mod)
  38.         if burn is True:
  39.             mod = mod+' <span class=\"glyphicon glyphicon-fire\"></span>'
  40.         if arms is True:
  41.             mod = mod+' <span class=\"glyphicon glyphicon-screenshot\"></span>'
  42.         print('      ', tagwrap(mod, 'li'), sep='')
  43.     print('    </ul>')
  44.     print('  </div> <!-- end col-sm-6 -->')
  45.  
  46. def nightfall():
  47.     """Get the name of this week's Nightfall"""
  48.     result = WEEK_LIST[0].previous_sibling.previous_sibling.get('style')
  49.     result = re.split(r'pgcrs\/|\.jpg', result)
  50.     result = result[1].replace('strike_', '').replace('_', ' ').title()
  51.     result = tagwrap('Nightfall: '+result, 'h4')
  52.     return result
  53.  
  54. def heroic():
  55.     """Get the name of this week's Heroic story"""
  56.     result = div_id(WEEK_LIST[1], 'subtitle')[0].get_text()
  57.     result = result.replace(' Heroic', '')
  58.     result = tagwrap('Weekly Heroic: '+result, 'h4')
  59.     return result
  60.  
  61. #Write output to activities.txt
  62. with open('activities.txt', 'w+') as out_file:
  63.     sys.stdout = out_file
  64.     mods_list(nightfall(), WEEK_LIST[0])
  65.     mods_list(heroic(), WEEK_LIST[1])
  66.     sys.stdout = ORIGINAL
  67.  
  68. print(r'Scrape completed! File is located: .\activities.txt')
Add Comment
Please, Sign In to add comment