Server Records/top users.py

See txt: ServerStats.txt

```
# top\_users.py
#
# Change log: None

from pathlib import Path

def most\_messages(p: Path):
 '''
 {User: [Number of Messages, Date]}
 '''
    the\_file = None

    try:
        the\_file = p.open('r', encoding = 'utf-8')

        user\_dict = dict()

                date = line[:-1]
                continue
            if line.startswith('Top 5 Users') or line.startswith('Messages | Top 5 - Members'):
                if line.startswith('Messages | Top 5 - Members'):
                    next(the\_file)
                    next(the\_file)
                for \_ in range(5):
                    x = next(the\_file)[:-1]
                    user = x[x.find('@'):x.find(':')]
                    mess = int(x[x.find(':')+2:x.find('messages')-1].replace(',',''))
                    if len(user\_dict) == 10:
                        least = sorted(user\_dict.items(), key=lambda x: -x[1][0])[-1]
                        if mess > least[1][0]:
                            del user\_dict[least[0]]
                        else:
                            continue
                    if user not in user\_dict:
                        user\_dict[user] = [mess, date]
                            user\_dict[user] = [mess, date]
            else:
                continue

        return user\_dict

            the\_file.close()

def total\_time(t: tuple):
    return t[0]\*60 + t[1]

def most\_voice(p: Path):
 '''
 {User: [(Hours, Minutes), Date]}
 '''

    the\_file = None

    try:
        the\_file = p.open('r', encoding = 'utf-8')

        user\_dict = dict()

                date = line[:-1]
                continue
            if line.startswith('Voice | Top 5 - Members'):
                next(the\_file)
                next(the\_file)
                for \_ in range(5):
                    x = next(the\_file)[:-1]

                    h = m = 0

                    user = x[x.find('@'):x.find(':')]
                    if 'hours' in x:
                        h = int(x[x.find(':')+2:x.find('hours')-1])
                    if 'minutes' in x:
                        m = int(x[x.find(',')+2:x.find('minutes')-1])

                    if len(user\_dict) == 10:
                        least = sorted(user\_dict.items(), key=lambda x: -total\_time(x[1][0]))[-1]
                        if total\_time((h,m)) > total\_time(least[1][0]):
                            del user\_dict[least[0]]
                        else:
                            continue
                    if user not in user\_dict:
                        user\_dict[user] = [(h,m), date]
                    else:
                        if total\_time((h,m)) > total\_time(user\_dict.get(user)[0]):
                            user\_dict[user] = [(h,m), date]
            else:
                continue

        return user\_dict

            the\_file.close()

if \_\_name\_\_ == '\_\_main\_\_':
    m = most\_messages(Path('ServerStats.txt'))
    print('== Top Messages Within 14 Days ==')
    for user, mess in sorted(m.items(), key=lambda x: -x[1][0]):
        print(mess[1], user + ':', mess[0])

    print()

    v = most\_voice(Path('ServerStats.txt'))
    print('== Top Voice Time Within 14 Days ==')
    for user, voice in sorted(v.items(), key=lambda x: -total\_time(x[1][0])):
        print(voice[1], user + ':', voice[0][0], 'hours,', voice[0][1], 'minutes')

```