Browse Source

initial commit

pull/1/head
Wesley Kerfoot 4 years ago
commit
73afaeae7a
  1. 2
      .gitignore
  2. 9
      README.me
  3. 1
      config.nims
  4. 7
      src/twit2blog.nim
  5. 45
      src/twit2blogpkg/twitter.nim
  6. 1
      tests/config.nims
  7. 12
      tests/test1.nim
  8. 15
      twit2blog.nimble

2
.gitignore

@ -0,0 +1,2 @@
.envrc
twit2blog

9
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.

1
config.nims

@ -0,0 +1 @@
switch("define", "ssl")

7
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

45
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

1
tests/config.nims

@ -0,0 +1 @@
switch("path", "$projectDir/../src")

12
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!"

15
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"
Loading…
Cancel
Save