-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtumblr_exporter.py
65 lines (51 loc) · 2.13 KB
/
tumblr_exporter.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from sfmutils.exporter import BaseExporter, BaseTable
from tumblr_warc_iter import TumblrWarcIter
import logging
from dateutil.parser import parse as date_parse
log = logging.getLogger(__name__)
QUEUE = "tumblr_exporter"
USER_POSTS_ROUTING_KEY = "export.start.tumblr.tumblr_blog_posts"
class TumblrStatusTable(BaseTable):
"""
Assume rows status for Tumblr posts
"""
def __init__(self, warc_paths, dedupe, item_date_start, item_date_end, seed_uids, segment_row_size=None):
BaseTable.__init__(self, warc_paths, dedupe, item_date_start, item_date_end, seed_uids, TumblrWarcIter,
segment_row_size)
def _header_row(self):
return ('created_at', 'tumblr_id', 'blog_name', 'post_type', 'post_slug',
'post_summary', 'post_text', 'tags', 'tumblr_url', 'tumblr_short_url')
def _row(self, item):
row = [date_parse(item['date']),
item['id'],
item['blog_name'],
item['type'],
item['slug'],
item['summary'],
self._item_content(item),
', '.join([tag for tag in item['tags']]),
item['post_url'],
item['short_url']
]
return row
def id_field(self):
return "tumblr_id"
@staticmethod
def _item_content(item):
"""
To extract the main text in one item since different type has different field
"""
if item['type'] in ['audio', 'video', 'photo']:
return item['caption']
elif item['type'] in ['chat', 'text']:
return item['body']
elif item['type'] == 'link':
return item['excerpt']
class TumblrExporter(BaseExporter):
def __init__(self, api_base_url, working_path, mq_config=None, warc_base_path=None):
BaseExporter.__init__(self, api_base_url, TumblrWarcIter, TumblrStatusTable, working_path,
mq_config=mq_config, warc_base_path=warc_base_path)
if __name__ == "__main__":
TumblrExporter.main(TumblrExporter, QUEUE, [USER_POSTS_ROUTING_KEY])