|
| 1 | +import datetime |
| 2 | +from optparse import make_option |
| 3 | +from django.conf import settings |
| 4 | +from django.core.management.base import BaseCommand, CommandError |
| 5 | +from django.contrib.auth.models import User |
| 6 | +from django.template.defaultfilters import slugify |
| 7 | +from telemeta.models import * |
| 8 | +from telemeta.util.unaccent import unaccent |
| 9 | +from teleforma.models import * |
| 10 | +import logging |
| 11 | +import os |
| 12 | +#import timeside |
| 13 | + |
| 14 | + |
| 15 | +class Logger: |
| 16 | + """A logging object""" |
| 17 | + |
| 18 | + def __init__(self, file): |
| 19 | + self.logger = logging.getLogger('myapp') |
| 20 | + self.hdlr = logging.FileHandler(file) |
| 21 | + self.formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s') |
| 22 | + self.hdlr.setFormatter(self.formatter) |
| 23 | + self.logger.addHandler(self.hdlr) |
| 24 | + self.logger.setLevel(logging.INFO) |
| 25 | + |
| 26 | + |
| 27 | +class Command(BaseCommand): |
| 28 | + help = "Import conferences from the MEDIA_ROOT directory, create conferences if don't exist" |
| 29 | + |
| 30 | + args = 'organization log_file' |
| 31 | + spacer = '_-_' |
| 32 | + original_format = ['webm', 'mp4'] |
| 33 | + transcoded_formats = ['mp4', 'ogg', 'mp3'] |
| 34 | + image_formats = ['png', 'jpg'] |
| 35 | + |
| 36 | + def cleanup(self): |
| 37 | + medias = Media.objects.all() |
| 38 | + for media in medias: |
| 39 | + media.delete() |
| 40 | + items = MediaItem.objects.all() |
| 41 | + for item in items: |
| 42 | + item.delete() |
| 43 | + |
| 44 | + def get_duration(self, file): |
| 45 | + #TODO: avoid timeside |
| 46 | + #decoder = timeside.decoder.FileDecoder(file) |
| 47 | + #value = str(period_nametime.timedelta(0,decoder.input_duration)) |
| 48 | + #t = value.split(':') |
| 49 | + #t[2] = t[2].split('.')[0] |
| 50 | + #return ':'.join(t) |
| 51 | + return |
| 52 | + |
| 53 | + |
| 54 | + #decoder.setup() |
| 55 | + # time.sleep(0.5) |
| 56 | + def handle(self, *args, **options): |
| 57 | + organization_name = args[0] |
| 58 | + department_name = args[1] |
| 59 | + period_name = args[2] |
| 60 | + log_file = args[3] |
| 61 | + logger = Logger(log_file) |
| 62 | + |
| 63 | + organization = Organization.objects.get(name=organization_name) |
| 64 | + department = Department.objects.get(name=department_name, |
| 65 | + organization=organization) |
| 66 | + self.media_dir = settings.MEDIA_ROOT + organization.name + os.sep + department_name |
| 67 | + file_list = [] |
| 68 | + all_conferences = Conference.objects.all() |
| 69 | + i = 1 |
| 70 | + |
| 71 | +# self.cleanup() |
| 72 | + |
| 73 | + for root, dirs, files in os.walk(self.media_dir): |
| 74 | + for filename in files: |
| 75 | + name = os.path.splitext(filename)[0] |
| 76 | + ext = os.path.splitext(filename)[1][1:] |
| 77 | + |
| 78 | + if ext and (ext in self.original_format or ext in self.transcoded_formats) and name[0] != '.': |
| 79 | + root_list = root.split(os.sep) |
| 80 | + public_id = root_list[-1] |
| 81 | + course = root_list[-2] |
| 82 | + course_code = course.split(self.spacer)[0] |
| 83 | + course_type_name = course.split(self.spacer)[1].lower() |
| 84 | + year = root_list[-3] |
| 85 | + department_name = root_list[-4] |
| 86 | + organization_name = root_list[-5] |
| 87 | + abs_path = root + os.sep + filename |
| 88 | + dir = os.sep.join(root_list[-5:]) |
| 89 | + path = dir + os.sep + filename |
| 90 | + collection_id = '_'.join([department_name, course_code, course_type_name]) |
| 91 | + |
| 92 | + print(public_id) |
| 93 | + courses = Course.objects.filter(code=course_code) |
| 94 | + course_type_obj = CourseType.objects.get(name=course_type_name) |
| 95 | + period_obj = Period.objects.get(name=period_name) |
| 96 | + mtime = os.path.getmtime(abs_path) |
| 97 | + conf_datetime = datetime.datetime.fromtimestamp(mtime) |
| 98 | + |
| 99 | + if department and courses: |
| 100 | + course_obj = courses[0] |
| 101 | + conferences = Conference.objects.filter(public_id=public_id) |
| 102 | + if conferences: |
| 103 | + conference = conferences[0] |
| 104 | + else: |
| 105 | + conference = Conference(public_id=public_id) |
| 106 | + conference.course = course_obj |
| 107 | + conference.course_type = course_type_obj |
| 108 | + conference.period = period_obj |
| 109 | + conference.date_begin = conf_datetime |
| 110 | + conference.save() |
| 111 | + |
| 112 | + department = Department.objects.get(name=department_name, |
| 113 | + organization=organization) |
| 114 | + exist = False |
| 115 | + medias = conference.media.all() |
| 116 | + for media in medias: |
| 117 | + if media.item.file == path: |
| 118 | + exist = True |
| 119 | + break |
| 120 | + |
| 121 | + streaming = False |
| 122 | + try: |
| 123 | + stations = conference.station.filter(started=True, public_id=public_id) |
| 124 | + if stations: |
| 125 | + streaming = True |
| 126 | + except: |
| 127 | + pass |
| 128 | + |
| 129 | + if not exist and not streaming: |
| 130 | + collections = MediaCollection.objects.filter(code=collection_id) |
| 131 | + if not collections: |
| 132 | + collection = MediaCollection(code=collection_id,title=collection_id) |
| 133 | + collection.save() |
| 134 | + else: |
| 135 | + collection = collections[0] |
| 136 | + |
| 137 | + id = '_'.join([collection_id, public_id, ext, str(i)]) |
| 138 | + |
| 139 | + items = MediaItem.objects.filter(collection=collection, code=id) |
| 140 | + if not items: |
| 141 | + item = MediaItem(collection=collection, code=id) |
| 142 | + item.save() |
| 143 | + else: |
| 144 | + item = items[0] |
| 145 | + |
| 146 | + item.title = name |
| 147 | + item.file = path |
| 148 | + #item.approx_duration = self.get_duration(root+os.sep+filename) |
| 149 | + item.save() |
| 150 | + |
| 151 | + files = os.listdir(root) |
| 152 | + for file in files: |
| 153 | + filename, extension = os.path.splitext(file) |
| 154 | + if extension[1:] in self.image_formats: |
| 155 | + related = MediaItemRelated(item=item) |
| 156 | + related.file = dir + os.sep + file |
| 157 | + related.title = 'preview' |
| 158 | + related.set_mime_type() |
| 159 | + related.save() |
| 160 | + break |
| 161 | + |
| 162 | + media = Media(conference=conference) |
| 163 | + media.item = item |
| 164 | + media.course = conference.course |
| 165 | + media.period = conference.period |
| 166 | + media.course_type = conference.course_type |
| 167 | + media.type = ext |
| 168 | + media.set_mime_type() |
| 169 | + media.save() |
| 170 | + conference.save() |
| 171 | + logger.logger.info(path) |
| 172 | + i += 1 |
| 173 | + |
0 commit comments