Пример #1
0
def get_github_activity(author_id):
    author = get_object_or_404(Author, id=author_id)
    # If the author has set a github profile URL
    if author.github:
        githubUsername = author.github.rsplit('/', 1)[-1]
        url = f"https://api.github.com/users/{githubUsername}/received_events/public"
        try:
            response = requests.get(url,
                                    headers={
                                        'content-type': 'application/json',
                                        'Accept': 'application/json'
                                    },
                                    timeout=settings.GLOBAL_TIMEOUT)
        except Timeout:
            return
        if response.status_code == 200:
            response_json = response.json()
            for event in response_json:
                # Set the origin to include the event's github id, so we can
                # check if we've seen it before.
                origin = url + "/" + str(event["id"])
                if len(Post.objects.filter(origin=origin)) == 1:
                    # already seen this event
                    continue
                try:
                    # Split up the event type with spaces.
                    title = "Github " + \
                        re.sub(r"([A-Z])", r" \1", event["type"])
                    content = parse_github_event(event)
                    published = datetime.datetime.strptime(
                        event["created_at"], '%Y-%m-%dT%H:%M:%SZ')
                    post = Post(title=title,
                                description=event["type"],
                                contentType='text/markdown',
                                content=content,
                                author=author,
                                categories='github',
                                published=published,
                                visibility='PRIVATE')
                    post.origin = origin
                    post.source = settings.FORMATTED_HOST_NAME + \
                        'posts/' + str(post.id)
                    post.save()
                except:
                    print("Failed to import github event!")