Last active
January 12, 2020 06:56
-
-
Save SeolHa314/491ce4d17fd73351f27c8f8af9b16ba9 to your computer and use it in GitHub Desktop.
Dccon Download
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
import asyncio | |
import aiohttp | |
import requests | |
import sys | |
import os | |
import urllib.parse | |
from bs4 import BeautifulSoup | |
helptext = """ | |
This program is designed to download Dcinside's icon, Dccon. | |
This program will break if Dcinside changes its structre of webpage. | |
Since this program may violate DCinside's TOS, there is a danger of getting banned if you use heavily. Be sure to rate-limit yourself. | |
-h, --help: Display help. | |
-d, --download [Dccon number]: Download Dccon [Dccon number]. | |
-s, --search [Dccon name]: Search for [Dccon name]. | |
-v, --verbose: Display verbose log. (NOT IMPLAMENTED) | |
""" | |
def getDcconInfo(DcconNumber): | |
dcconUrl = "https://dccon.dcinside.com/index/package_detail" | |
data = {"package_idx": str(DcconNumber) } | |
header = {"X-Requested-With": "XMLHttpRequest"} | |
r = requests.post(dcconUrl, data=data, headers=header) | |
r.raise_for_status() | |
return r.json() | |
async def download(DcconNo, DcconName, DcconExt, DcconTitle): | |
async with aiohttp.ClientSession(headers={"Referer": "https://dccon.dcinside.com"}) as session: | |
async with session.get("https://dcimg5.dcinside.com/dccon.php?no=" + DcconNo) as con: | |
with open(DcconTitle + '/' + DcconName + "." + DcconExt, "wb") as f: | |
f.write(await con.read()) | |
def downloadDccon(DcconData): | |
if not os.path.isdir(DcconData["info"]["title"]): | |
os.mkdir(DcconData["info"]["title"]) | |
else: | |
return False | |
tasks = [download(x["path"], x["title"], x["ext"], DcconData["info"]["title"]) for x in DcconData["detail"]] | |
loop = asyncio.get_event_loop() | |
loop.run_until_complete(asyncio.wait(tasks)) | |
return True | |
def searchDccon(searchName): | |
searchUrl = "https://dccon.dcinside.com/hot/1/title/" | |
nameEncoded = urllib.parse.quote(searchName) | |
r = requests.get(searchUrl + nameEncoded) | |
r.raise_for_status() | |
soup = BeautifulSoup(r.content, "html.parser") | |
if soup.select("#right_cont_wrap > header > h3 > span")[0].get_text() == "(0건)": | |
print("No Dccon found.") | |
return | |
dcconLists = soup.select("#right_cont_wrap > div > div.dccon_listbox > ul > li") | |
askLists = [] | |
for i in dcconLists: | |
askLists.append([int(i["package_idx"]), i.find(class_= "dcon_name").get_text()]) | |
for i in range(len(askLists)): | |
print(f"{i + 1})", askLists[i][1]) | |
desiredNum = int(input("Input number of DCcon you want to download : ")) | |
if desiredNum > len(askLists) + 1: | |
print("Input proper Number.") | |
return | |
else: | |
DcconInfo = getDcconInfo(askLists[desiredNum - 1][0]) | |
if not downloadDccon(DcconInfo): | |
print("Download Error") | |
return | |
else: | |
print("Downloaded") | |
return | |
def main(): | |
arguments = sys.argv | |
downloadFlag_short, downloadFlag_long, searchFlag_short, searchFlag_long = False, False, False, False | |
mode = 0 # 1 is download, 2 is search | |
dcconNum = 0 | |
if len(arguments) == 1: | |
print("Input an argument. To view help, try using argument \"--help\"") | |
return | |
if "-h" in arguments or "--help" in arguments: | |
print(helptext) | |
return | |
elif "-d" in arguments: | |
downloadFlag_short = True | |
mode = 1 | |
elif "--download" in arguments: | |
downloadFlag_long = True | |
mode = 1 | |
elif "-s" in arguments: | |
searchFlag_short = True | |
mode = 2 | |
elif "--search" in arguments: | |
searchFlag_long = True | |
mode = 2 | |
elif "-v" in arguments or "--verbose" in arguments: | |
verboseFlag = True | |
else: | |
print("Wrong argument.") | |
print(helptext) | |
return | |
if (downloadFlag_short or downloadFlag_long) and (searchFlag_short or searchFlag_long): | |
print("You can't search and download at same time.") | |
print(helptext) | |
return | |
try: | |
if downloadFlag_short: | |
dcconNum = int(arguments[arguments.index("-d") + 1]) | |
elif downloadFlag_long: | |
dcconNum = int(arguments[arguments.index("--download") + 1]) | |
else: | |
pass | |
except ValueError: | |
print("Input a proper Dccon number to download. Only numeric is permitted.") | |
return | |
except IndexError: | |
print("Input a Dccon number") | |
return | |
try: | |
if searchFlag_short: | |
searchName = arguments[arguments.index("-s") + 1] | |
elif searchFlag_long: | |
searchName = arguments[arguments.index("--search") + 1] | |
else: | |
pass | |
except IndexError: | |
print("Input Dccon name") | |
return | |
if mode == 1: | |
#print(dcconNum) | |
conInfo = getDcconInfo(dcconNum) | |
if not downloadDccon(conInfo): | |
print("Unknown donwload error") | |
elif mode == 2: | |
searchDccon(searchName) | |
else: | |
print("Mode Error.") | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment