Skip to content

Instantly share code, notes, and snippets.

@wcaleb
Last active August 29, 2015 14:21
Show Gist options
  • Select an option

  • Save wcaleb/60d52759f6d0c5bc270d to your computer and use it in GitHub Desktop.

Select an option

Save wcaleb/60d52759f6d0c5bc270d to your computer and use it in GitHub Desktop.

Revisions

  1. wcaleb revised this gist May 13, 2015. 1 changed file with 15 additions and 7 deletions.
    22 changes: 15 additions & 7 deletions wikitweet.py
    Original file line number Diff line number Diff line change
    @@ -19,31 +19,39 @@
    api_secret = wcaleb['api_secret']
    access_token = wcaleb['access_token']
    access_token_secret = wcaleb['access_token_secret']
    base_url = 'http://wiki.wcaleb.rice.edu/'

    repo = git.Repo('/home/wcm1/wiki/wikidata')

    most_recent_diff = repo.head.commit.diff('HEAD~1')
    changed_files = []

    # This bit of black magic comes from http://www.masnun.com/2012/01/28/fetching-changed-files-diff-between-two-git-commits-in-python.html
    for x in most_recent_diff:
    if x.a_blob.path not in changed_files:
    changed_files.append(x.a_blob.path)
    if x.b_blob is not None and x.b_blob.path not in changed_files:
    changed_files.append(x.b_blob.path)

    # Filter out changed discussion pages
    def isNotDiscuss(file):
    return file[0] != '@'
    filter_files = [os.path.splitext(n)[0] for n in filter(isNotDiscuss, changed_files)]

    if len(filter_files) == 1:
    pageUrl = urllib.pathname2url(filter_files[0])
    url = base_url + '_diff/' + pageUrl + '?to=' + repo.head.commit.hexsha
    else:
    url = base_url + '_activity'

    file_names = ', '.join([os.path.splitext(n)[0] for n in filter(isNotDiscuss, changed_files)])
    file_names = ', '.join(filter_files)

    if len(file_names) < 80:
    tweet = 'Updated notes on ' + file_names + ' in my research wiki. http://wiki.wcaleb.rice.edu/_activity'
    tweet = 'Updated notes on ' + file_names + ' in my research wiki. ' + url
    else:
    tweet = 'Updated notes in my research wiki. http://wiki.wcaleb.rice.edu/_activity'

    tweet = 'Updated notes in my research wiki. ' + url
    try:
    api = twitter.Api(consumer_key = api_key, consumer_secret = api_secret, access_token_key = access_token, access_token_secret = access_token_secret)
    status = api.PostUpdate(tweet)
    except twitter.TwitterError:
    except twitter.TwitterError, error:
    print(error.message)
    exit()
  2. wcaleb revised this gist May 13, 2015. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions wikitweet.py
    Original file line number Diff line number Diff line change
    @@ -24,6 +24,8 @@

    most_recent_diff = repo.head.commit.diff('HEAD~1')
    changed_files = []

    # This bit of black magic comes from http://www.masnun.com/2012/01/28/fetching-changed-files-diff-between-two-git-commits-in-python.html
    for x in most_recent_diff:
    if x.a_blob.path not in changed_files:
    changed_files.append(x.a_blob.path)
  3. wcaleb created this gist May 13, 2015.
    47 changes: 47 additions & 0 deletions wikitweet.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,47 @@
    #!/usr/bin/python

    # I run a wiki on Gitit at http://wiki.wcaleb.rice.edu. It's usually
    # updated by pushing to the wiki's remote git repo from my local machine.
    # I have a `post-receive` hook in that repo that runs this script, like so:
    #
    # unset GIT_DIR
    # python /path/to/wikitweet.py
    #
    # Now, whenever I push to the repo, the script looks to see which files have
    # been changed and tweets about the update to https://twitter.com/wcaleb

    import os
    import twitter # https://github.com/bear/python-twitter
    import git # http://gitpython.readthedocs.org/en/stable/index.html
    from OAuthSettings import wcaleb

    api_key = wcaleb['api_key']
    api_secret = wcaleb['api_secret']
    access_token = wcaleb['access_token']
    access_token_secret = wcaleb['access_token_secret']

    repo = git.Repo('/home/wcm1/wiki/wikidata')

    most_recent_diff = repo.head.commit.diff('HEAD~1')
    changed_files = []
    for x in most_recent_diff:
    if x.a_blob.path not in changed_files:
    changed_files.append(x.a_blob.path)
    if x.b_blob is not None and x.b_blob.path not in changed_files:
    changed_files.append(x.b_blob.path)

    def isNotDiscuss(file):
    return file[0] != '@'

    file_names = ', '.join([os.path.splitext(n)[0] for n in filter(isNotDiscuss, changed_files)])

    if len(file_names) < 80:
    tweet = 'Updated notes on ' + file_names + ' in my research wiki. http://wiki.wcaleb.rice.edu/_activity'
    else:
    tweet = 'Updated notes in my research wiki. http://wiki.wcaleb.rice.edu/_activity'

    try:
    api = twitter.Api(consumer_key = api_key, consumer_secret = api_secret, access_token_key = access_token, access_token_secret = access_token_secret)
    status = api.PostUpdate(tweet)
    except twitter.TwitterError:
    exit()