commit 73afaeae7ae0648620ae3573a0139036228ee184 Author: Wesley Kerfoot Date: Thu Dec 19 00:12:35 2019 -0500 initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..bbfc536 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.envrc +twit2blog diff --git a/README.me b/README.me new file mode 100644 index 0000000..0b5628f --- /dev/null +++ b/README.me @@ -0,0 +1,9 @@ +### Twit2Blog + +A simple tool to help make it easier to turn your tweet rants into real blog posts + +### Building +`nimble build` is all you'll need at this point. + +### Running +Requires `TWITTER_CONSUMER_KEY` and `TWITTER_CONSUMER_SECRET`, both of which you can only get if you have a registered developer account and an application created for twitter. diff --git a/config.nims b/config.nims new file mode 100644 index 0000000..de09037 --- /dev/null +++ b/config.nims @@ -0,0 +1 @@ +switch("define", "ssl") diff --git a/src/twit2blog.nim b/src/twit2blog.nim new file mode 100644 index 0000000..a4a060e --- /dev/null +++ b/src/twit2blog.nim @@ -0,0 +1,7 @@ +# This is just an example to get you started. A typical hybrid package +# uses this file as the main entry point of the application. + +import twit2blogpkg/twitter + +when isMainModule: + echo "weskerfoot".listTweets.repr diff --git a/src/twit2blogpkg/twitter.nim b/src/twit2blogpkg/twitter.nim new file mode 100644 index 0000000..b8c303a --- /dev/null +++ b/src/twit2blogpkg/twitter.nim @@ -0,0 +1,45 @@ +# This is just an example to get you started. Users of your hybrid library will +# import this file by writing ``import twit2blogpkg/submodule``. Feel free to rename or +# remove this file altogether. You may create additional modules alongside +# this file as required. + +import httpClient, base64, uri, json, os, strformat + +proc buildAuthHeader() : string = + let consumerKey = "TWITTER_CONSUMER_KEY".getEnv + let secret = "TWITTER_CONSUMER_SECRET".getEnv + "Basic " & (consumerKey.encodeUrl & ":" & secret.encodeUrl).encode + +proc getToken*() : string = + var client = newHttpClient() + client.headers = newHttpHeaders( + { + "Content-Type" : "application/x-www-form-urlencoded;charset=UTF-8", + "Authorization" : buildAuthHeader() + } + ) + + let body = "grant_type=client_credentials" + + let response = client.request("https://api.twitter.com/oauth2/token", + httpMethod = HttpPost, + body = body).body.parseJson + + let responseType = response["token_type"].getStr + + assert(responseType == "bearer") + + "Bearer " & response["access_token"].getStr + + +proc listTweets*(user : string) : JsonNode = + var client = newHttpClient() + let reqTarget = fmt"/1.1/statuses/user_timeline.json?count=100&screen_name={user}" + let url = fmt"https://api.twitter.com{reqTarget}" + client.headers = newHttpHeaders( + { + "Authorization" : getToken() + } + ) + + client.request(url, httpMethod = HttpGet).body.parseJson diff --git a/tests/config.nims b/tests/config.nims new file mode 100644 index 0000000..3bb69f8 --- /dev/null +++ b/tests/config.nims @@ -0,0 +1 @@ +switch("path", "$projectDir/../src") \ No newline at end of file diff --git a/tests/test1.nim b/tests/test1.nim new file mode 100644 index 0000000..afc1c73 --- /dev/null +++ b/tests/test1.nim @@ -0,0 +1,12 @@ +# This is just an example to get you started. You may wish to put all of your +# tests into a single file, or separate them into multiple `test1`, `test2` +# etc. files (better names are recommended, just make sure the name starts with +# the letter 't'). +# +# To run these tests, simply execute `nimble test`. + +import unittest + +import twit2blogpkg/twitter +#test "correct welcome": + #check getWelcomeMessage() == "Hello, World!" diff --git a/twit2blog.nimble b/twit2blog.nimble new file mode 100644 index 0000000..549fd97 --- /dev/null +++ b/twit2blog.nimble @@ -0,0 +1,15 @@ +# Package + +version = "0.1.0" +author = "Wesley Kerfoot" +description = "Turn Your Tweets Into Blog Posts" +license = "MIT" +srcDir = "src" +installExt = @["nim"] +bin = @["twit2blog"] + + + +# Dependencies + +requires "nim >= 1.0.9"