Ejemplo n.º 1
0
def user(user_id):
    try:
        user_name = select(user_id)
        return "<H1 id='user'>" + user_name + "</H1>"
    except Exception as e:
        print(e)
        return "<H1 id='error'>no such user: "******"</H1>"
Ejemplo n.º 2
0
def users(user_id):
    if request.method == 'POST':
        request_data = request.json
        user_name = request_data.get('user_name')
        result = insert(user_id, user_name)

        if result==1:
            return {'status': 'ok', 'user_added': user_name}, 200 # status code
        elif result==2:
            return {'status': 'error', 'reason': 'id already exists'}, 500
        elif result==3:
            return {'status': 'error', 'reason': "Can't connect to MySQL server"}, 500
        else:
            return {'status': 'error', 'reason': 'unknown'}, 500
    elif request.method == 'GET':
        result = select(user_id)

        if result not in {2, 3, 4}:
            return {'status': 'ok', 'user_name': result}, 200 # status code
        elif result == 2:
            return {'status': 'error', 'reason': 'no such id'}, 500
        elif result == 3:
            return {'status': 'error', 'reason': "Can't connect to MySQL server"}, 500
        else:
            return {'status': 'error', 'reason': 'unknown'}, 500

    elif request.method == 'PUT':
        request_data = request.json
        user_name = request_data.get('user_name')
        result = update(user_id, user_name)

        if result == 1:
            return {'status': 'ok', 'user_updated': user_name}, 200  # status code
        elif result == 2:
            return {'status': 'error', 'reason': 'no such id'}, 500
        elif result == 3:
            return {'status': 'error', 'reason': "Can't connect to MySQL server"}, 500
        else:
            return {'status': 'error', 'reason': 'unknown'}, 500

    elif request.method == 'DELETE':
        result = delete(user_id)
        print("delete user",user_id)
        if result == 1:
            return {'status': 'ok', 'user_deleted': user_id}, 200 # status code
        elif result == 2:
            return {'status': 'error', 'reason': 'no such id'}, 500
        elif result == 3:
            return {'status': 'error', 'reason': "Can't connect to MySQL server"}, 500
        else:
            return {'status': 'error', 'reason': 'unknown'}, 500
Ejemplo n.º 3
0
def user(user_id):
    if request.method == 'POST':
        try:
            # getting the json data payload from request
            request_data = request.json
            # treating request_data as a dictionary to get a specific value from key
            user_name = request_data.get('user_name')
            if insert(user_id, user_name):
                return {
                    'status': 'ok',
                    'user_added': user_name
                }, 200  # status code
            else:
                return {'status': 'error', 'reason': 'id already exists'}, 500
        except Exception as e:
            print(e)
            return {'status': 'error', 'reason': 'id already exists'}, 500
    elif request.method == 'GET':
        try:
            user_name = select(user_id)
            return {'status': 'ok', 'user_name': user_name}, 200  # status code
        except Exception as e:
            print(e)
            return {'status': 'error', 'reason': 'no such id'}, 500
    elif request.method == 'PUT':
        try:
            # getting the json data payload from request
            request_data = request.json
            # treating request_data as a dictionary to get a specific value from key
            user_name = request_data.get('user_name')
            update(user_id, user_name)
            return {
                'status': 'ok',
                'user_updated': user_name
            }, 200  # status code
        except Exception as e:
            print(e)
            return {'status': 'error', 'reason': 'no such id'}, 500
    elif request.method == 'DELETE':
        try:
            delete(user_id)
            return {
                'status': 'ok',
                'user_deleted': user_id
            }, 200  # status code
        except Exception as e:
            print(e)
            return {'status': 'error', 'reason': 'no such id'}, 500
Ejemplo n.º 4
0
from rest_app import user
from db_connector import select
from selenium import webdriver
from selenium.webdriver.common.keys import Keys

# backend testing, get info from sql to flask web as json.

res = requests.post('http://127.0.0.1:5000/users/1',
                    json={"user_name": "itay"})

res = requests.get('http://127.0.0.1:5000/users/1')

select()

driver = webdriver.Chrome(
    executable_path="C:\\Users\\Itay Zrihan\\Desktop\\ChromeDriver.exe")
driver.get("http://127.0.0.1:5001/users/2")
def main(filename, crashed=False): #"crashed" is an option to continue from the current state if the requests time out
	#print filename
	#print crashed

	h = html2text.HTML2Text()
	h.ignore_links = True
	stemmer = PorterStemmer()
	tf_folder_path = os.path.join(os.getcwd(), 'tf')
	if not os.path.exists(tf_folder_path):
		os.mkdir(tf_folder_path)
	corpus = set()
	pause_time = 0.5

	if check_validity:
		valid_words = set(str(stemmer.stem(line.rstrip().lower())) for line in open(valid_words_file, 'r'))
		stop_words = set(str(stemmer.stem(line.rstrip().lower())) for line in open(stop_words_file, 'r'))
		keywords = set(str(stemmer.stem(word.lower()) for word in extra_search_keywords.split()))
		stop_words = stop_words.union(keywords)

	if logging:
		log = open('tf-log', 'w')

	# Step 1: Find all distinct specialty classes.
	connection = sqlite3.connect(db_name)
	c = connection.cursor()
	db.select(c, ['specialty'], 'th_specialties', distinct=True)
	issues = set(str(re.sub(r'[^a-zA-Z]+', ' ', i[0])).lower() for i in c.fetchall())
	connection.close()
	if logging:
		log.write("Issues: \n")
		log.write(', '.join(issues))
		log.write('\n\n')

	print "Step 1 complete."

	# Step 2: For each category, find the top num_articles google results and generate tf counts of the stemmed plaintext.

	if crashed:
		completed = set(f for f in os.listdir(tf_folder_path) if os.path.isfile(os.path.join(tf_folder_path, f)))
		issues = issues - completed
		#print issues

	for issue in issues:
		results = search(issue + ' ' + extra_search_keywords, stop = num_articles, pause = pause_time)
		urls = [str(url) for url in results][: num_articles]
		
		if logging:
			print issue
			log.write('Issue: ' + issue + '\n')
			log.write('\n'.join(urls))
			log.write('\n\n')

		cumulative = []

		for url in urls:
			if not url.endswith('.pdf'):
				try:
					html = urllib2.urlopen(url) #gets the raw html of the url
					plaintext = h.handle(unicode(html.read(), 'ISO-8859-1')) #converts the html into plaintext
					processed = re.sub(r'[^a-zA-Z]+', ' ', plaintext)
					if check_validity:
						for word in processed.split():
							processed = str(stemmer.stem(word.lower()))
							if processed not in stop_words and processed in valid_words:
								cumulative.append(processed)
					else:
						stemmed = [str(stemmer.stem(word.lower())) for word in processed.split()]
						cumulative += stemmed
				except: #mostly to ignore urllib2 errors...
					pass
		counts = Counter(cumulative)
		tf = open(os.path.join(tf_folder_path, issue), 'w')

		for word in sorted(counts.keys()): #sort words in alphabetical order
			corpus.add(word)
			tf.write(str((word, counts[word]))) #write tuples of words with the word count
			tf.write('\n')

		tf.close()

	print "Step 2 complete."

	# Step 3: Combine files

	files = sorted(issues)
	num_files = len(files)
	count_vectors = {}
	for word in corpus:
		count_vectors[word] = [0]*num_files

	# Flesh out count_vectors
	for i in range(len(files)):
		curr = open(os.path.join(tf_folder_path, files[i]), 'r')
		for line in curr.readlines():
			pair = ast.literal_eval(line)
			count_vectors[pair[0]][i] = pair[1]
		curr.close()

	# Write to tf_matrix
	tf_matrix = open(filename, 'w')
	tf_matrix.write(','.join(files))
	tf_matrix.write('\n')

	for word in sorted(count_vectors.keys()):
		line = word + ',' + ','.join([str(num) for num in count_vectors[word]])
		tf_matrix.write(line)
		tf_matrix.write('\n')
	tf_matrix.close()

	shutil.rmtree(tf_folder_path) #removes intermediates!

	print "Step 3 complete."
		
	if logging:
		log.close()
