def birthday_to_date(self, birthday): now = get_today() year = now.year if now.month == 12 and birthday[2] == 1: year = year + 1 return datetime.strptime("{} {} {}".format(birthday[2], birthday[3], year), "%m %d %Y").date()
def election_closed(self): from standrew.utils import get_today now = get_today() today = now.date() if self.movie_date < today: return True if self.movie_date > today: return False return now.hour >= 12
def date_for_subject(date): now = get_today().date() try: date = date.date() except: pass if now == date: return "Today" week_from_now = now + timedelta(days=6) weekday = date.strftime("%a") day = date.strftime("%-d") ordinal = get_ordinal(int(day)) if date > week_from_now: return "{} {}".format(weekday, ordinal) return "{}".format(weekday)
def data(self): date = get_today() SPREADSHEET_ID = "1BpaVvpi66UKojz9SnO41iLKfVTggWq2jI_ReniyCeH4" RANGE_NAME = "A2:K35" service = build("sheets", "v4", developerKey=GOOGLE_API_KEY) sheet = service.spreadsheets() try: result = sheet.values().get(spreadsheetId=SPREADSHEET_ID, range=RANGE_NAME).execute() except HttpError as e: print(e) return return [ self.decorate_birthday(row) for row in result["values"] if row[2] and row[3] and int(row[2]) == date.month and int(row[3]) == date.day ]
def data(self): date = get_today() result = requests.get( "http://api.dailyoffice2019.com/api/v1/calendar/{}-{}-{}".format(date.year, date.month, date.day) ) if result.status_code != 200: print("ERROR") return content = result.json() major_feasts = [ feast for feast in content["commemorations"] if int(feast["rank"]["precedence"]) <= 4 and feast["rank"]["name"] != "SUNDAY" ] return { "day": content, "feasts": major_feasts, "today": timezone.now(), }
def full_date_range(self): now = get_today() return [(now + timedelta(days=x)).date() for x in range(9)]
def __init__(self): self.today = get_today().date()
def get_movie_details(imdb_id): def standardized_fields(context): def title(): return get_movie_details_from_dict( context, [["omdb", "Title"], ["imdb", "title"], ["utelly", "collection", "name"]]) def year(): return get_movie_details_from_dict( context, [["omdb", "Year"], ["imdb", "year"]]) def rating(): return get_movie_details_from_dict( context, [["omdb", "Rated"], ["imdb", "rated"]]) def genre(): value = get_movie_details_from_dict( context, [["imdb", "genres"], ["omdb", "Genre"]]) if type(value) is list: value = ", ".join(value) return value def runtime(): value = get_movie_details_from_dict( context, [["omdb", "Runtime"], ["imdb", "runtime"]]) try: value = int(value.replace("min", "")) hours = int(value / 60) minutes = value % 60 if hours: return "{} hours {} minutes".format(hours, minutes) return "{} minutes".format(minutes) except Exception: return value def poster(): return get_movie_details_from_dict( context, [["omdb", "Poster"], ["utelly", "collection", "picture"]]) def plot(): return get_movie_details_from_dict( context, [["omdb", "Plot"], ["imdb", "description"]]) def trailer(): return get_movie_details_from_dict( context, [["imdb", "youtube_trailer_key"], ["youtube"]]) def services(): locations = get_movie_details_from_dict( context, [["utelly", "collection", "locations"]]) if locations: locations = [ location for location in locations if location["name"] in ( "AmazonPrimeVideoIVAUS", "NetflixIVAUS", "DisneyPlusIVAUS", "HuluIVAUS", "AppleTvPlusIVAUS", "HBOMaxIVAUS", ) ] return locations return None def ratings(): value = get_movie_details_from_dict( context, [["omdb", "Ratings"], ["imdb", "imdb_rating"]]) if value: if type(value) is list: for i, rating in enumerate(value): if rating["Source"] == "Internet Movie Database": value[i]["Source"] = "IMDb" else: value = [ { "Value": "{}/10".format(value), "Source": "IMDb" }, ] return value return { "title": title(), "year": year(), "rating": rating(), "genre": genre(), "runtime": runtime(), "poster": poster(), "plot": plot(), "trailer": trailer(), "services": services(), "ratings": ratings(), "imdb_id": imdb_id, } @lru_cache(maxsize=None) def omdb(imdb_id): try: result = requests.get( "http://www.omdbapi.com/?apikey={}&plot=full&i={}".format( OMDB_API_KEY, imdb_id)) content = result.content.decode("utf-8") return json.loads(content) except: return {"imdbID": imdb_id, "error": True} @lru_cache(maxsize=None) def utelly(imdb_id): try: url = "https://utelly-tv-shows-and-movies-availability-v1.p.rapidapi.com/idlookup" querystring = { "source_id": imdb_id, "source": "imdb", "country": "us" } headers = { "x-rapidapi-key": UTELLY_API_KEY, "x-rapidapi-host": "utelly-tv-shows-and-movies-availability-v1.p.rapidapi.com", } result = requests.request("GET", url, headers=headers, params=querystring) content = result.content.decode("utf-8") content = json.loads(content) return content except: return [] @lru_cache(maxsize=None) def imdb(imdb_id): url = "https://movies-tvshows-data-imdb.p.rapidapi.com/" querystring = {"type": "get-movie-details", "imdb": imdb_id} headers = { "x-rapidapi-key": IMDB_API_KEY, "x-rapidapi-host": "movies-tvshows-data-imdb.p.rapidapi.com", } result = requests.request("GET", url, headers=headers, params=querystring) content = result.content.decode("utf-8") content = json.loads(content) return content @lru_cache(maxsize=None) def youtube(imdb_id): try: params = { "regionCode": "US", "relevanceLanguage": "en", "type": "video", "part": "snippet", "q": "{} trailer".format(omdb(imdb_id)["Title"]), "topicId": "/m/02vxn", "key": YOUTUBE_API_KEY, } url = "https://www.googleapis.com/youtube/v3/search" result = requests.request("GET", url, params=params) content = result.content.decode("utf-8") content = json.loads(content) return content["items"][0]["id"]["videoId"] except Exception as e: print(e) return "" try: today = get_today() one_week_ago = today - timedelta(days=7) existing_record = MovieDetails.objects.get(imdb_id=imdb_id, updated__gte=one_week_ago) context = existing_record.movie_details except MovieDetails.DoesNotExist: context = { "omdb": omdb(imdb_id), "utelly": utelly(imdb_id), "youtube": youtube(imdb_id), "imdb": imdb(imdb_id), } context["fields"] = standardized_fields(context) details = MovieDetails.objects.get_or_create(imdb_id=imdb_id)[0] details.movie_details = context details.save() return context