User:Hkm/Rankings program: Difference between revisions
No edit summary |
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 | ||
print(parts) | |||
link = parts[1].strip() | |||
name = extract_display_text(link) | |||
print("LINK " + link + ". NAME: " + name) ################## | |||
tuning = parts[2].strip() | tuning = parts[2].strip() | ||
notes = parts[3].strip() | notes = parts[3].strip() | ||
pop = parts[5].strip() | pop = parts[5].strip() | ||
# 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= | 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)) | ||
clip += (f"|-\n| {author} || {link} || {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), ("", "", "", "")) | ||
clip += (f"|-\n| {author} || {link} || {tuning} || {notes} || || \n") | |||
clip += "|}" | clip += "|}" | ||