<commit_before>import feedparser
import re

from background_task import background

from social.app.models.author import Author
from social.app.models.post import Post

# Get the GitHub activity of a user
# Reference source: https://pypi.python.org/pypi/django-background-tasks
@background(schedule=60)
def get_github_activity(authorId):
    gitAuthor = Author.objects.get(id=authorId)
    gitUrl = gitAuthor.github
    print("Entered")

    # Using RegEx to check if it's a proper URL
    # Reference source: 
    if((gitUrl[:19] == "https://github.com/")):
        data = feedparser.parse(gitUrl + ".atom")

        # get users post
        posts = Post.objects.filter(author__id=authorId).filter(title__contains="New GitHub Activity:")

        # Get encoding to decode the data
        encoding = data["encoding"]

        # Go over all the entries for the RSS feed, turn them into posts (if possible), save them
        for x in data.get("entries"):
            found = False
            gitId = x["id"].encode(encoding)
            gitTitle = x["title"].encode(encoding)
            publishDate = x["published"].encode(encoding)
            contentStr = "See [this page](%s)" %(x["link"].encode(encoding))

            # Create or update the post
            # Reference source: https://docs.djangoproject.com/en/1.10/ref/models/querysets/#update-or-create
            post, created = Post.objects.update_or_create(github_id=gitId,
                             defaults={"author": gitAuthor,
                                       "title": "New GitHub Activity",
                                       "github_id": gitId,
                                       "description": gitTitle,
                                       "content_type": "text/markdown",
                                       "content": contentStr,
                                       "published": publishDate},
                           )

        '''
            for post in posts:
               
                #entry = post.title.split()
                #if(entry[3] == gitId[1]):
                #    found = True
                #    break

            if(found is False):
                post = Post.objects.create(author=gitAuthor)

                post.title = "New GitHub Activity"
                post.githubId = gitId[1]
                # uses given title to describe what the user did
                post.description = x["title"].encode(encoding)
                post.content_type = "text/markdown"
                # gives a link to the page
                post.content = "See [this page](%s)" %(x["link"].encode(encoding))
                # use their given published date so that way it's properly sorted
                post.published = x["published"].encode(encoding)
                post.save()
        '''<commit_msg>David's recommendations implemented (still need to fix RegEx)<commit_after>import feedparser
import re

from background_task import background

from social.app.models.author import Author
from social.app.models.post import Post

# Get the GitHub activity of a user
# Reference source: https://pypi.python.org/pypi/django-background-tasks
@background(schedule=60)
def get_github_activity(authorId):
    gitAuthor = Author.objects.get(id=authorId)
    gitUrl = gitAuthor.github

    # Using RegEx to check if it's a proper URL
    # Reference source: https://github.com/lorey/social-media-profiles-regexs#github
    # TODO: Fix this so that it won't continue if given something like /user/repo
    if(r'http(s)?:\/\/(www\.)?github\.com/[A-z 0-9 _ -]+\/?$'):
        data = feedparser.parse(gitUrl + ".atom")

        # get users post
        posts = Post.objects.filter(author__id=authorId).filter(title__contains="New GitHub Activity:")

        # Get encoding to decode the data
        encoding = data["encoding"]

        # Go over all the entries for the RSS feed, turn them into posts (if possible), save them
        for x in data.get("entries"):
            gitId = x["id"].encode(encoding)
            gitTitle = x["title"].encode(encoding)
            contentStr = "See [this page](%s)" %(x["link"].encode(encoding))

            # Create or update the post
            # Reference source: https://docs.djangoproject.com/en/1.10/ref/models/querysets/#update-or-create
            post, created = Post.objects.update_or_create(github_id=gitId,
                             defaults={"author": gitAuthor,
                                       "title": "New GitHub Activity",
                                       "github_id": gitId,
                                       "description": gitTitle,
                                       "content_type": "text/markdown",
                                       "content": contentStr,
                                       "published": x["published"].encode(encoding)},
                           )
{'fraction_non_alphanumeric': 0.10835214446952596, 'fraction_numerical': 0.00348861071208701, 'mean_word_length': 1.786735277301315, 'pattern_counts': {'":': 14, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 6, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5474589', 'n_tokens_mistral': 1291, 'n_tokens_neox': 1212, 'n_words': 328}