From fd7e33023d0ed22ca14e321b492363a64cc4881c Mon Sep 17 00:00:00 2001 From: Cameron Rodriguez <rod.cam2014+dev@gmail.com> Date: Sun, 18 Nov 2018 22:52:46 -0500 Subject: [PATCH] added Pipfile, OAuth Added dependencies to Pipfile. Created Twitter class (for API) and OAuth header creation function. --- Pipfile | 14 +++++++++ Pipfile.lock | 85 ++++++++++++++++++++++++++++++++++++++++++++++++++++ xkcd_alt.py | 41 ++++++++++++++++++++++++- 3 files changed, 139 insertions(+), 1 deletion(-) create mode 100644 Pipfile create mode 100644 Pipfile.lock diff --git a/Pipfile b/Pipfile new file mode 100644 index 0000000..2bfbf3f --- /dev/null +++ b/Pipfile @@ -0,0 +1,14 @@ +[[source]] +name = "pypi" +url = "https://pypi.org/simple" +verify_ssl = true + +[dev-packages] + +[packages] +requests = "*" +requests-oauthlib = "*" +bs4 = "*" + +[requires] +python_version = "3.7" diff --git a/Pipfile.lock b/Pipfile.lock new file mode 100644 index 0000000..c11b8cf --- /dev/null +++ b/Pipfile.lock @@ -0,0 +1,85 @@ +{ + "_meta": { + "hash": { + "sha256": "f01c1f197a84635e3a6149fc6bcc62f13d447978f2d9bb7d0cdcab2c86b8ed51" + }, + "pipfile-spec": 6, + "requires": { + "python_version": "3.7" + }, + "sources": [ + { + "name": "pypi", + "url": "https://pypi.org/simple", + "verify_ssl": true + } + ] + }, + "default": { + "beautifulsoup4": { + "hashes": [ + "sha256:194ec62a25438adcb3fdb06378b26559eda1ea8a747367d34c33cef9c7f48d57" + ], + "version": "==4.6.3" + }, + "bs4": { + "hashes": [ + "sha256:36ecea1fd7cc5c0c6e4a1ff075df26d50da647b75376626cc186e2212886dd3a" + ], + "index": "pypi", + "version": "==0.0.1" + }, + "certifi": { + "hashes": [ + "sha256:339dc09518b07e2fa7eda5450740925974815557727d6bd35d319c1524a04a4c", + "sha256:6d58c986d22b038c8c0df30d639f23a3e6d172a05c3583e766f4c0b785c0986a" + ], + "version": "==2018.10.15" + }, + "chardet": { + "hashes": [ + "sha256:84ab92ed1c4d4f16916e05906b6b75a6c0fb5db821cc65e70cbd64a3e2a5eaae", + "sha256:fc323ffcaeaed0e0a02bf4d117757b98aed530d9ed4531e3e15460124c106691" + ], + "version": "==3.0.4" + }, + "idna": { + "hashes": [ + "sha256:156a6814fb5ac1fc6850fb002e0852d56c0c8d2531923a51032d1b70760e186e", + "sha256:684a38a6f903c1d71d6d5fac066b58d7768af4de2b832e426ec79c30daa94a16" + ], + "version": "==2.7" + }, + "oauthlib": { + "hashes": [ + "sha256:ac35665a61c1685c56336bda97d5eefa246f1202618a1d6f34fccb1bdd404162", + "sha256:d883b36b21a6ad813953803edfa563b1b579d79ca758fe950d1bc9e8b326025b" + ], + "version": "==2.1.0" + }, + "requests": { + "hashes": [ + "sha256:65b3a120e4329e33c9889db89c80976c5272f56ea92d3e74da8a463992e3ff54", + "sha256:ea881206e59f41dbd0bd445437d792e43906703fff75ca8ff43ccdb11f33f263" + ], + "index": "pypi", + "version": "==2.20.1" + }, + "requests-oauthlib": { + "hashes": [ + "sha256:8886bfec5ad7afb391ed5443b1f697c6f4ae98d0e5620839d8b4499c032ada3f", + "sha256:e21232e2465808c0e892e0e4dbb8c2faafec16ac6dc067dd546e9b466f3deac8" + ], + "index": "pypi", + "version": "==1.0.0" + }, + "urllib3": { + "hashes": [ + "sha256:61bf29cada3fc2fbefad4fdf059ea4bd1b4a86d2b6d15e1c7c0b582b9752fe39", + "sha256:de9529817c93f27c8ccbfead6985011db27bd0ddfcdb2d86f3f663385c6a9c22" + ], + "version": "==1.24.1" + } + }, + "develop": {} +} diff --git a/xkcd_alt.py b/xkcd_alt.py index b38e6b7..bec06d4 100644 --- a/xkcd_alt.py +++ b/xkcd_alt.py @@ -1,4 +1,43 @@ """This is the XKCD Alt Text Bot. This bot checks once a minute for new Tweets from @xkcdComic. If one is found, it accesses the -linked comic, extracts the image alt text, and Tweets it as a reply.""" \ No newline at end of file +linked comic, extracts the image alt text, and Tweets it as a reply.""" + +import time # Program sleeping +import os # API keys & access tokens +import requests # Accessing API +from requests_oauthlib import OAuth1 # OAuth +from bs4 import BeautifulSoup # HTML searching + +class Twitter(): + """This class handles all API requests to Twitter.""" + def __init__(self, auth): + """This class constructor collects the OAuth keys for the class.""" + self.auth = auth + + def get(self): + """This function returns the result of the Twitter search for the reply to @xkcdComic.""" + pass + + def post(self, tweet): + """This function Tweets the alt (title) text as a reply to @xkcdComic.""" + pass + +def get_auth(): + """This function retrieves the API keys and access tokens from environmental variables.""" + key = [os.environ.get('XKCD_API_KEY', None), + os.environ.get('XKCD_API_SECRET_KEY', None), + os.environ.get('XKCD_ACCESS_TOKEN', None), + os.environ.get('XKCD_ACCESS_SECRET_TOKEN', None)] + for i in key: + if i is None: # Verify keys were loaded + print("OAuth initiation failed: Environmental variable not found") + auth = 'crash' + if key != 'crash': + auth = OAuth1(key[0], key[1], key[2], key[3]) + print('OAuth initiation successful!') + del key + return auth + else: + del key + return auth \ No newline at end of file -- GitLab