User:Hkm/Rankings program: Difference between revisions

Hkm (talk | contribs)
No edit summary
Hkm (talk | contribs)
No edit summary
Line 7: Line 7:
from bs4 import BeautifulSoup
from bs4 import BeautifulSoup
import pyperclip
import pyperclip
def extract_display_text(wiki_text):
    # Process internal links: [[Page|Display]] → Display (or Page if no Display)
    processed_text = re.sub(
        r'\[\[(.*?)(?:\|(.*?))?\]\]',
        lambda m: m.group(2) if m.group(2) else m.group(1),
        wiki_text
    )
   
    # Process external links: [URL Display] → Display (remove if no Display)
    processed_text = re.sub(
        r'\[(https?://\S+)(?:\s+([^\]]+))?\]',
        lambda m: m.group(2) if m.group(2) else '',
        processed_text
    )
   
    return processed_text


def process_talk(url):
def process_talk(url):
Line 127: Line 144:
              
              
             # Split the second part into LINK and NAME
             # Split the second part into LINK and NAME
             link_name = parts[1].strip()
             print(parts)
             if link_name[0] == "[" and link_name[-1] == "]":
            link = parts[1].strip()
                link_name = link_name[1:-1]
             name = extract_display_text(link)
                split_link_name = link_name.split(maxsplit=1)
            print("LINK " + link + ". NAME: " + name) ##################
                if len(split_link_name) < 2:
                    continue
                link, name = split_link_name[0], split_link_name[1]
            else:
                name = link_name
                link = ""
      
      
             tuning = parts[2].strip()
             tuning = parts[2].strip()
             notes = parts[3].strip()
             notes = parts[3].strip()
             pop = parts[5].strip()
             pop = parts[5].strip()
            #print("AUTHOR, NAME, LINK, TUNING") ###############################3
            #print(author, name, link, tuning)
            #print()
              
              
            # pop is collected in case the user puts in a new entry at the top
            # accidentally trashing old tuning and notes
   
             # Add to the result dictionary
             # Add to the result dictionary
             if (author, name) in result:
             if (author, name) in result:
Line 164: Line 167:
                     continue
                     continue


                '''
                 if old_pop < new_pop:
                 if old_pop < new_pop:
                     result[(author, name)] = (tuning, notes, pop, link)
                     result[(author, name)] = (tuning, notes, pop, link)
                 else:
                 else:
                     continue
                     continue
                '''
             else:
             else:
                 result[(author, name)] = (tuning, notes, pop, link)
                 result[(author, name)] = (tuning, notes, pop, link)
Line 180: Line 185:


talk = "https://en.xen.wiki/index.php?title=User_talk:Hkm/Rankings&action=edit"
talk = "https://en.xen.wiki/index.php?title=User_talk:Hkm/Rankings&action=edit"
main = "https://en.xen.wiki/index.php?title=User:Hkm/Rankings&action=edit"
main = "https://en.xen.wiki/index.php?title=List_of_xenharmonic_music_by_community_ratings&action=edit"
averages = process_talk(talk)
averages = process_talk(talk)
data, before, after = process_main(main)
data, before, after = process_main(main)
Line 193: Line 198:
# print from talk page
# print from talk page
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), ("", "", "", work))
     if link:
     clip += (f"|-\n| {author} || {link} || {tuning} || {notes} || {rating:.2f} || {pop}\n")
        clip += (f"|-\n| {author} || [{link} {work}] || {tuning} || {notes} || {rating:.2f} || {pop}\n")
    else:
        clip += (f"|-\n| {author} || {work} || {tuning} || {notes} || {rating:.2f} || {pop}\n")


# print main pages not in talk page
# print main pages not in talk page
Line 204: Line 206:
         (author, work) = i
         (author, work) = i
         tuning, notes, _, link = data.setdefault((author, work), ("", "", "", ""))
         tuning, notes, _, link = data.setdefault((author, work), ("", "", "", ""))
         if link:
         clip += (f"|-\n| {author} || {link} || {tuning} || {notes} || || \n")
            clip += (f"|-\n| {author} || [{link} {work}] || {tuning} || {notes} || || \n")
 
        else:
            clip += (f"|-\n| {author} || {work} || {tuning} || {notes} || || \n")
          
          
clip += "|}"
clip += "|}"