def get_contestdata(siteinfo): start = dtwrapper.now() duration = datetime.timedelta(days=1) dt_from = start.isoformat(timespec='microseconds') dt_to = (start+duration).isoformat() service = api_auth() events_data = service.events().list( calendarId=siteinfo.googlecal_id, timeMin=dt_from, timeMax=dt_to, maxResults=10, singleEvents=True, orderBy="startTime" ).execute() events = events_data.get('items', []) contests = [] for event in events: try: contest_name = event["summary"] contest_begin = parse(event["start"]["dateTime"]).astimezone(pytz.timezone('Asia/Tokyo')) contest_end = parse(event["end"]["dateTime"]).astimezone(pytz.timezone('Asia/Tokyo')) contest_duration = contest_end - contest_begin contest = ContestData(contest_name, siteinfo, contest_begin, contest_duration) except KeyError: continue else: if(contest.is_valid(dtwrapper.now())): contests.append(contest) return contests
def lambda_handler(event, context): _now = datetime.datetime.now().astimezone(pytz.timezone("Asia/Tokyo")) dtwrapper.now(_now.replace(hour=8, minute=0, second=0, microsecond=0)) contest_sites = (AtCoder(), TopCoder(), Codeforces(), CSAcademy(), yukicoder()) contests = [] for site in contest_sites: contests.extend(site.get_contestdata()) contests.sort(key=lambda x: x.begin, reverse=True) twitterutil.tweet(contests)
def lambda_handler(event, context): event_arn = event["resources"][0] print("Event AWS Resource Name(ARN): " + event_arn) now = datetime.datetime.now().astimezone(pytz.timezone("Asia/Tokyo")) dtwrapper.now(now.replace(hour=8, minute=0, second=0, microsecond=0)) contest_sites = (AtCoder(), TopCoder(), Codeforces(), CSAcademy(), yukicoder(), LeetCode(), GoogleCodeJam()) contests = [] for site in contest_sites: contests.extend(site.get_contestdata()) contests.sort(key=lambda x: x.begin, reverse=True) pprint.pprint(contests) if (event_arn == os.environ["AWS_EVENT_ARN"]): twitterutil.tweet(contests)
def contests2tweets(contests): strings = contests2tweetformat(contests) if(len(strings) == 0): return ["本日" + dtwrapper.now().strftime("%m/%d") + "はコンテストがありません."] tweets_num = (len(strings)+1)//2 tweet_title = dtwrapper.now().strftime("%m/%d") + "のコンテスト予定" tweets = [] for i in range(tweets_num): tweet = tweet_title + ("\n" if(tweets_num == 1) else "("+str(i+1)+")\n") # if tweets are 2 or more, insert tweet number for j in range(2): try: tweet += strings.pop() + "\n" except IndexError: pass tweets.append(tweet) return tweets
def contests2tweetformat(contests): strings = [] NAME_LENGTH = (129 - 9 * 2) // 2 for contest in contests: if (len(contest.name) > NAME_LENGTH): contest.name = contest.name[:(NAME_LENGTH - 1)] + "…" is_nextday = "翌" if ( contest.begin.date() > dtwrapper.now().date()) else "" string = "[%s]%s %s" % (contest.siteinfo.acronym, is_nextday + contest.begin.strftime("%H:%M"), contest.name) strings.append(string) return strings
def get_contestdata(self): html = urlopen("https://beta.atcoder.jp/contests/") bs_obj = BeautifulSoup(html, "html.parser") table = bs_obj.find_all("table", class_="table table-default table-striped table-hover table-condensed table-bordered small")[1] rows = table.find_all("tr") del rows[0] contests = [] for row in rows: # contest_data = [start, contestname, duration, rated] contest_data = [cell.get_text() for cell in row.find_all("td")] contest_name = contest_data[1] contest_begin = parse(contest_data[0]).astimezone(pytz.timezone('Asia/Tokyo')) contest_duration = dtwrapper.str2timedelta(contest_data[2]) contest = ContestData(contest_name, self.siteinfo, contest_begin, contest_duration) if(not contest.is_valid(dtwrapper.now())): continue contests.append(contest) return contests