Guest User

Untitled

a guest
Aug 9th, 2020
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.51 KB | None | 0 0
  1. import requests
  2. import gspread
  3. from oauth2client.service_account import ServiceAccountCredentials
  4.  
  5. scope = ["https://spreadsheets.google.com/feeds", 'https://www.googleapis.com/auth/spreadsheets',
  6.          "https://www.googleapis.com/auth/drive.file", "https://www.googleapis.com/auth/drive"]
  7. credbot = ServiceAccountCredentials.from_json_keyfile_name("credbot.json", scope)
  8. client = gspread.authorize(credbot)
  9. sheet = client.open("Definite Guild Daily XP").sheet1
  10.  
  11. data = requests.get("https://api.hypixel.net/guild?key=lol&name=Definite").json()
  12.  
  13. # @bot.command()
  14. # @commands.has_role("Guild Staff")
  15. # @commands.cooldown(1, 45, commands.BucketType.user)
  16. # async def markgxp(ctx):
  17. #     await ctx.channel.purge(limit=1)
  18. #     embed = discord.Embed(title="You Have Marked the Weekly GXP", color=0x77bb40)
  19. #     embed.set_author(name="View the Weekly GXP Document", url="https://shorturl.at/fzJ08")
  20. #     embed.set_footer(text="This command has a 45 second cooldown!")
  21. #     await ctx.author.send(embed=embed)
  22.     xpHist = []
  23.     names = []
  24.     for count in range(len(data['guild']['members'])): # For every guild member:
  25.         res = requests.get("https://playerdb.co/api/player/minecraft/" + data['guild']['members'][count]['uuid']) # Response from server
  26.  
  27.         if res.status_code != 200: # If it wasn't a success, continue the loop and print a message
  28.             ctx.author.send("Error 1: Invaild name from player database API!")
  29.             continue
  30.  
  31.         name = res.json()['data']['player']['username'] # We know it was successful if we got to this point, so it's safe to try and get data from our response
  32.         names.append(name)
  33.  
  34.     # Members' GXP
  35.         xp = data['guild']['members'][count]['expHistory']
  36.         xp = sum(xp.values())
  37.         xpHist.append(xp)
  38.  
  39.     # Weekly Total
  40.     wTotal = sum(xpHist)
  41.     print(xpHist)
  42.  
  43.     ## Google Sheets
  44.  
  45.  
  46.     # Sorting and printing names list
  47.  
  48.     # Nesting values into lists in the list
  49.     def splitNames(names):
  50.         return [[i] for i in names]
  51.  
  52.     def splitXP(xpHist):
  53.         return [[i] for i in xpHist]
  54.  
  55.     for i in range(3):
  56.         names.insert(0, None)
  57.         xpHist.insert(0, None)
  58.  
  59.  
  60.     # Pushes updates from the lists to the google sheet #
  61.     sheet.update('B:B', splitNames(names))
  62.     sheet.update('C:C', splitXP(xpHist))
  63.  
  64.     lastRow = sheet.row_count
  65.     lastRow = lastRow
  66.  
  67.     if sheet.append_row(['Total', wTotal]):
  68.         sheet.delete_rows(lastRow)
  69.     else:
  70.         print("Could not delete outdated 'Total' row!"
Add Comment
Please, Sign In to add comment