Skip to content

Commit 87efff0

Browse files
committed
Now instead of only getting one copy of the subdomain, it'll track how many times its been seen so that we can order by popularity
1 parent f41d83e commit 87efff0

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

Substr3am.py

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import re
1414
import argparse
1515

16-
from sqlalchemy import create_engine
16+
from sqlalchemy import create_engine, update
1717
from declarative_sql import Subdomain, Base
1818
from sqlalchemy.orm import sessionmaker
1919

@@ -102,12 +102,23 @@ def print_callback(message, context):
102102
# It doesn't exist...
103103
if not subdomain_exists:
104104
# ...so create it
105-
subdomain_new = Subdomain(subdomain=subdomain)
105+
subdomain_new = Subdomain(subdomain=subdomain, count=1)
106106
session.add(subdomain_new)
107107
session.commit()
108108

109109
# Debug line
110110
print(subdomain)
111+
112+
# It does exist
113+
if subdomain_exists:
114+
# Add one to the counter to track its popularity
115+
counter = subdomain_exists.count + 1
116+
117+
# Add 1 to the counter
118+
session.query(Subdomain).filter(Subdomain.id == subdomain_exists.id).\
119+
update({'count': counter})
120+
session.commit()
121+
111122

112123
def dump():
113124
# Set up the connection to the sqlite db
@@ -117,12 +128,15 @@ def dump():
117128
Session.configure(bind=engine)
118129
session = Session()
119130

120-
# Get all the subdomains
121-
subdomains = session.query(Subdomain).all()
131+
# Get all the subdomains from the DB
132+
subdomains = session.execute("SELECT * FROM subdomains ORDER BY count DESC").fetchall()
133+
134+
# Assuming there's anything in the list...
122135
if len(subdomains) > 0:
136+
# Open the file
123137
f = open("names.txt", "w")
124138
for subdomain in subdomains:
125-
# And write them to a file
139+
# And write them
126140
f.write(subdomain.subdomain)
127141
f.write("\r\n")
128142
f.close()

declarative_sql.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ class Subdomain(Base):
1717
__tablename__ = 'subdomains'
1818
id = Column(Integer, primary_key=True)
1919
subdomain = Column(String(250), nullable=False)
20+
count = Column(Integer)
2021

2122
subdomains_db = 'sqlite:///subdomains.db'
2223
engine = create_engine(subdomains_db)

0 commit comments

Comments
 (0)