Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changes made when integrating with Hippo #2

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 19 additions & 6 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,28 @@
Main script file
"""

from judging import judge
from retrieval import intranet, analyze

judge_ = True
try:
from judging import judge
judge_ = False
except ImportError as e:
print( '[WARN] Failed to import judge due to %s' % e )
print( '\t No judging today' )

import logging

THEME = "Portraits"
DEADLINE = "25 January 2018"

def judge_cal():
judge.calculate_score("final_Scores.xlsx")
global judge_
if judge_:
judge.calculate_score("final_Scores.xlsx")


def intra():
intranet.do_all(THEME, DEADLINE)
def intra( args ):
intranet.do_all(THEME, DEADLINE, args)

def check_defaulters():
analyze.get_defaulters(THEME, DEADLINE)
Expand All @@ -24,7 +33,7 @@ def main( args ):
, format = '%(asctime)s %(name)-10s %(levelname)-5s %(message)s'
)
logging.debug( 'Calling intra' )
intra()
intra( args )

if __name__ == '__main__':
import argparse
Expand All @@ -46,6 +55,10 @@ def main( args ):
, dest = 'loglevel', const = logging.INFO
, help = 'Be chatty. Like academic canteen and SLC.'
)
parser.add_argument('--task', '-t'
, required = False, default = 'all'
, help = 'Which task: json|all'
)
class Args: pass
args = Args()
parser.parse_args(namespace=args)
Expand Down
33 changes: 23 additions & 10 deletions retrieval/intranet.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
from models.photography import PhotoObject as Photo
from user_constants import *


def read_main_page() -> list:
"""
Read entire page. All currently uploaded photos will be here
Expand Down Expand Up @@ -170,11 +169,17 @@ def retrieve_pics(photos: list) -> list:
count_text, re.sub('[^A-Za-z0-9]+', '', p2.title))
filename = filename[
:20] + ".jpg" # If name if more than 20 characters,
# strip it and add file extension
p2.file_name = filename # Update file_name field in the PhotoObject
testfile = request.URLopener() # start downloading
testfile.retrieve(p2.photo_url, photo_store_folder + filename) # Save
file_counter += 1

# if for some reason file does not get downloaded, warn and continue.
# Required for Hippo integration.
try:
# strip it and add file extension
p2.file_name = filename # Update file_name field in the PhotoObject
testfile = request.URLopener() # start downloading
testfile.retrieve(p2.photo_url, photo_store_folder + filename) # Save
file_counter += 1
except Exception as e:
logging.warn( "Failed to download %s due to %s" % (p2.photo_url,e))

return photos

Expand All @@ -195,8 +200,13 @@ def save_details(photos: list) -> None:
# information for further process.
all_info = {}
for photo in photos:
key = ''.join(random.choices(string.ascii_uppercase + string.digits,
k=10)) # Create some random key

# This is python3.6 specific. Making a python3.5 compatible version.
#key = ''.join(random.choices(string.ascii_uppercase + string.digits,
# k=10)) # Create some random key
alphas = string.ascii_uppercase + string.digits
key = ''.join( [ random.choice( alphas ) for i in range(10) ] )

# Create json entry
all_info[key] = {"uid": key,
"url": photo.url,
Expand All @@ -211,7 +221,6 @@ def save_details(photos: list) -> None:
# Dump to json
with open(output_file_name + ".json", "w") as f:
print(json.dumps(all_info), file=f)

logging.info( 'Wrote %s.json' % output_file_name )


Expand Down Expand Up @@ -273,14 +282,18 @@ def create_md_file(pics) -> None:
pass


def do_all(theme: str, last_day: str):
def do_all(theme: str, last_day: str, args):
"""
For lazy people. This function does entire process
:return: Download all photos with their information and details
"""
pics = retrieve_pics(get_all_info())
pics.sort(key=lambda x: x.points, reverse=True)
save_details(pics)
if args.task == 'json':
logging.warn( 'Task %s complete. Quitting' % args.task )
return

no_of_winners = max([2, round(len(pics) / 15)])
selected = pics[:no_of_winners]
last_entry_score = selected[-1].points
Expand Down