-
-
Save GitHub30/951849edff73ca10f185cd688a93e57d to your computer and use it in GitHub Desktop.
connpassのイベントの参加者の一覧を取得するAPI
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
from flask import jsonify | |
from requests_html import HTMLSession | |
def applicants(request): | |
"""Responds to any HTTP request. | |
Args: | |
request (flask.Request): HTTP request object. | |
Returns: | |
The response text or any set of values that can be turned into a | |
Response object using | |
`make_response <http://flask.pocoo.org/docs/1.0/api/#flask.Flask.make_response>`. | |
""" | |
url = request.path[1:] | |
url = url.replace('https:/', 'https://') | |
if re.match(r'^https://.*connpass\.com/event/\d+/$', url): | |
url += 'participation/' | |
print('url:', url) | |
table_area_names = [ | |
'lottery_table_area', | |
'participation_table_area', | |
'waitlist_table_area', | |
'cancelled_table_area' | |
] | |
applicants = {} | |
r = HTMLSession().get(url) | |
for table_area_name in table_area_names: | |
for table_area in r.html.xpath('//div[contains(@class, "%s")]' % table_area_name): | |
see_more_url = table_area.xpath('//tbody/tr[@class="empty"]//a/@href') | |
if see_more_url: | |
see_more_url = see_more_url[0] | |
while see_more_url: | |
ptype_r = HTMLSession().get(see_more_url) | |
for ptype_table_area in ptype_r.html.xpath('//div[contains(@class, "%s")]' % table_area_name): | |
for tr in ptype_table_area.xpath('//tbody/tr'): | |
label_ptype_name = tr.xpath('//span[contains(@class, "label_ptype_name")]/text()')[0] | |
if not label_ptype_name in applicants: | |
applicants[label_ptype_name] = [] | |
applicant = { | |
'label_status_tag': tr.xpath('//span[contains(@class, "label_status_tag")]/text()')[0].strip(), | |
'user_url': tr.xpath('//p[contains(@class, "display_name")]/a/@href')[0], | |
'display_name': tr.xpath('//p[contains(@class, "display_name")]/a/text()')[0] | |
} | |
twitter_url = tr.xpath('//a[starts-with(@href, "https://twitter.com/intent/user")]/@href') | |
if twitter_url: | |
applicant['twitter_url'] = twitter_url[0] | |
github_url = tr.xpath('//a[starts-with(@href, "https://github.com/")]/@href') | |
if github_url: | |
applicant['github_url'] = github_url[0] | |
applicants[label_ptype_name].append(applicant) | |
see_more_url = ptype_r.html.xpath('//a[text()="次へ>>"]/@href') | |
if see_more_url: | |
see_more_url = ptype_r.html.xpath('//a[text()="次へ>>"]')[0].absolute_links.pop() | |
continue | |
for tr in table_area.xpath('//tbody/tr'): | |
label_ptype_name = tr.xpath('//span[contains(@class, "label_ptype_name")]/text()')[0] | |
if not label_ptype_name in applicants: | |
applicants[label_ptype_name] = [] | |
applicant = { | |
'label_status_tag': tr.xpath('//span[contains(@class, "label_status_tag")]/text()')[0].strip(), | |
'user_url': tr.xpath('//p[contains(@class, "display_name")]/a/@href')[0], | |
'display_name': tr.xpath('//p[contains(@class, "display_name")]/a/text()')[0] | |
} | |
twitter_url = tr.xpath('//a[starts-with(@href, "https://twitter.com/intent/user")]/@href') | |
if twitter_url: | |
applicant['twitter_url'] = twitter_url[0] | |
github_url = tr.xpath('//a[starts-with(@href, "https://github.com/")]/@href') | |
if github_url: | |
applicant['github_url'] = github_url[0] | |
applicants[label_ptype_name].append(applicant) | |
print(applicants) | |
response = jsonify(applicants) | |
response.headers.set('Access-Control-Allow-Origin', '*') | |
return response |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Function dependencies, for example: | |
# package>=version | |
requests | |
requests-html |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment