User:Hkm/Rankings program: Difference between revisions

Hkm (talk | contribs)
No edit summary
Hkm (talk | contribs)
No edit summary
Line 1: Line 1:
<syntaxhighlight lang="python" line>
<syntaxhighlight lang="python" line>


'''when adding a new work, the user should put it at the bottom
so that it does not replace the tuning and notes if that work was already there'''
import re
import re
import requests
import requests
from bs4 import BeautifulSoup
from bs4 import BeautifulSoup
import pyperclip


def process_talk(url):
def process_talk(url):
Line 36: Line 39:
                 continue
                 continue
                  
                  
             if len(thing_a) > 40 or len(thing_b) > 40:
             if len(thing_a) > 40 or len(thing_b) > 70:
                 continue
                 continue


Line 73: Line 76:


         raw_text = textarea.get_text()
         raw_text = textarea.get_text()
        (before, after) = raw_text.split('{|', 1)
        (raw_text, after) = after.split('|}', 1)


         # Preprocess the text: replace newline followed by | with ||, then split into entries
         # Preprocess the text: replace newline followed by | with ||, then split into entries
Line 122: Line 128:
             pop = parts[5].strip()
             pop = parts[5].strip()


             print("AUTHOR, NAME, LINK, TUNING") ###############################3
             #print("AUTHOR, NAME, LINK, TUNING") ###############################3
             print(author, name, link, tuning)
             #print(author, name, link, tuning)
             print()
             #print()
              
              


Line 154: Line 160:
         return {}
         return {}


     return result
     return result, before, after
          
          


Line 162: Line 168:
main = "https://en.xen.wiki/index.php?title=User:Hkm/Rankings&action=edit"
main = "https://en.xen.wiki/index.php?title=User:Hkm/Rankings&action=edit"
averages = process_talk(talk)
averages = process_talk(talk)
data = process_main(main)
data, before, after = process_main(main)
 
clip = before
 
clip += '''{| class="wikitable sortable" style="margin: auto;"
! Creator !! Work !! Tuning !! Notes !! R !! #


print('''
'''
{| class="wikitable" style="margin: auto;"
! Creator !! Work !! Tuning !! Notes !! Rating !! Pop ''')


for (author, work), (rating, pop) in sorted(averages.items(), key=lambda a: -a[1][0]):
for (author, work), (rating, pop) in sorted(averages.items(), key=lambda a: -a[1][0]):
     tuning, notes, _, link = data.setdefault((author, work), ("", "", "", ""))
     tuning, notes, _, link = data.setdefault((author, work), ("", "", "", ""))
     if link:
     if link:
         print(f"|-\n| {author} || [{link} {work}] || {tuning} || {notes} || {rating:.2f} || {pop}")
         clip += (f"|-\n| {author} || [{link} {work}] || {tuning} || {notes} || {rating:.2f} || {pop}\n")
     else:
     else:
         print(f"|-\n| {author} || {work} || {tuning} || {notes} || {rating:.2f} || {pop}")
         clip += (f"|-\n| {author} || {work} || {tuning} || {notes} || {rating:.2f} || {pop}\n")
 
clip += "|}"


print("|}")
clip += after
pyperclip.copy(clip)




‎</syntaxhighlight>
‎</syntaxhighlight>