-
Notifications
You must be signed in to change notification settings - Fork 314
/
lso.py
85 lines (65 loc) · 2.52 KB
/
lso.py
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
# This is code adapted from KU Leuven crawler code written by
# Gunes Acar and Marc Juarez
from __future__ import absolute_import, print_function
import fnmatch
import os
import sys
import traceback
from collections import namedtuple
from miniamf import sol
def ensure_unicode(val):
"""Coerce VAL to a Unicode string by any means necessary."""
import six
if isinstance(val, six.text_type):
return val
if not isinstance(val, six.binary_type):
return six.text_type(val)
try:
return val.decode("utf-8", "backslashescape")
except (UnicodeDecodeError, TypeError):
# Backslash escaping on decode doesn't work in Python 2.
# This does approximately the same thing.
return (val.decode("latin1")
.encode("ascii", "backslashreplace")
.decode("ascii"))
# TODO: Linux only
FLASH_DIRS = ['~/.macromedia/Flash_Player/#SharedObjects/']
_BaseFlashCookie = namedtuple(
'_BaseFlashCookie',
('filename', 'domain', 'local_path', 'key', 'content'))
class FlashCookie(_BaseFlashCookie):
def __new__(self, path, key, content):
local_path = path.split("#SharedObjects/")[1]
filename = os.path.basename(path)
domain = local_path.split("/")[1]
key = ensure_unicode(key)
content = ensure_unicode(content)
return _BaseFlashCookie.__new__(
self, filename, domain, local_path, key, content)
def parse_flash_cookies(lso_file):
import six
lso_dict = sol.load(lso_file)
return [FlashCookie(lso_file, k, v) for k, v in six.iteritems(lso_dict)]
def gen_find_files(filepat, top):
"""
http://www.dabeaz.com/generators/
returns filenames that matches the given pattern under() a given dir
"""
for path, _, filelist in os.walk(top):
for name in fnmatch.filter(filelist, filepat):
yield os.path.join(path, name)
def get_flash_cookies(mod_since=0):
"""Return a list of Flash cookies (Local Shared Objects)."""
flash_cookies = list()
for top_dir in FLASH_DIRS:
top_dir = os.path.expanduser(top_dir)
for lso_file in gen_find_files("*.sol", top_dir):
mtime = os.path.getmtime(lso_file)
if mtime > mod_since:
try:
flash_cookies.extend(parse_flash_cookies(lso_file))
except Exception:
sys.stderr.write("Exception reading {!r}:\n"
.format(lso_file))
traceback.print_exc()
return flash_cookies