File: list_expired_cache

package info (click to toggle)
mpd-sima 0.18.2-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 688 kB
  • sloc: python: 4,364; sh: 222; makefile: 166
file content (95 lines) | stat: -rwxr-xr-x 2,878 bytes parent folder | download | duplicates (3)
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
DESCRIPTION:
    List expired entries of sima's internal http cache.

    Use SIMA_VARDIR env. var to override default location.

    SIMA_VARDIR="/var/lib/mpd-sima/http/LastFM/" ./list_expired_cache

    Default location: $XDG_DATA_HOME/http/LastFM/
    $XDG_DATA_HOME usually expands to $HOME/.local/share/

HELP:
    Run  script with "-h" CLI option for info on defaults.
"""

import email.utils
import calendar
import time
import sys

from hashlib import md5
from os import environ
from os.path import join as pjoin

import sima.lib.cache

def parse_cache_control(headers):
    """
    Parse the cache control headers returning a dictionary with values
    for the different directives.
    """
    retval = {}
    # requests provides a CaseInsensitiveDict as headers
    cc_header = 'cache-control'
    if cc_header in headers:
        parts = headers[cc_header].split(',')
        parts_with_args = [
            tuple([x.strip().lower() for x in part.split("=", 1)])
            for part in parts if -1 != part.find("=")]
        parts_wo_args = [(name.strip().lower(), 1)
                         for name in parts if -1 == name.find("=")]
        retval = dict(parts_with_args + parts_wo_args)
    return retval

def encode(val):
    """encode url to hash table key"""
    return md5(val.encode('utf-8')).hexdigest()


homedir = environ.get('HOME')
xdh = environ.get('XDG_DATA_HOME', pjoin(homedir, '.local/share/'))
default_cache = pjoin(xdh, 'mpd_sima/http/LastFM')
cache = environ.get('SIMA_VARDIR', default_cache)

if len(sys.argv) > 1:
    local_var = 'DEFAULTS\n\tXDG_DATA_HOME={0}\n\n\tWill use: {1}\n'.format(xdh, cache)
    print(__doc__.split('HELP')[0]+local_var)
    sys.exit(0)

# use SIMA_VARDIR to override default XDG_DATA_HOME
cache_obj = sima.lib.cache.FileCache(environ.get('SIMA_VARDIR', cache))

for elem in cache_obj:
    now = time.time()
    date = calendar.timegm(email.utils.parsedate_tz(elem.headers.get('Date')))
    current_age = max(0, now - date)
    cc = parse_cache_control(elem.headers)
    # determine freshness
    freshness_lifetime = 0
    if 'max-age' in cc and cc['max-age'].isdigit():
        freshness_lifetime = int(cc['max-age'])
    elif 'expires' in elem.headers:
        expires = email.utils.parsedate_tz(elem.headers['expires'])
        if expires is not None:
            expire_time = calendar.timegm(expires) - date
            freshness_lifetime = max(0, expire_time)

    # see how fresh we actually are
    fresh = (freshness_lifetime > current_age)

    if fresh:
        continue
    else:
        #print(elem.url)
        pass
    # we're not fresh. If we don't have an Etag, clear it out
    if 'etag' not in elem.headers:
        #print('no etag and expired')
        print(pjoin(cache, encode(elem.url)))


# VIM MODLINE
# vim: ai ts=4 sw=4 sts=4 expandtab