Completed
Push0864f5...15cfe6
passed — Build
created

parse_original_name_from_media()   A

↳ Parent: Project

Complexity

Conditions 3

Duplication

Lines 0
Ratio 0 %

Size

Total Lines 18

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 3
c 1
b 0
f 1
dl 0
loc 18
rs 9.4285
1
from __future__ import print_function
2
3
import os
4
import re
5
import sys
6
7
from elodie import constants
8
from elodie import geolocation
9
from elodie import log
10
from elodie.compatability import _decode
11
from elodie.filesystem import FileSystem
12
from elodie.localstorage import Db
13
from elodie.media.base import Base, get_all_subclasses
14
from elodie.media.media import Media
15
from elodie.media.text import Text
16
from elodie.media.audio import Audio
17
from elodie.media.photo import Photo
18
from elodie.media.video import Video
19
from elodie.result import Result
20
21
def main(argv):
22
    filesystem = FileSystem()
23
    result = Result()
24
    subclasses = get_all_subclasses()
25
26
    paths = argv[1:]
27
28
    for path in paths:
29
        path = os.path.expanduser(path)
30
        if os.path.isdir(path):
31
            for source in filesystem.get_all_files(path, None):
32
                status = add_original_name(source, subclasses)
33
                result.append((source, status))
34
                
35
        else:
36
            status = add_original_name(source, subclasses)
37
            result.append((source, status))
38
39
    result.write()
40
41
def add_original_name(source, subclasses):
42
    media = Media.get_class_by_file(source, subclasses)
43
    metadata = media.get_metadata()
44
    if metadata['original_name'] is not None:
45
        print('{} already has OriginalFileName...Skipping'.format(source))
46
        return
47
    
48
    original_name = parse_original_name_from_media(metadata)
49
    return media.set_original_name(original_name)
50
    
51
    
52
def parse_original_name_from_media(metadata):
53
    # 2015-07-23_04-31-12-img_9414-test3.jpg
54
    base_name = metadata['base_name']
55
    title = metadata['title']
56
    extension = metadata['extension']
57
    date_regex = r'^\d{4}-\d{2}-\d{2}_\d{2}-\d{2}-\d{2}-'
58
    if not re.match(date_regex, base_name):
59
        print("File name did not match date pattern...Skipping")
60
        return
61
62
    trimmed_base_name = re.sub(date_regex, '', base_name)
63
    if title:
64
        trimmed_base_name = trimmed_base_name.replace(
65
            '-{}'.format(title), 
66
            ''
67
        )
68
69
    return '{}.{}'.format(trimmed_base_name, extension)
70
71
if __name__ == "__main__":
72
    main(sys.argv)
73