<commit_before>import requests
import smtplib
import json
import configparser
import json
import time
"""
Find some cool stuff.
"""
config = configparser.ConfigParser()
config.read('config.ini')
ourthings = json.load(open('things.json'))
params = json.load(open('params.json'))

DEBUG = True

def process_search(text):
	hitresults = json.loads(text)
	for ad in hitresults:
		print(time.strftime('%m/%d/%Y %H:%M:%S', time.gmtime(ad['display_time']/1000.)))

server = None
if not DEBUG:
	server = smtplib.SMTP('smtp.gmail.com', 587)
	server.starttls()
	server.login(config['EMAIL']['email'], config['EMAIL']['key'])

for email, info in ourthings.items():
	things = info['things']
	print("Checking {} stuff".format(email))
	for cool_thing in things:
		params['s'] = cool_thing
		print("\t- {}".format(cool_thing))

		r = requests.post(config['PARAMS']['api_base'], data=params)
		process_search(r.text)
		if not DEBUG:
			server.sendmail(config['EMAIL']['email'], email, msg)
			server.quit()



<commit_msg>Finished script. Works pretty decent. Added logging. Add more html to email.<commit_after>import requests
import smtplib
import json
import configparser
import json
import time
from datetime import datetime, timedelta
import logging

from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.mime.application import MIMEApplication

"""
Find some cool stuff.
"""
config = configparser.ConfigParser()
config.read('config.ini')
ourthings = json.load(open('things.json'))
params = json.load(open('params.json'))

logging.basicConfig(filename='klassifinds.log',level=logging.INFO)

DEBUG = False

HTML_EMAIL_TEMPLATE = """\
			<div class="column">
			<img class="thumbnail" src="{}{}">
			<h5>{}   |   {}    |    {}/{}    | {}</h5>
			<a href="{}nid={}&ad={}">Go to ad</a>
			</div>
		"""

def process_ad(ad, keywords):
	ad_params = {
		"cmd": "ad",
		"id": ad['sid']
	}
	r = requests.post(config['PARAMS']['api_base'], data=ad_params)
	ad_info = json.loads(r.text)[0]
	ad_description = []
	if ad_info['title']:
		ad_despcrition = ad_info['title'].split()
	if ad_info['body']:
		ad_description += ad_info['body'].split()

	ad_description = [word.lower() for word in ad_description]
	keyword_hits = []
	for word in keywords:
		if word.lower() in ad_description:
			keyword_hits.append(word)

	return_string = ""
	if keyword_hits:
		return_string = HTML_EMAIL_TEMPLATE.format(ad['image'], config['PARAMS']['thumbnail'], ad_info['title'], 
			ad_info['price'], ad_info['city'], ad_info['state'], ' '.join(keyword_hits),
			config['PARAMS']['web_base'], '218', ad['sid'])
	else:
		return_string = None

	return return_string

def handle_things(email, things):
	logging.info("Checking {} stuff".format(email))
	# Create message container.
	email_summary = MIMEMultipart('related')
	email_summary['Subject'] = config['EMAIL']['subject']
	email_summary['From'] = config['EMAIL']['email']
	email_summary['To'] = email
	email_contents = []
	total_hits = 0
	for cool_thing, info in things.items():

		params['s'] = cool_thing
		logging.info("\t- {}".format(cool_thing))
		max_price = info['max']
		keywords = info['keywords']

		r = requests.post(config['PARAMS']['api_base'], data=params)
		hitresults = json.loads(r.text)
		now = datetime.now()
		last_run = now - timedelta(hours = 1)
		promising_ads = [ad for ad in hitresults if (datetime.fromtimestamp(ad['display_time']) > last_run) and ad['image'] and float(ad['price']) < max_price ]
		hits = 0
		ad_hit_contents = []
		for ad in promising_ads:
			#Only process ads from the last hour...
			result = process_ad(ad, keywords)
			if result:
				hits += 1
				ad_hit_contents.append(result)
		if hits:
			email_contents.append("<h2>{}</h2>".format(' '.join(cool_thing.split('+')).title()))
			email_contents.extend(ad_hit_contents)
			email_contents.append('<hr>')
		total_hits += hits
	html = ''.join(email_contents)
	email_summary.attach(MIMEText(html, 'html'))
	return total_hits, email_summary

def klassifinds():
	server = smtplib.SMTP('smtp.gmail.com', 587)
	server.starttls()
	server.login(config['EMAIL']['email'], config['EMAIL']['key'])

	for email, things in ourthings.items():
		total_hits, email_summary = handle_things(email, things)
		if total_hits and not DEBUG:
			logging.info("Sending {} Klassifind winners to {}.".format(total_hits, email))
			server.sendmail(config['EMAIL']['email'], email, email_summary.as_string())

if __name__ == '__main__':
	klassifinds()
{'fraction_non_alphanumeric': 0.1680728241563055, 'fraction_numerical': 0.004884547069271759, 'mean_word_length': 5.300699300699301, 'pattern_counts': {'":': 2, '<': 14, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '54706', 'n_tokens_mistral': 1653, 'n_tokens_neox': 1544, 'n_words': 334}