Skip to content

Instantly share code, notes, and snippets.

@JockeTF
Last active February 19, 2018 16:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JockeTF/73ce775ae8688739586a10d9584b32f2 to your computer and use it in GitHub Desktop.
Save JockeTF/73ce775ae8688739586a10d9584b32f2 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import sys
from csv import DictWriter
from pathlib import Path
from typing import TextIO
from jmespath import compile as jmes
from tqdm import tqdm
from fimfarchive.converters.alpha_beta import Handler
from fimfarchive.fetchers import FimfarchiveFetcher
class RowHandler(Handler):
attrs = (
'id',
'title',
'author_id',
'author_name',
'prequel',
'completion_status',
'content_rating',
'date_modified',
'date_published',
'date_updated',
'num_chapters',
'num_comments',
'num_dislikes',
'num_likes',
'num_views',
'total_num_views',
'num_words',
'rating',
'status',
'tags',
'short_description',
)
paths = {
'author_id': jmes('author.id'),
'author_name': jmes('author.name'),
}
@property
def tags(self):
tags = self.meta.get('tags')
if not tags:
return None
names = (tag['name'] for tag in tags)
return ', '.join(names)
def render(fetcher: FimfarchiveFetcher, fobj: TextIO):
writer = DictWriter(fobj, fieldnames=RowHandler.attrs, dialect='unix')
writer.writeheader()
for story in tqdm(fetcher, leave=False, ascii=True, ncols=72):
handler = RowHandler(story.meta)
writer.writerow(dict(iter(handler)))
def main():
if len(sys.argv) != 3:
exit(f"Usage: {sys.argv[0]} <input> <output>")
inpath = Path(sys.argv[1])
outpath = Path(sys.argv[2])
if not inpath.is_file():
exit(f"File '{inpath}' does not exist.")
if outpath.exists():
exit(f"Please remove '{outpath}' and rerun the command.")
with open(outpath, mode='wt', newline='') as fobj:
with FimfarchiveFetcher(str(inpath)) as fetcher:
render(fetcher, fobj)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment