-
Notifications
You must be signed in to change notification settings - Fork 23
/
redditfs.py
196 lines (155 loc) · 4.99 KB
/
redditfs.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import fuse
import errno
import stat
import time
import sys
import requests
import os
import os.path
from fsfile import *
try:
import urlparse
except ImportError:
import urllib.parse as urlparse
CACHE_TIMEOUT = 60 * 60
class RedditFS(fuse.Operations):
PERMS = stat.S_IRUSR | stat.S_IRGRP | stat.S_IROTH
DIR_PERMS = stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH
def __init__(self):
self.fd = 0
self.fs = FSDirectory(
'/',
DirectoryType.root,
RedditFS.PERMS | RedditFS.DIR_PERMS,
time.time(),
)
@property
def dirlist(self):
if not self._dirlist:
self._dirlist = self._populate_dirlist()
return self._dirlist
def open(self, path, flags):
self.fd += 1
return self.fd
def getattr(self, path, fh=None):
f = self.traverse(path)
if f is None:
raise fuse.FuseOSError(errno.ENOENT)
return f.getattr()
def read(self, path, size, offset, fh):
f = self.traverse(path)
if f is None:
raise fuse.FuseOSError(errno.ENOENT)
if f.dir():
raise fuse.FuseOSError(errno.EISDIR)
return f.read(size, offset)
def readdir(self, path, fh):
f = self.traverse(path)
if f is None:
raise fuse.FuseOSError(errno.ENOENT)
if not f.dir():
raise fuse.FuseOSError(errno.ENOTDIR)
return f.readdir()
def traverse(self, path):
path = self._split_path(path)
node = self.fs
return self._traverse(path, node)
def _traverse(self, path, node):
if len(path) == 0:
return node
fn = path.pop(0)
next_node = node.get_child(fn)
if node.dirtype == DirectoryType.root:
self._lazy_load_subreddit(next_node, fn)
return self._traverse(path, next_node)
def _split_path(self, path):
# TODO Move to a util module ?
head, tail = os.path.split(path)
if tail == '':
return []
if head == '' or head == os.sep:
return [tail]
return self._split_path(head) + [tail]
def _lazy_load_subreddit(self, node, filename):
# If the directory does not exist, attempt to load it
if node is None:
return self._populate_subreddit(filename)
# If the directory exists but was created more than CACHE_TIMEOUT
# seconds ago, re-populate the directory
if node.getattr().get('st_ctime') < (time.time() - CACHE_TIMEOUT):
self.fs.remove_child(node.filename)
return self._populate_subreddit(node.filename)
# The directory exists and is fresh, return it
return node
def _populate_subreddit(self, subreddit):
r = requests.get(
'http://api.reddit.com/r/{}/hot'.format(subreddit),
headers={
'User-Agent': 'redditfs /u/evilyomiel'
},
allow_redirects=False,
)
if r.status_code in [404, 302]:
return
r.raise_for_status()
links = [link['data'] for link in r.json()['data']['children']]
root_file = FSDirectory(
filename=subreddit,
dirtype=DirectoryType.subreddit,
mode=RedditFS.PERMS | RedditFS.PERMS,
ctime=time.time(),
)
for zelda in links:
self._add_reddit_link_to_fs(root_file, zelda)
self.fs.add_child(root_file)
return root_file
def _add_reddit_link_to_fs(self, fs, zelda):
title = zelda['title']
filename = self._sanitize_path(title)
permalink = urlparse.urljoin(
'http://www.reddit.com/',
zelda['permalink']
)
url = zelda['url']
selftext = zelda['selftext']
root_file = FSDirectory(
filename=filename,
dirtype=DirectoryType.normal,
mode=RedditFS.PERMS | RedditFS.DIR_PERMS,
ctime=zelda['created_utc'],
)
permalink_file = FSFile(
filename='permalink',
mode=RedditFS.PERMS,
content=permalink,
ctime=zelda['created_utc'],
)
url_file = FSFile(
filename='url',
mode=RedditFS.PERMS,
content=url,
ctime=zelda['created_utc'],
)
selftext_file = FSFile(
filename='selftext',
mode=RedditFS.PERMS,
content=selftext,
ctime=zelda['created_utc'],
)
for f in (permalink_file, url_file, selftext_file):
root_file.add_child(f)
fs.add_child(root_file)
def _sanitize_path(self, path):
replace = (
('/', ''),
(' ', '_'),
("'", ''),
('"', ''),
)
for r in replace:
path = path.replace(*r)
return path.lower()
def main():
fuse.FUSE(RedditFS(), sys.argv[1], foreground=True, nothreads=True)
if __name__ == '__main__':
main()