Advertisement
Guest User

pickit.py

a guest
Aug 15th, 2019
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.01 KB | None | 0 0
  1. # SROUCE: https://www.ownedcore.com/forums/diablo-3/turbohud/turbohud-community-plugins/630376-d3-pickit-importer.html
  2.  
  3. # Make two new directories
  4. #   build_html
  5. #   output
  6.  
  7. # Browse to builds you want on diablofans, 'Save page as' html only into 'build_html' directory and run pickit.py
  8. # builds are in 'output' each in its own ini {CLASS}-{TITLE} taken from the html
  9.  
  10. # USER SETTINGS
  11. fourthree = "three"
  12. # fourthree = "four"
  13.  
  14. # DO NOT change BELOW UNLESS YOU KNOW HOW TO change
  15.  
  16. import json
  17. import requests
  18. import urllib.request, urllib.error, urllib.parse
  19. from bs4 import BeautifulSoup
  20. from collections import OrderedDict
  21. import os
  22.  
  23. # GLOBAL VARIABLES */
  24. soup = None
  25. pickitlist = ""
  26.  
  27. # Build Types
  28. # Build ini with essential items or just build.
  29. # i broke this. Just copy paste like your reddit shit posts.
  30. buildtype = "build"
  31.  
  32. FANS_BASEURL = 'http://www.diablofans.com/builds/'
  33.  
  34. with open(r'data\itemlist.json', 'r') as f:
  35.     itemList = json.load(f)
  36.  
  37. with open(r'data\statlist2.json', 'r') as f1:
  38.     statList = {int(k): v for k, v in json.load(f1).items()}
  39.  
  40. with open(r'data\typelist.json', 'r') as f2:
  41.     typeList = json.load(f2)
  42.  
  43.  
  44. def switch(importance):
  45.     if importance == "1":
  46.         return "Required"
  47.     elif importance == "2":
  48.         return "Recommended"
  49.     elif importance == "3":
  50.         return "Adequate"
  51.  
  52.  
  53. def getiteminfos(name):
  54.     for item in itemList:
  55.         if item['name'].strip() == name:
  56.             return item['type']
  57.  
  58.  
  59. def getitemslot(slot):
  60.     global soup
  61.     slotobj = {}
  62.     itemarr = []
  63.     statarr = []
  64.  
  65.     itemslot_soup = soup.select('#item-{} li[data-item-id]'.format(slot))
  66.     for item in itemslot_soup:
  67.         name = item.find('a').get_text()
  68.         importance = switch(item.get('data-item-importance')[0])
  69.         _type = getiteminfos(name)
  70.  
  71.         print('NAME={} | TYPE={} | SLOT={}'.format(name, _type, slot))
  72.  
  73.         itemdetails = {
  74.             'name': name,
  75.             'type': _type,
  76.             'importance': importance,
  77.         }
  78.         itemarr.extend([itemdetails])
  79.  
  80.     statslot_soup = soup.select('#item-{} .item-stat a'.format(slot))
  81.     for stats in statslot_soup:
  82.         stattitle = stats.get('title')
  83.         stattitle_soup = BeautifulSoup(stattitle, 'html.parser')
  84.         statarr.extend([stattitle_soup.get_text().rsplit(':', 1)[0]])
  85.  
  86.     slotobj['items'] = itemarr
  87.     slotobj['stats'] = statarr
  88.     return slotobj
  89.  
  90.  
  91. def getitemtype(name):
  92.     _type = "undefined"
  93.     for item in itemList:
  94.         # print 'item in itemlist: {}'.format(item)
  95.         if name['name'] == item['name']:
  96.             _type = item['type']
  97.             pickityype = typeList[_type]
  98.             return pickityype
  99.  
  100.  
  101. def getpickitstat(stat):
  102.     global statList
  103.     for arr in statList:
  104.         pickit = statList[arr][1]
  105.         full = statList[arr][0]
  106.         if full == stat:
  107.             return pickit
  108.  
  109.  
  110. def generatestring(item):
  111.     global pickitlist
  112.     global fourthree
  113.     k = 0
  114.     string = ""
  115.     while k < len(item['items']):
  116.         name = item['items'][k]['name']
  117.         statcount = len(item['stats'])
  118.         stats = item['stats']
  119.         atleaststring = generateatleaststring(statcount, stats)
  120.         _type = getitemtype(item['items'][k])
  121.  
  122.         if not _type:
  123.             _type = 'fucking-errors-man'
  124.  
  125.         string += _type + ' = name=' + name + ' ' + atleaststring + '\n'
  126.  
  127.         # string += '\n'
  128.         k += 1
  129.     pickitlist += string
  130.  
  131.  
  132. def generatecubestring(name):
  133.     global pickitlist
  134.     _type = None  # err is err
  135.     for item in itemList:
  136.         if name == item['name']:
  137.             _type = item['type']
  138.             pickittype = typeList[_type]
  139.             _type = pickittype
  140.     pickitlist += _type + ' = name=' + name + ' ' + '& can_cubed=1 & cubed=0\n'
  141.  
  142.  
  143. def generateatleaststring(statcount, stats):
  144.     global fourthree
  145.     string = ''
  146.     if not stats or statcount == '0':
  147.         return string
  148.  
  149.     i = 0
  150.  
  151.     while i < len(stats):
  152.         pickit = getpickitstat(stats[i])
  153.         if pickit == 'not_imp':
  154.             stats.pop(i)
  155.             i -= 1
  156.         else:
  157.             i += 1
  158.     string += '& at_least['
  159.     if fourthree == '4':
  160.         string += str(statcount) + ', '
  161.     else:
  162.         string += str(statcount - 1) + ', '
  163.     j = 0
  164.     while j < len(stats):
  165.         pickit = getpickitstat(stats[j])
  166.         if not pickit:
  167.             pickit = 'fucking-errors-man'
  168.         string += pickit
  169.         if j < len(stats) - 1:
  170.             string += ', '
  171.             j += 1
  172.         else:
  173.             string += ']'
  174.             j += 1
  175.             return string
  176.  
  177.  
  178. def gen_build(html):
  179.     global fourthree, buildtype, soup, pickitlist
  180.     page = open(html, 'r')
  181.     soup = BeautifulSoup(page, 'html.parser')
  182.  
  183.     resobj = OrderedDict()
  184.     resobj['build_name'] = soup.find(attrs={'class': 'build-title'}).get_text()
  185.     resobj['build_url'] = FANS_BASEURL + soup.find(attrs={'class': 'd3build-bbcode-button'}).get('data-build-id')
  186.     resobj['build_class'] = soup.find(attrs={'class': 'classBadge'}).get('title')
  187.  
  188.     resobj['item_head'] = getitemslot('head')
  189.     resobj['item_shoulders'] = getitemslot('shoulders')
  190.     resobj['item_amulet'] = getitemslot('amulet')
  191.     resobj['item_torso'] = getitemslot('torso')
  192.     resobj['item_wrists'] = getitemslot('wrists')
  193.     resobj['item_hands'] = getitemslot('hands')
  194.     resobj['item_waist'] = getitemslot('waist')
  195.     resobj['item_legs'] = getitemslot('legs')
  196.     resobj['item_feet'] = getitemslot('feet')
  197.     resobj['item_rings'] = getitemslot('rings')
  198.     resobj['item_weapon'] = getitemslot('weapon')
  199.     resobj['item_offhand'] = getitemslot('offhand')
  200.  
  201.     # buildnumber = 'DH-UE-MultiShot'
  202.     buildnumber = '%s-%s' % (resobj['build_class'], resobj['build_name'])
  203.  
  204.     if soup.select('#kanai-weapon .db-title span'):
  205.         resobj['kanai_weapon'] = soup.select('#kanai-weapon .db-title span')[0].get_text()
  206.         print(('Kanai Weapon Slot | NAME={}'.format(resobj['kanai_weapon'])))
  207.  
  208.     if soup.select('#kanai-armor .db-title span'):
  209.         resobj['kanai_armor'] = soup.select('#kanai-armor .db-title span')[0].get_text()
  210.         print('Kanai Armor Slot | NAME={}'.format(resobj['kanai_armor']))
  211.  
  212.     if soup.select('#kanai-jewelry .db-title span'):
  213.         resobj['kanai_jewelry'] = soup.select('#kanai-jewelry .db-title span')[0].get_text()
  214.         print('Kanai Jewelry Slot | NAME={}'.format(resobj['kanai_jewelry']))
  215.  
  216.     if buildtype == "full":
  217.         with open(r'data\essentials.txt', 'r') as essentials:
  218.             with open('pickit_sc_70.ini', 'w') as a_file:
  219.                 a_file.write(essentials.read())
  220.                 a_file.write('\n')
  221.  
  222.     pickitlist = ';||||' + resobj['build_class'] + '-Build: ' + resobj['build_name'] + ' Link: ' + resobj[
  223.         'build_url'] + ' ||||\n'
  224.  
  225.     for entry in resobj:
  226.         if entry.startswith('item'):
  227.             # print(resobj[entry])
  228.             generatestring(resobj[entry])
  229.         if entry.startswith('kanai'):
  230.             generatecubestring(resobj[entry])
  231.  
  232.     pickitlist += ';|||| End of Build ||||'
  233.     print('')
  234.  
  235.     if buildtype == "full":
  236.         with open(r'data\essentials.txt', 'r') as essentials:
  237.             with open('output/pickit_sc_70.ini', 'w') as a_file:
  238.                 a_file.write(essentials.read())
  239.                 a_file.write('\n')
  240.         with open('output/pickit_sc_70.ini', 'a') as a_file:
  241.             pickitlist += '\n'
  242.             a_file.write(pickitlist)
  243.         if buildtype == "full":
  244.             with open(r'data\essentials2.txt', 'r') as essentials2:
  245.                 with open('output/pickit_sc_70.ini', 'a') as a_file:
  246.                     a_file.write(essentials2.read())
  247.         print('INFO | save successful')
  248.     else:
  249.         with open('output/' + buildnumber + '.ini', 'w') as a_file:
  250.             pickitlist += '\n'
  251.             a_file.write(pickitlist)
  252.         print('INFO | save successful')
  253.  
  254.  
  255. if __name__ == '__main__':
  256.     for build in os.listdir(r'build_html'):
  257.         gen_build(os.path.join('build_html', build))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement