1313import re
1414import argparse
1515
16- from sqlalchemy import create_engine
16+ from sqlalchemy import create_engine , update
1717from declarative_sql import Subdomain , Base
1818from 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
112123def 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 ()
0 commit comments