Created
July 6, 2024 10:45
-
-
Save Rajdave69/a42c39cc7da5336ded124273c9537977 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# This code downloads attachments from a specific user in discord data packages | |
# | |
# | |
import os, requests, csv | |
# for folder in current directory | |
for folderName, subfolders, filenames in os.walk('A:\Discord Packages\user-folder'): | |
print('The current folder is ' + folderName) | |
# for each .csv file in folder | |
for filename in filenames: | |
if filename.endswith('.csv'): | |
print('Downloading attachments from ' + os.path.basename(filename) + '...') | |
try: | |
# open file | |
csvFileObj = open(os.path.join(folderName, filename), encoding="utf8") | |
csvReader = csv.reader(csvFileObj) | |
# the 5th column is "Attachments" in the csv file, for each row in the csv file download the attachment and save it in a new folder named csv file name | |
for row in csvReader: | |
if row[4] != None and row[4] != '' and row[4] != 'Attachments': | |
print(row[4]) | |
# download attachment | |
print('Downloading attachment from ' + row[4] + ' in ' + os.path.basename(filename)) | |
if "," in row[4]: | |
for media in row[4].split(","): | |
# check if it exists in a folder with the name of the CSV file | |
if os.path.isfile(os.path.join(folderName, os.path.splitext(filename)[0], os.path.basename(media))): | |
print("File already exists in folder " + os.path.join(folderName, os.path.splitext(filename)[0], os.path.basename(media))) | |
continue | |
response = requests.get(media) | |
response.raise_for_status() | |
# save attachment to new folder | |
csvFileName = os.path.basename(filename) | |
csvPath = os.path.join(folderName, csvFileName) | |
csvPath = os.path.dirname(csvPath) | |
csvPath = os.path.join(csvPath, os.path.splitext(csvFileName)[0]) | |
os.makedirs(csvPath, exist_ok=True) | |
attachmentFile = open(os.path.join(csvPath, os.path.basename(media)), 'wb') | |
for chunk in response.iter_content(100000): | |
attachmentFile.write(chunk) | |
attachmentFile.close() | |
else: | |
# check if it exists in a folder with the name of the CSV file | |
if os.path.isfile(os.path.join(folderName, os.path.splitext(filename)[0], os.path.basename(row[4]))): | |
print("File already exists in folder " + os.path.join(folderName, os.path.splitext(filename)[0], os.path.basename(row[4]))) | |
continue | |
response = requests.get(row[4]) | |
response.raise_for_status() | |
# save attachment to new folder | |
csvFileName = os.path.basename(filename) | |
csvPath = os.path.join(folderName, csvFileName) | |
csvPath = os.path.dirname(csvPath) | |
csvPath = os.path.join(csvPath, os.path.splitext(csvFileName)[0]) | |
os.makedirs(csvPath, exist_ok=True) | |
attachmentFile = open(os.path.join(csvPath, os.path.basename(row[4])), 'wb') | |
for chunk in response.iter_content(100000): | |
attachmentFile.write(chunk) | |
attachmentFile.close() | |
csvFileObj.close() | |
except: | |
print("!!!!Error downloading attachments from " + os.path.basename(filename)) | |
# for each subfolder in folder | |
for subfolder in subfolders: | |
for filename in filenames: | |
if filename.endswith('.csv'): | |
# open file | |
try: | |
# open file | |
csvFileObj = open(os.path.join(folderName, filename), encoding="utf8") | |
csvReader = csv.reader(csvFileObj) | |
# the 5th column is "Attachments" in the csv file, for each row in the csv file download the attachment and save it in a new folder named csv file name | |
for row in csvReader: | |
if row[4] != None and row[4] != '' and row[4] != 'Attachments': | |
print(row[4]) | |
# download attachment | |
print('Downloading attachment from ' + row[4] + ' in ' + os.path.basename(filename)) | |
if "," in row[4]: | |
for media in row[4].split(","): | |
# check if it exists in a folder with the name of the CSV file | |
if os.path.isfile(os.path.join(folderName, os.path.splitext(filename)[0], os.path.basename(media))): | |
print("File already exists in folder " + os.path.join(folderName, os.path.splitext(filename)[0], os.path.basename(media))) | |
continue | |
response = requests.get(media) | |
response.raise_for_status() | |
# save attachment to new folder | |
csvFileName = os.path.basename(filename) | |
csvPath = os.path.join(folderName, csvFileName) | |
csvPath = os.path.dirname(csvPath) | |
csvPath = os.path.join(csvPath, os.path.splitext(csvFileName)[0]) | |
os.makedirs(csvPath, exist_ok=True) | |
attachmentFile = open(os.path.join(csvPath, os.path.basename(media)), 'wb') | |
for chunk in response.iter_content(100000): | |
attachmentFile.write(chunk) | |
attachmentFile.close() | |
else: | |
# check if it exists in a folder with the name of the CSV file | |
if os.path.isfile(os.path.join(folderName, os.path.splitext(filename)[0], os.path.basename(row[4]))): | |
print("File already exists in folder " + os.path.join(folderName, os.path.splitext(filename)[0], os.path.basename(row[4]))) | |
continue | |
response = requests.get(row[4]) | |
response.raise_for_status() | |
# save attachment to new folder | |
csvFileName = os.path.basename(filename) | |
csvPath = os.path.join(folderName, csvFileName) | |
csvPath = os.path.dirname(csvPath) | |
csvPath = os.path.join(csvPath, os.path.splitext(csvFileName)[0]) | |
os.makedirs(csvPath, exist_ok=True) | |
attachmentFile = open(os.path.join(csvPath, os.path.basename(row[4])), 'wb') | |
for chunk in response.iter_content(100000): | |
attachmentFile.write(chunk) | |
attachmentFile.close() | |
csvFileObj.close() | |
except: | |
print("!!!!Error downloading attachments from " + os.path.basename(filename)) | |
print('SUBFOLDER OF ' + folderName + ': ' + subfolder) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment