Just wondering what I did bad/could have done better. It's a simple script for pulling information from Leafly about cannabis strains. I've also added in a function to search. I am fairly new to Python. Anything is greatly appreciated. I also realize nothing is commented. I will quickly answer any questions, but it's simple enough, so hopefully you can read it.
# PYTHON 2.7.3
# Leafly API test
# STumbles
import urllib2
import json
import sys
import pprint
class Leafly:
def __init__(self):
self.author = 'STumbles'
self.title = 'Leafly API python'
self.python_version = '2.7.3'
def get_info(self):
self.search_term = raw_input('[*] Leafly [*]' + '\n\r'*23+': ').lower().replace(' ', '-')
self.url = 'http://www.leafly.com/api/details/%s' % self.search_term
dict_a = json.loads(urllib2.urlopen(self.url).read())
if (dict_a) != {}:
return dict_a
else:
print 'Please type a valid strain name.'
def specific_info(self, dict_a):
try:
self.container = []
if dict_a['Overview']:
self.overview = dict_a['Overview']
try:
self.overview = str(self.overview)
except UnicodeEncodeError:
pass
else:
print "could not open Overview"
if dict_a['Rating']:
self.rating = str(dict_a['Rating']) + '/10'
else:
print "could not open Rating"
if dict_a['Name']:
self.name = dict_a['Name']
else:
print "could not open Name"
if dict_a['Abstract']:
self.details = dict_a['Abstract']
else:
print "could not open Abstract"
if dict_a['Category']:
self.category = 'it is a %s' % dict_a['Category']
else:
print "could not open Category"
if dict_a['Medical'][0]['Name']:
self.medical = 'Helps with %s' % dict_a['Medical'][0]['Name']
else:
print "could not open Medical"
if dict_a['Effects'][0]['Name']:
self.effect = 'Makes you %s' % dict_a['Effects'][0]['Name']
else:
print "could not open Effects"
if dict_a['Negative'][0]['Name']:
self.negative = 'Causes %s' % dict_a['Negative'][0]['Name']
else:
print "could not open Negative"
try:
self.container.append(self.name)
self.container.append(self.rating)
self.container.append(self.overview)
self.container.append(self.details)
self.container.append(self.category)
self.container.append(self.medical)
self.container.append(self.effect)
self.container.append(self.negative)
return self.container
except AttributeError:
pass
except TypeError:
pass
def search(self):
print '\n'*23
self.search_url = json.loads(urllib2.urlopen("http://www.leafly.com/api/strains").read())
self.keyword = raw_input("Search: ").lower().replace(' ', '-')
for i in self.search_url:
for keys in xrange(1):
if self.keyword in i['Key']:
print i['Key']
else:
pass
print '\n'*23
flags = raw_input("Press 1 to search, or press 2 to get strain information\n[*] : ")
try:
if flags == 1 or 2:
flags = int(flags)
except ValueError:
print "please enter a valid number"
leafly = Leafly()
if flags == 2:
weed_info = leafly.get_info()
strain_info = leafly.specific_info(weed_info)
try:
for i in strain_info:
print i
except TypeError:
pass
if flags == 1:
leafly.search()
else:
pass