aboutsummaryrefslogtreecommitdiffstats
path: root/titlebot/commands/poll.py
blob: 595ab269be72e1ac4f4b00e9a9fc71eac7a06f8a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#!/usr/bin/env python
import json
def load_db(f):
    try:
        with open(f) as fl:
            return json.load(fl)
    except:
        #default db is []
        return []

def title_in_db(t,d):
    for index,entry in enumerate(d):
        if t == entry['title']:
            print("Title is already in list.")
            print("To vote for this type '.up %d'" %index)
            return True
    return False
def save_db(f,db):
    with open(f,"w") as x:
        json.dump(db,x)

def sort_by_votes(db):
    return sorted(db,key=lambda entry:sum(entry['votes'].values()),reverse=True)