Python that displays voting results and post counts for release announcements

Script present (no issue)

for test, we can run online

#!/usr/bin/python
"""
 default: sort by date
 -t select branch testing (default stable)
 TODO
 -c sort by posts count
 -v sort by voters count
 -p sort by votes % ok no issue
"""

import sys
import urllib.request
import json
import concurrent.futures
import time

def get_subject(topic):
    with urllib.request.urlopen(f"https://forum.manjaro.org/t/{topic['id']}.json") as f_url:
        req = f_url.read()
    post = json.loads(req)['post_stream']['posts'][0]
    topic['voters'] = post['polls'][0]['voters']
    topic['poll_ok'] = post['polls'][0]['options'][0]['votes']
    pourcentage = round((post['polls'][0]['options'][0]['votes'] / post['polls'][0]['voters']) * 100)
    topic['poll_pourcent'] = pourcentage
    topic['poll_thanks'] = post['polls'][0]['options'][1]['votes']
    topic['poll_opps'] = post['polls'][0]['options'][2]['votes']


branch = 'stable'
if '-t' in sys.argv:
    branch = 'testing'
with urllib.request.urlopen(f"https://forum.manjaro.org/c/announcements/{branch}-updates.json") as f_url:    
    req = f_url.read()

#doc: https://docs.discourse.org/#tag/Categories    
topics = json.loads(req)['topic_list']['topics']
topics = [t for t in topics if not t['title'].startswith('About')]


start_time = time.time()

#download all topics:
with concurrent.futures.ThreadPoolExecutor(max_workers=12) as executor:
    executor.map(get_subject, topics)

end_time = time.time() - start_time
print(f"Downloaded {len(topics)} subjects in {end_time} seconds", end="\n\n")
# classic   :  15.45 seconds
# 5  Thread :   3.54 seconds
# 12 Thread :   2.27 seconds

# display 30 results
for topic in topics:
    print(topic['title'])
    print('  posts count', f"\33[33m{topic['posts_count']}\33[0m")
    try:
        print('  % ok', f"\33[32m{topic['poll_pourcent']}\33[0m", end=' > ')
        print("\33[32m", end='')
        for i in range(1,topic['poll_pourcent']):
            print("#", end='')
        print("\33[31m", end='')
        for i in range(topic['poll_pourcent']+1, 101):
            print("#", end='')
        print("\33[0m")
        print('  voters count', topic['voters'])
    except KeyError:
        pass # no poll ?
6 Likes

Moved from #technical-issues-and-assistance:programming to #showcase as it's showing off work not a request for help. Renamed topic title for clarity.

3 Likes

That is pretty cool. What are you using it for @papajoke?

Here is a snippet of what it outputs:

1 Like