Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- # -*- coding: utf-8 -*-
- """
- Created on Sun Feb 25 21:06:26 2018
- @author: MacBook
- """
- import requests
- import os
- import sys
- api_dev_key = 'd679fba2731eb2b81f080788e4ad0f6e'
- username = 'skyinstaller'
- password = 'skyinstaller'
- # PARAMS
- def paste(file_name,content,session=''):
- global api_dev_key
- paste_name = "#[PYLIB]#" + file_name
- # PARAMS
- data = str(content)
- url = 'https://pastebin.com/api/api_post.php'
- pastebin_vars = {'api_dev_key': api_dev_key,'api_paste_code': data,'api_user_key': session,'api_paste_private':'0',
- 'api_option' : 'paste','api_paste_name': paste_name}
- r = requests.post(url,pastebin_vars)
- return r.text
- def login():
- login_url = 'https://pastebin.com/api/api_login.php'
- api_user_name = username
- api_user_password = password
- r = requests.post(login_url,{'api_dev_key':api_dev_key,'api_user_name':api_user_name,'api_user_password':api_user_password})
- if r.ok:
- return r.text
- else:
- return None
- def get_list(session):
- import xml.etree.ElementTree as ET
- global api_dev_key
- api_results_limit = 100;
- list_url = 'https://pastebin.com/api/api_post.php';
- r = requests.post(list_url,{'api_dev_key':api_dev_key, 'api_user_key':session, 'api_results_limit':api_results_limit, 'api_option': 'list'})
- paste_list = r.text
- #parse tree
- t = "<root>" + paste_list + "</root>"
- root = ET.fromstring(t)
- content_list = []
- for paste in root:
- one_post = {}
- for element in paste:
- if element.tag.strip() == 'paste_key':
- one_post['key'] = str(element.text).strip()
- elif element.tag.strip() == 'paste_title':
- one_post['title'] = str(element.text).strip()
- elif element.tag.strip() == 'paste_size':
- one_post['size'] = str(element.text).strip()
- content_list.append(one_post)
- return content_list
- def paste_by_user(title,content,session=''):
- if content is None:
- content = ''
- #print 'paste_by_user', title,content,session
- paste_url = paste(file_name=title,content=content,session=session)
- if paste_url.strip().startswith('http'):
- return paste_url
- return None
- def get_content_from_key(key):
- r = requests.get('https://pastebin.com/raw/' + str(key).strip())
- return r.text
- def get_content_from_url(url):
- r = requests.get(url)
- return r.text
- def generate_project_structure(folderpath,suffix=['.py','.txt']):
- root_file = {}
- fname_content = {}
- if folderpath.strip() == '.':
- folderpath = os.path.abspath('.')
- basefolder = os.path.basename(folderpath)
- print "Project Structure"
- print '#' * 10
- for root, dirs, files in os.walk(folderpath):
- filtered_files = []
- for _file in files:
- filename, file_extension = os.path.splitext(_file)
- if file_extension.lower() in suffix:
- filtered_files.append(_file)
- path = os.path.join(root,_file)
- with open(path,'r') as f:
- fname_content[_file] = f.read()
- root_file[root.replace(folderpath,basefolder)] = filtered_files
- level = root.replace(folderpath, '').count(os.sep)
- indent = ' ' * 4 * (level)
- print('{}{}/'.format(indent, os.path.basename(root)))
- subindent = ' ' * 4 * (level + 1)
- for f in files:
- sign = '[x]'
- for fmt in suffix:
- if f.lower().endswith(fmt):
- sign = '[o]'
- break
- print('{}{}{}'.format(subindent, f, sign))
- return root_file, fname_content
- def jsonify_content(root_file, fname_content):
- import json
- ret = {}
- ret['rf'] = root_file
- ret['fc'] = fname_content
- output = json.dumps(ret)
- return output
- def push(folderpath,suffix=['.py','.txt']):
- rf,fc = generate_project_structure(folderpath,suffix)
- output = jsonify_content(rf,fc)
- basefolder = os.path.basename(folderpath)
- url = paste_by_user(basefolder,output)
- if url is not None:
- print 'created at:', url
- return url
- print 'failed to create'
- return None
- def get_resource(url,folderpath='.'):
- import json
- if url.startswith('http') and url.find('raw') < 0:
- url = url.replace('https://pastebin.com/','https://pastebin.com/raw/').strip()
- else:
- url = 'https://pastebin.com/raw/' + url.strip()
- content = get_content_from_url(url)
- raw_dict = json.loads(content)
- root_file = raw_dict['rf']
- fname_content = raw_dict['fc']
- return root_file,fname_content
- def build(url,folderpath='.'):
- if not os.path.exists(folderpath):
- os.makedirs(folderpath)
- rf,fc = get_resource(url,folderpath)
- for folder_route,files_in_side in rf.iteritems():
- full_folder_route = os.path.join(folderpath,folder_route)
- if not os.path.exists(full_folder_route):
- os.makedirs(full_folder_route)
- print 'folder created:',full_folder_route
- for f in files_in_side:
- full_file_path = os.path.join(folderpath,folder_route,f)
- if f not in fc.keys():
- raise Exception('FileContentMissing','Cannot find the content of file' + str(f))
- content = fc[f]
- content = content.encode("utf-8")
- print 'file_path',full_file_path
- with open(full_file_path,'w') as fw:
- fw.write(content)
- print 'All done! Contents are located in ' + os.path.abspath(folderpath)
- def parse_type_list(type_list_str):
- types = type_list_str.split(',')
- types_ = []
- for t in types:
- if not t.strip().startswith('.'):
- types_.append('.' + t.strip())
- else:
- types_.append(t.strip())
- return types_
- def interactive_push():
- print 'Please input the local folder you want to push to cloud.'
- folder_path = raw_input('>')
- while not os.path.isdir(folder_path):
- print '{} is not a valid folder, please reinput the local folder.'.format(folder_path)
- print '[example]/Users/Test/MyProject'
- folder_path = raw_input('>')
- folder_path = folder_path.strip()
- print 'Please input the target text file extensions, separated by commar.'
- print '[example] txt,py,md,xml'
- list_str = raw_input('>')
- type_list = parse_type_list(list_str)
- print 'Please wait for seconds..'
- push(folder_path,type_list)
- return True
- def interactive_pull():
- print 'Please input the project pastebin key or url.'
- url = raw_input('>')
- if sys.platform == 'darwin' or sys.platform.startswith('win'):
- print 'Please input the existing destination folder path.'
- dest = raw_input('>')
- while not os.path.isdir(dest):
- print 'Destination folder path not found, please reinput.'
- dest = raw_input('>')
- print 'Please wait for seconds..'
- build(url,dest)
- else:
- print 'Choose your current platform:\n[1]Android\n[2]Linux PC'
- choice = raw_input('>')
- choice = choice.strip()
- while choice != '1' and choice != '2':
- print 'Choose your current platform:\n[1]Android\n[2]Linux PC'
- choice = raw_input('>')
- choice = choice.strip()
- if choice == '1':
- default_root_path = '/storage/emulated/0/PyApps'
- if not os.path.isdir(default_root_path):
- os.makedirs(default_root_path)
- build(url,default_root_path)
- elif choice == '2':
- print 'Please input the existing destination folder path.'
- dest = raw_input('>')
- while not os.path.isdir(dest):
- print 'Destination folder path not found, please reinput.'
- dest = raw_input('>')
- print 'Please wait for seconds..'
- build(url,dest)
- return True
- def interactive_command():
- print 'Please choose action:\n[1]Push/Upload\n[2]Pull/Download'
- choice = raw_input('>')
- if choice.strip() == '1':
- return interactive_push()
- elif choice.strip() == '2':
- return interactive_pull()
- return interactive_command()
- if __name__ == '__main__':
- if len(sys.argv) == 1:
- print '\n'
- print 'Sky Installer 2.0'
- print '\n'
- print '#' * 20
- print "You can upload/push your app to the cloud using:"
- print '--push <app_folder> <type_list> or --upload <app_folder> <type_list>'
- print "Or, you can download app from the cloud using:"
- print '--pull <url> <destination_path> or --download <url> <destination_path>'
- print '#' * 20
- print '\n' * 2
- print 'Examples:'
- print './skyinstaller.py --push /Users/Test/MyLocalApp py,txt,md'
- print './skyinstaller.py --pull https://pastebin.com/h3eeRJcP /Users/Test/MyApp'
- #print 'You can create a file named ignore.txt to list the files you do not want to include'
- print 'Happy Using!'
- print '#' * 20
- success = interactive_command()
- if success:
- print 'Successful.'
- if len(sys.argv) == 4:
- if sys.argv[1].strip() == '--push' or sys.argv[1].strip() == '--upload':
- #do the push
- app_folder = sys.argv[2]
- type_list = parse_type_list(sys.argv[3])
- push(app_folder,type_list)
- elif sys.argv[1].strip() == '--pull' or sys.argv[1].strip() == '--download':
- #do the pull
- url = sys.argv[2].strip()
- dest = sys.argv[3].strip()
- build(url,dest)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement