Commit 583e5ce8 authored by The Heavy's avatar The Heavy 🚂
Browse files

Hacked in basic support for webm videos

parent 8bfb2242
Loading
Loading
Loading
Loading
+22 −7
Original line number Diff line number Diff line
@@ -82,8 +82,12 @@ if __name__ == '__main__':
                    log.debug('img_end_tag = {0}'.format(img_end_tag))
                img_tag = document.find('img', img_end_tag + 1)
                log.debug('img_tag = {0}'.format(img_tag))
                vid_tag = document.find('video', img_end_tag + 1)
                log.debug('vid_tag = {0}'.format(vid_tag))
                if vid_tag < img_tag or img_tag < 0:
                    img_tag = vid_tag
                if img_tag < 0:
                    log.debug('No more img tags found in file.')
                    log.debug('No more img or video tags found in file.')
                    continue
                if document[img_tag - 1:img_tag] == '<':
                    img_end_tag = document.find('>', img_tag)
@@ -92,16 +96,24 @@ if __name__ == '__main__':
                    img_end_tag = document.find('&gt;', img_tag)
                    log.debug('img_end_tag = {0}'.format(img_end_tag))
                elif document[img_tag:img_tag + 5] == 'img {' or document[img_tag - 1:img_tag + 4] == ',img,' \
                        or document[img_tag - 1:img_tag + 4] == '|img|':
                    log.debug('Quietly skipping a CSS or JS reference to an img tag.')
                        or document[img_tag - 1:img_tag + 4] == '|img|' \
                        or document[img_tag:img_tag + 7] == 'video {' or document[img_tag - 1:img_tag + 6] == ',video,' \
                        or document[img_tag - 1:img_tag + 6] == '|video|':
                    log.debug('Quietly skipping a CSS or JS reference to an img or video tag.')
                    continue
                elif document[img_tag - 2:img_tag + 9] == 'i.imgur.com' \
                        or document[img_tag - 6:img_tag + 6] == 'i.postimg.cc':
                    log.debug('Quietly skipping a domain with img in it (not an img tag).')
                    continue
                elif document[img_tag - 1:img_tag + 5] == '/video':
                    log.debug('Quietly skipping closing video tag')
                    continue
                elif document[img_tag:img_tag + 6] == 'video/':
                    log.debug('Quietly skipping video already replaced')
                    continue
                else:
                    log.warning(
                        'img keyword found without &lt; or <, skipped: {0}'.format(document[img_tag - 4:img_tag + 40])
                        'img or video keyword found without &lt; or <, skipped: {0}'.format(document[img_tag - 4:img_tag + 40])
                    )
                    continue

@@ -151,7 +163,7 @@ if __name__ == '__main__':
                    log.warning('src attribute not long enough: {0}'.format(document[img_tag - 4:img_end_tag + 40]))
                    continue
                if url[:4] != 'http':
                    if url[:len(dest_folder.name)] == dest_folder.name or url[:11] == 'data:image/':
                    if url[:len(dest_folder.name)] == dest_folder.name or url[:11] in ('data:image/', 'data:video/'):
                        log.debug('Quietly ignore already replaced tags.')
                        continue
                    elif args.url is not None:
@@ -176,7 +188,7 @@ if __name__ == '__main__':

                file_type = url[url.rfind('.') + 1:]
                log.debug('file_type = {0}'.format(file_type))
                if file_type not in ('png', 'gif', 'jpg', 'jpeg'):
                if file_type not in ('png', 'gif', 'jpg', 'jpeg', 'webm'):
                    log.warning('Unrecognised file type: {0}'.format(document[img_tag - 4:img_end_tag + 40]))
                    continue

@@ -199,7 +211,10 @@ if __name__ == '__main__':
                    replacement = 'src=' + src_quote_type + dest_folder.name + '/' + dest_file.name + src_quote_type
                    log.debug('replacement = {0}'.format(replacement))
                else:
                    replacement = 'src=' + src_quote_type + 'data:image/' + file_type + ';base64,' + \
                    media_type = 'image'
                    if img_tag == vid_tag:
                        media_type = 'video'
                    replacement = 'src=' + src_quote_type + 'data:' + media_type + '/' + file_type + ';base64,' + \
                                  base64.b64encode(req.content).decode() + src_quote_type
                    log.debug('replacement[:40]...[-10:] = {0}...{1}'.format(replacement[:40], replacement[-10:]))
                document = document.replace(orig, replacement)