summaryrefslogtreecommitdiff
path: root/Caching.py
blob: a048af2954816a5ad3431b259aeaf10a680b216b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#!/usr/bin/env python3

import os
import time
THIS_DIR = os.path.dirname(os.path.abspath(__file__))
CACHE_DIR = os.path.join(THIS_DIR, 'cache')

class Caching():

    def readCache(profile, steamid, timetolife):
        filepath = os.path.join(CACHE_DIR, profile)
        filename = str(steamid)+'.tmp'
        complete = os.path.join(filepath, filename)
        try:
            fileinfo = os.stat(complete)
        except FileNotFoundError:
            return False
        if fileinfo.st_mtime < (time.time() - timetolife):
            return False

        with open(complete, 'rt') as cachefile:
            content = cachefile.read()

        return content

    def writeCache(profile, steamid, content):
        filepath = os.path.join(CACHE_DIR, profile)
        filename = str(steamid)+'.tmp'
        complete = os.path.join(filepath, filename)
        try:
            os.makedirs(filepath, exist_ok=True)
            with open(complete, 'wt') as cachefile:
                cachefile.write(content)
        except:
            print('Could not create cache file/dir.')
            return False
        return True

if __name__ == "__main__":
    # TODO(andre): Maybe run tests here?
    print('This is a module.')