Ejemplo n.º 6
0
c = connection.cursor()

#prof_id = '95997' #Choy
#prof_id = '35139' #Truong
#prof_id = '69009' #May
#prof_id = '66434' #Schellenberg

for i in range(num_profiles):

	#case we have wrapped back around so we're done
	if prof_id == prof_start_id and i > 0:
		break

	info = []
	exists = False #keeps track of whether we've seen the therapist before
	db.select(c, ['therapist_id'], 'therapists', where='pt_id=' + prof_id)
	try:
		therapist_id = c.fetchone()[0]
		if therapist_id is not None:
			info.append(therapist_id)
			exists = True
	except TypeError:
		pass

	print '\nID'
	print '-------------------------'
	print prof_id
	info.append(int(prof_id))
	driver.get(url % prof_id)
	soup = BeautifulSoup(driver.page_source)
	#print soup.prettify()
Ejemplo n.º 7
0
def users(user_id):
    """
1.  POST – will accept user_name parameter inside the JSON payload.
        A new user will be created in the database (Please refer to Database section) with the
        id passed in the URL and with user_name passed in the request payload.
        ID has to be unique!
    On success: return JSON : {“status”: “ok”, “user_added”: <USER_NAME>} + code: 200
    On error: return JSON : {“status”: “error”, “reason”: ”id already exists”} + code: 500
2.  GET – returns the user name stored in the database for a given user id.
        Following the example: 127.0.0.1:5000/users/1 will return john.
    On success: return JSON : {“status”: “ok”, “user_name”: <USER_NAME>} + code: 200
    On error: return JSON : {“status”: “error”, “reason”: ”no such id”} + code: 500
3.  PUT – will modify existing user name (in the database).
        Following the above example, when posting the below JSON payload to
        127.0.0.1:5000/users/1
        george will replace john under the id 1
        {“user_name”: “george”}
    On success: return JSON : {“status”: “ok”, “user_updated”: <USER_NAME>} + code: 200
    On error: return JSON : {“status”: “error”, “reason”: ”no such id”} + code: 500
4.  DELETE – will delete existing user (from database).
        Following the above (marked) example, when using delete on 127.0.0.1:5000/users/1
        The user under the id 1 will be deleted.
    On success: return JSON : {“status”: “ok”, “user_deleted”: <USER_ID>} + code: 200
    On error: return JSON : {“status”: “error”, “reason”: ”no such id”} + code: 500
"""
    if request.method == 'POST':
        my_data = request.json
        user_name = my_data.get("user_name")
        mycon = select("'Y'", "users", f"user_id = {user_id}")
        if mycon.rowcount > 0:
            return json.dumps({
                'status': 'error',
                'reason': "ID already exists"
            }), 500
        else:
            insert("users", "user_id,user_name,creation_date",
                   f"{user_id}, '{user_name}', '{datetime.datetime.now()}'")
            return json.dumps({'status': 'ok', 'user_added': user_name}), 200
    elif request.method == 'GET':
        mycon = select("user_name", "users", f"user_id = {user_id}")
        if mycon.rowcount > 0:
            for row in mycon:
                return json.dumps({'status': 'ok', 'user_name': row[0]}), 200
        else:
            return json.dumps({'status': 'error', 'reason': "No such ID"}), 500
    elif request.method == 'PUT':
        my_data = request.json
        user_name = my_data.get("user_name")
        mycon = select("'Y'", "users", f"user_id = {user_id}")
        if mycon.rowcount > 0:
            update("users", f"user_name = '{user_name}'",
                   f"user_id = {user_id}")
            return json.dumps({'status': 'ok', 'user updated': user_name}), 200
        else:
            return json.dumps({'status': 'error', 'reason': "No such ID"}), 500
    elif request.method == 'DELETE':
        mycon = select("'Y'", "users", f"user_id = {user_id}")
        if mycon.rowcount > 0:
            delete("users", f"user_id = {user_id}")
            return json.dumps({'status': 'ok', 'user deleted': user_id}), 200
        else:
            return json.dumps({'status': 'error', 'reason': "No such ID"}), 500