Skip to content

Instantly share code, notes, and snippets.

@gfreezy
Created September 19, 2014 07:22
Show Gist options
  • Save gfreezy/f3420977fce17da940f5 to your computer and use it in GitHub Desktop.
Save gfreezy/f3420977fce17da940f5 to your computer and use it in GitHub Desktop.
自动使用爱加密加密apk
#!/usr/bin/env python
import os
import requests
import time
import sys
from collections import namedtuple
LOGIN_PATH = 'http://www.ijiami.cn/toLogin'
UPLOAD_PATH = 'http://www.ijiami.cn/upload/'
APPLY_FOR_ENCRYPTION_PATH = 'http://www.ijiami.cn/uploadApp'
LIST_APP_PATH = 'http://www.ijiami.cn/apply/encryptLogsByList'
MAIL = ''
PASSWORD = ''
AppStatus = namedtuple('AppStatus', ('name', 'filename', 'size', 'apk_url', 'icon_url', 'version', 'md5', 'package'))
class Encryption(object):
def __init__(self, filename, alias_name):
if not os.path.exists(filename):
raise Exception('filename not exists')
self.filepath = filename
self.alias_name = alias_name
self.cookies = {}
self.app_status = None
self.requests_fired = False
self.session = requests.session()
def get(self, *args, **kwargs):
r = self.session.get(*args, **kwargs)
r.raise_for_status()
return r
def post(self, *args, **kwargs):
r = self.session.post(*args, **kwargs)
r.raise_for_status()
return r
def log(self, msg):
sys.stderr.write('%s\n' % msg)
def _login(self):
self.post(LOGIN_PATH, data={'email': MAIL, 'pwd': PASSWORD})
self.log('Login succeeded')
def _upload(self):
files = {'upload': open(self.filepath)}
filename = os.path.basename(self.filepath)
self.log('Start uploading apk file')
r = self.post(UPLOAD_PATH, files=files)
segs = r.text.split('|')
name, size, apk_url, icon_url, version, md5, package, _, _, _, _ = segs
self.app_status = AppStatus(name, filename, size, apk_url, icon_url, version, md5, package)
self.log('Upload apk file succeeded')
def _apply_for_encryption(self):
now = int(time.time() * 1000)
self.get(APPLY_FOR_ENCRYPTION_PATH, params={
'check': '',
'values': '',
'appval': self.app_status.filename,
'app_type': 0,
'cxmc': self.app_status.name,
'bbh': self.app_status.version,
'bm': self.alias_name or os.path.splitext(self.app_status.filename)[0],
'apkUrl': self.app_status.apk_url,
'iconUrl': self.app_status.icon_url,
'version': 'undefined',
'md5': self.app_status.md5,
'package': self.app_status.package,
'msg_content': '',
'error': '',
'size': self.app_status.size,
'signUrl': '',
'_': now,
})
self.log('Apply for new encryption succeeded')
def _list_apps_on_page(self, page=1, page_size=10):
r = self.post(LIST_APP_PATH, data={
'pageNum': page,
'psize': page_size,
'selStr': ''
})
apps = r.json()['result']['plist']
return apps
def _find_download_url(self):
page = 1
while True:
apps = self._list_apps_on_page(page, page_size=30)
if not apps:
# not found
return None
url, found = self._find_url_in_page(apps)
if not found:
page += 1
continue
if url:
return url
self.log('Under encryption, sleep 3 seconds')
time.sleep(3)
def _find_url_in_page(self, apps):
for app in apps:
if app['nvcAppAlias'] != self.alias_name:
continue
return app['nvcDownloadURL'], True
return None, False
def _fire_requests(self):
self._login()
self._upload()
self._apply_for_encryption()
self.requests_fired = True
def get_download_url(self):
if not self.requests_fired:
self._fire_requests()
return self._find_download_url()
def main():
if len(sys.argv) != 3:
sys.stderr.write('''Usage:\n\t%s path_to_apk alias_name''' % sys.argv[0])
sys.exit(-1)
filename = sys.argv[1]
alias_name = sys.argv[2]
e = Encryption(filename, alias_name)
print e.get_download_url()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment