Skip to content

Feature/directory upload support #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 48 additions & 2 deletions neocities/neocities.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def delete(self, *filenames):
for i in filenames:
args['filenames[]'].append(i)
if self.api_key:
response = requests.get(self._request_url('delete'), data=args, headers={'Authorization':'Bearer '+self.api_key})
response = requests.post(self._request_url('delete'), data=args, headers={'Authorization':'Bearer '+self.api_key})
else:
response = requests.post(self._request_url('delete'), auth=self.auth, data=args)
return self._decode(response)
Expand All @@ -92,6 +92,12 @@ def upload(self, *filenames):
(name_on_disk, name_on_server)
Note: name_on_server must include the file extension.

AND/OR

filenames: directory
If directory exists on server, will upload files to existing directory.
If directory does not exist on server, will create directory and upload files.

Returns
-------
request : dict
Expand All @@ -101,7 +107,47 @@ def upload(self, *filenames):

# NeoCities API expects a dict in the following format:
# { name_on_server: <file_object> }
args = {pair[1]: open(pair[0], 'rb') for pair in filenames}
from os.path import isdir

def __handle_dir(directory):
"""
Extracts files from directory as list of tuples (file_name, file_path)
"""
from os import listdir
from os.path import join

# print(f'dicrectory {directory} found. Parsing.')
tuples = list()

files = listdir(directory)

for file in files:
if isdir(join(directory, file)):
# print(f'inner directory {file} found'. Parsing.)
tuples.extend(__handle_dir(join(directory,file)))

else:
tuples.append((join(directory,file), join(directory,file)))

return tuples

args = dict()
tuples = list()

filenames = list(filenames)

for file in filenames:
if type(file) == tuple:
tuples.append((file[0], file[1]))
elif isdir(file):
tuples = __handle_dir(file)


for item in tuples:
print(item)
args[item[1]] = open(item[0], 'rb')


if self.api_key:
response = requests.post(self._request_url('upload'), files=args, headers={'Authorization':'Bearer '+self.api_key})
else:
Expand Down