Commits

Alexandre Macabies committed afab55e

Adding a Word format for transcripts

Comments (0)

Files changed (2)

     thumb_ratio: 0.751
     ask_directory: false
 
-    download_dir: ./downloads
-    transcript_use_odf: true
+    download_dir: "./downloads"
+    transcript_format: "odf" # odf, word or plain; if word, you need
+                             # a converter: http://odf-converter.sourceforge.net/download.html#command-line
+
+    odf_to_word_path: "C:/please/edit/me.exe" # path for the converter binary, if using 'word' format
 
 # ODF transcript styles
 styles:
 import odf.style
 import os
 import re
+import subprocess
+import tempfile
 import yaml
 
 BASE_URL = 'http://www.ted.com'
         self.rawdata.write(reply.readAll())
 
     def finished(self):
+        """
+        
+        """
         data = self.rawdata.data().data().decode('utf-8')
         offset = self.kwargs.get('offset', 0)
         stringList = tuple(toList(data, offset=offset))
         f.close()
 
         if self.kwargs.get('withTranscript', False):
-            if CONFIG.get('transcript_use_odf'):
+            format = CONFIG.get('transcript_format', 'odf')
+            assert format in ('odf', 'word', 'plain'), "transcript_format must be one of 'word', 'word' or 'plain'"
+            if format in ('odf', 'word'):
                 lines = toParagraphs(stringList)
                 textdoc = OpenDocumentText()
                 for item in self.styles.itervalues():
                 for line in lines:
                     p = P(text=line, stylename=self.styles['p'])
                     textdoc.text.addElement(p)
-                textdoc.save(unicode(self.filename) + '.doc')
-            else:
+                fname = unicode(self.filename)
+
+                if format == 'word':
+                    tmp = tempfile.TemporaryFile(suffix='.odt')
+                    textdoc.save(tmp)
+                    # TODO: make this non-blocking
+                    p = subprocess.call([CONFIG.get('odf_to_word_path'), '/I', tmp.name, '/O', fname + '.doc'])
+                    tmp.close()
+                    if not 'Done.' in p:
+                        QtGui.QMessageBox.warning(self, u"Error", u"Unable to convert transcript to Word format.\n"
+                            u"Please check your odf_to_word_path config line.")
+                else:
+                    textdoc.save(fname + '.odt')
+                    
+            else: # plain
                 wrapped = toPlainText(stringList)
                 f = QtCore.QFile(unicode(self.filename) + '.txt')
                 f.open(QtCore.QIODevice.WriteOnly)
                 return
             directory = QtCore.QDir(directory)
         else:
-            directory = QtCore.QDir(CONFIG.get('download_dir'))
+            # download_dir may still be absolute!
+            dir = os.path.normpath(os.path.join(os.path.dirname(__file__), CONFIG.get('download_dir')))
+            directory = QtCore.QDir(dir)
             newdir = self.slugify_spaced(self.talkName)
             a = directory.mkdir(newdir)
             a = directory.cd(newdir)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.