This repository has been archived by the owner on Oct 21, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathLUCA.py
187 lines (138 loc) · 5.7 KB
/
LUCA.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#! /usr/bin/python3
"""
This file is part of LUCA.
LUCA - LEGO Universe Creation (Lab) Archiver
Created 2013 Brickever <http://systemonbrick.wordpress.com/>
LUCA is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
LUCA is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with LUCA If not, see <http://www.gnu.org/licenses/>.
"""
import os
import sys
import time
import requests
from bs4 import BeautifulSoup
app = "LUCA"
majver = "0.4"
minver = ""
# Write window title
os.system("title {0} v{1}".format(app, majver))
localUserName = input("\nEnter your Creation Lab Username: ")
# Search the localUserName on the Creation Lab
url = "http://universe.lego.com/en-us/community/creationlab/displaycreationlist.aspx?SearchText={0}&order=oldest&show=12".format(localUserName)
r = requests.get(url).content
soup = BeautifulSoup(r)
creations = []
for link in soup.find_all('a'):
if link.get('href')[0:49] == "/en-us/Community/CreationLab/DisplayCreation.aspx":
creations.append('http://universe.lego.com' + link.get('href'))
# Check if links were found/added for the entered username
# If not, close LUCA
if not creations:
print('The username "{0}" does not result in any search entry on the Creation Lab.'.format(localUserName))
input("Press Enter to close LUCA.")
raise SystemExit(0)
# Check if one link contains the localUserName
# If not, close LUCA
r = requests.get(creations[0]).content
soup = BeautifulSoup(r)
onlineUserName = soup.find(id="ctl00_ContentPlaceHolderUniverse_HyperLinkUsername")
if localUserName.lower() == onlineUserName.string.lower():
memberid = onlineUserName.get('href')[63:99]
print("\nYour Creations are now downloading, {0}.\n".format(localUserName))
else:
print('The username "{0}" does not appear to match with any usernames online.'.format(localUserName))
input("Press Enter to close LUCA.")
raise SystemExit(0)
url = "http://universe.lego.com/en-us/community/creationlab/displaycreationlist.aspx?memberid={0}&show=48".format(memberid)
r = requests.get(url).content
soup = BeautifulSoup(r)
# Create folder to save files in,
# unless it already exists
if not os.path.exists(localUserName):
os.mkdir(localUserName)
creations = []
for link in soup.find_all('a'):
if link.get('href')[0:49] == "/en-us/Community/CreationLab/DisplayCreation.aspx":
creations.append('http://universe.lego.com' + link.get('href'))
for creation in creations:
# ------- INFORMATION GHATERING ------- #
r = requests.get(creation).content
soup = BeautifulSoup(r)
title = soup.find_all('h1')[2] #add .string to get only the text
titleString = title.string
titleString = titleString.replace('/','')
description = soup.find(id="creationInfoText")
tags = soup.find_all(class_='column-round-body')[3].contents[9]
challenge = soup.find(id="CreationChallenge").contents[1].contents[1]
date = soup.find(id="CreationUser")
date.div.decompose()
date.a.decompose()
page = '''<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>{0}</title>
</head>
<body>
{1}
{2}
{3}
{4}
{5}
</body>
</html>
'''.format(titleString, title, description, tags, challenge, date)
imgLinkList = []
i = 1
for imgLink in soup.find_all('a'):
if imgLink.get('href')[0:13] == "GetMedia.aspx":
imgLinkList.append('http://universe.lego.com/en-us/community/creationlab/'+ imgLink.get('href'))
# ------- INFORMATION WRITING ------- #
# List of illegal characters for filenames
blacklist = ["\\", "/", ":", "*", "?", '"', "<", ">", "|"]
# Makes it easier to understand what is going on.
filepath = os.path.join(os.getcwd(), localUserName)
HTMLfilename = "{0}.html".format(titleString)
for char in blacklist:
if char in HTMLfilename:
HTMLfilename = HTMLfilename.replace(char, "-")
# Write HTML documents.
with open(os.path.join(filepath, HTMLfilename), "wt") as newHTML:
newHTML.write(page)
# Display filename after it was installed,
# part of LUCA's non-GUI progress bar.
print(os.path.basename(HTMLfilename), end="\n")
for imgLink in imgLinkList:
r = requests.get(imgLink)
img = r.content
# Original filename
filename = "{0}{1}.jpg".format(titleString, i)
for char in blacklist:
if char in filename:
# If an illegal character is found, replace it with a dash
filename = filename.replace(char, "-")
# Write all non HTML files.
with open(os.path.join(filepath, filename), 'wb') as newImg:
newImg.write(img)
# Display filename after it was installed,
# part of LUCA's non-GUI progress bar.
print(os.path.basename(filename), end="\n")
i = i + 1
# Get list of all downloaded files
num_of_files = os.listdir(os.path.join(os.getcwd(), localUserName))
# Remove Thumbs.db from list
if "Thumbs.db" in num_of_files:
num_of_files.remove("Thumbs.db")
# Display success message containing number
# of files downloaded and where they were saved.
print('\n{0} files successfully downloaded and saved to \n"{1}"'.format(len(num_of_files), filepath))
input("\nPress Enter to close LUCA.\n")
raise SystemExit(0)