Last active
September 15, 2017 04:10
-
-
Save XayOn/41ec5118aa369328839a to your computer and use it in GitHub Desktop.
Github trending twitter bot
This file contains hidden or 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
[github] | |
login= | |
password= | |
[twitter] | |
CONSUMER_KEY= | |
CONSUMER_SECRET= | |
ACCESS_TOKEN= | |
ACCESS_TOKEN_SECRET= |
This file contains hidden or 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
""" | |
Simple github trending twittbot. | |
""" | |
import os | |
import json | |
import github3 | |
import datetime | |
import ConfigParser | |
from birdy.twitter import UserClient | |
def main(): | |
""" | |
main | |
""" | |
cfg = ConfigParser.ConfigParser() | |
cfg.read(os.path.expanduser('~/.ghtrend_bot.cfg')) | |
ghub = github3.login( | |
cfg.get('github', 'login'), | |
cfg.get('github', 'password') | |
) | |
client = UserClient( | |
cfg.get('twitter', 'CONSUMER_KEY'), | |
cfg.get('twitter', 'CONSUMER_SECRET'), | |
cfg.get('twitter', 'ACCESS_TOKEN'), | |
cfg.get('twitter', 'ACCESS_TOKEN_SECRET') | |
) | |
def get_short_description(obj): | |
""" | |
Get desc in less than 100 characters. | |
TODO: Use twitter's current 144 - max_link_lenght | |
""" | |
desc = unicode(obj['description']).encode('utf-8', errors="replace") | |
return desc[:100] + (desc[100:] and '...') | |
def get_twitt(obj): | |
""" | |
Given a github search result object, get description and | |
url in a format acceptable to twitter | |
""" | |
return "{}: {}".format(get_short_description(obj), obj['url']) | |
def get_first_dow(): | |
""" | |
Returns the first day of current week | |
""" | |
time_ = datetime.datetime.now() | |
return time_ - datetime.timedelta(time_.weekday()) | |
def get_dow(): | |
time_ = datetime.datetime.now() | |
return time_.weekday() | |
weekdate = get_first_dow().strftime('%Y-%m-01') | |
gh_repos = ghub.search_repositories( | |
"created:>{}".format(weekdate), sort='stars', number=7) | |
repos = [repo for repo in gh_repos] | |
repo = repos[get_dow()] | |
twitt = get_twitt(json.loads(repo.as_json())) | |
client.api.statuses.update.post(status=twitt) | |
if __name__ == '__main__': | |
main() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment