title: Is it possible to parse an .apk file in JavaScript?
body: <p>We have Python code that parses .apk files. We want to do it in JavaScript and then upload the file directly to S3. Is it possible? And if yes, how do we do it? We can use existing plugins if necessary.</p>
  
  <p>Here is the Python code that parses the files:</p>
  
  <pre><code>import os
  import urllib2
  import StringIO
  import re
  
  from django.conf import settings
  from django.core.files.uploadedfile import InMemoryUploadedFile
  
  from androguard.core.bytecodes.apk import APK
  from storages.backends.s3boto import S3BotoStorage
  
  from parser import Parser
  
  
  class APKParser(Parser):
  
      def __init__(self, filepath):
          if filepath.startswith("http://") or filepath.startswith("https://"):
              conn = urllib2.urlopen(filepath)
              self.apk = APK(conn.read(), raw=True)
              conn.close()
          else:
              self.apk = APK(filepath)
          self.apk_xml = self.apk.get_AndroidManifest()
  
      def get_version(self):
          manifest = self.apk_xml.getElementsByTagName("manifest")[0]
          return manifest.getAttribute("android:versionName")
  
      def get_name(self):
          app = self.apk_xml.getElementsByTagName("application")[0]
          name = app.getAttribute("android:label")
          if name.startswith("@"):
              package_parser = self.apk.get_android_resources()
              name = ''
              for package_name in package_parser.get_packages_names():
                  name = package_parser.get_string(package_name, 'app_name')
                  if name:
                      name = name[1]
                      break
          return name
  
      def get_package_name(self):
          return self.apk.package
  
      def get_image(self, content_item):
          storage = S3BotoStorage(bucket=settings.AWS_MANAGED_CONTENT_SETS_STORAGE_BUCKET_NAME,
                                  secure_urls=settings.SECURE_IMAGES_URL)
          filepath = ""
          for apk_file in self.apk.get_files():
              if re.match("(res/drawable.*/(icon|logo).*)", apk_file):
                  filepath = apk_file
                  break
          if filepath:
              try:
                  fileext = os.path.splitext(filepath)[1][1:]
                  file_io = StringIO.StringIO()
                  file_io.write(self.apk.get_file(filepath))
                  icon_filename = "%s.%s" % (self.get_name(), fileext)
                  inmemory_file = InMemoryUploadedFile(file_io, None, icon_filename, 'image/%s' % fileext,
                                                       file_io.len, None)
                  inmemory_file.seek(0)
                  upl_file = uploaded_filename(content_item, inmemory_file.name)
                  storage._save(upl_file, inmemory_file)
                  file_io.close()
                  return dict(url=storage.url(upl_file), path=upl_file)
              except:
                  pass
          return dict()
  </code></pre>