KnoemaUpload Package
The knoemaupload package uploads documents via API to Mckinsey portal.
Sample calling script:
from Upload import KnoemaUpload filepath=r"Path_Of_Your_Document_Upload" # Fill your own Path and Cookies. cookies= {Your_Cookies} # Generate your own cookies. obj=KnoemaUpload.KnoemaDocumentUploader(filepath,cookies) obj.FileUpload('client_name') # There is an option to pass 'headers' as parameter in FileUpload()
3. Package code:
import requests
import os, sys
import json
import smtplib
EMAIL_ADDRESS = 'ssubramanian@knoema.com'
EMAIL_PASSWORD = 'mtwnswdyohactvfm'
EMAIL_RECEIVERS = 'third-party-data-etl@knoema.com'
class KnoemaDocumentUploader():
UPL_URL='https://tmt.knoema.com/document/upload'
DOCID_URL = 'https://tmt.knoema.com/document/getlink?id='
headers={'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36'}
PUBLIC_URL = 'https://tmt.knoema.com/resource/share'
def __init__(self,filepath,cookies):
self.filepath= filepath
self.cookies=cookies
self.filename=os.path.basename(filepath)
#print(self.filename)
def FileUpload(self,CLIENT,headers=headers):
files_ = {'file': (self.filename, open(self.filepath, 'rb'))}
#try:
response = requests.post(KnoemaDocumentUploader.UPL_URL, cookies=self.cookies, files=files_)
print('>>>response status code: ', response.status_code, '\n')
text_res=response.text
if (len(text_res)>300 and "errorBeacon" in text_res):
subject = f'Mckinsey TMT File Upload Failed--Update {CLIENT} Cookies'
body = f'The upload failed most likely due to stale cookie. Update the cookie for {CLIENT}'
msg = f'Subject: {subject} \n\n Message: {body}'
with smtplib.SMTP('smtp.gmail.com',587) as smtp:
smtp.ehlo()
smtp.starttls()
smtp.ehlo()
smtp.login(EMAIL_ADDRESS, EMAIL_PASSWORD)
smtp.sendmail(EMAIL_ADDRESS,EMAIL_RECEIVERS,msg)
print(">>>> Exiting the routine due to stale cookie")
sys.exit()
json_ = json.loads(response.text)
if 'error' in json_.keys():
raise Exception(str(json_['error']))
else:
pass
res = requests.get(KnoemaDocumentUploader.DOCID_URL + json_['id'], cookies=self.cookies)
file_url = res.json()
payload = {"IsPublic": "true", "Id": json_['id']}
r = requests.post(KnoemaDocumentUploader.PUBLIC_URL, cookies=self.cookies, data=json.dumps(payload), headers = {'Content-Type': 'application/json'})
test_url = 'https://tmt.knoema.com/' + json_['id']
#print(">>>Test URL for developer to delete test files",test_url)
download_url = 'https://tmt.knoema.com/'+file_url
print('>>>Download file url for users :', download_url)
file = open('Download.txt', 'a')
file.writelines(download_url + '\n')
file.close()
4. Contents of setup.cfg:
[metadata]
name = KnoemaUpload
version = 0.4.1
author = Vasanth
author_email = vbalakrishna@knoema.com
description = Used for only internal purpose of the knoema employees to upload files to portal via API.
long_description = file: README.md
long_description_content_type = text/markdown
url = https://github.com/pypa/sampleproject
project_urls =
Bug Tracker = https://github.com/pypa/sampleproject/issues
classifiers =
Programming Language :: Python :: 3
License :: OSI Approved :: MIT License
Operating System :: OS Independent
[options]
package_dir =
= src
packages = find:
python_requires = >=3.6
[options.packages.find]
where = src
5. Update the version in the setup.cfg before building a new version of the package.
6. To push the new version of the package one must require the
username: Vasanth_Balakrishna
password: Vasanth7003
7. The folder structure of the package is:
8. To make any changes to the package one must open >>src\Upload\ folder and edit KnoemaUpload.py .
This is the only script in this package, after completing the changes we should push this to pypi repo as new version of the package.
9. How to push new version to pypi repo:
a) After editing the script navigate to the folder shown in the Fig 1 (the name of this folder itself is not important).
b) Change the version name of the package in setup.cfg
c) Open cmd in this folder and build the package by :
py -m build
d) This will create a new version of the package inside the dist folder.
e) To push the code to pypi repo:
py -m twine upload --repository pypi dist/*
Note: One must have installed both ‘Build' and 'Twine’ packages before hand.
f) It will ask for the username and password.
username: Vasanth_Balakrishna
password: Vasanth7003
g) The above mentioned username and password is linked with my personal email id unfortunately.
h) General link on how to push new package to pypi: Packaging Python Projects - Python Packaging User Guide