Skip to content

Commit

Permalink
Set up v1.1 POST media/upload and tweeting with media ids
Browse files Browse the repository at this point in the history
  • Loading branch information
aspiring-aster committed Jul 27, 2024
1 parent 88ab432 commit c1f5407
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 3 deletions.
5 changes: 3 additions & 2 deletions src/xnim.nim
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import xnim/utils/xapi,
xnim/v2/tweets
xnim/v2/tweets,
xnim/v1_1/media



export xapi, tweets
export xapi, tweets, media
35 changes: 35 additions & 0 deletions src/xnim/v1_1/media.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import std/[httpclient, strformat, json, times, mimetypes]
import ../utils/[xapi, oauth1]

const MEDIA_ENDPOINT*: string = "https://upload.twitter.com/1.1/media/upload.json"

proc PostMedia*(xAPI: XAPI, fileName: string): string =

var client: HttpClient = newHttpClient()
var oauthNonce: string = OauthNonce()
var timeStamp: int = toInt(epochTime())
var oauthSignature:string = OauthSignature(xApi, "POST", MEDIA_ENDPOINT, fileName, oauthNonce, timeStamp)
var AUTH_STRING: string =
&"OAuth oauth_consumer_key=\"{xAPI.consumerKey}\"," &
&"oauth_token=\"{xAPI.accessToken}\"," &
&"oauth_signature_method=\"HMAC-SHA1\"," &
&"oauth_timestamp=\"{timeStamp}\"," &
&"oauth_nonce=\"{oauthNonce}\"," &
&"oauth_version=\"1.0\"," &
&"oauth_signature=\"{oauthSignature}\""


client.headers = newHttpHeaders({"Content-Type": "multipart/form-data",
"authorization": AUTH_STRING})

var multipart = newMultipartData()
multipart.addFiles({"media": fileName})


var response: Response
try:
response = client.request(MEDIA_ENDPOINT,
httpMethod = HttpPost, multipart=multipart)
result = response.body
finally:
client.close()
37 changes: 36 additions & 1 deletion src/xnim/v2/tweets.nim
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import std/[httpclient, strformat, json, times]
import std/[httpclient, strformat, json, times, strutils]
import ../utils/[xapi, oauth1]

const TWEET_ENDPOINT*: string = "https://api.twitter.com/2/tweets"
Expand All @@ -23,6 +23,41 @@ proc PostTextTweet*(xAPI: XAPI, text: string): string =
"text": &"{text}"
}


var response: Response
try:
response = client.request(TWEET_ENDPOINT,
httpMethod = HttpPost, body = $body)
result = response.body
finally:
client.close()

proc PostTextTweet*(xAPI: XAPI, text: string, media_ids: seq[string]): string =

var client: HttpClient = newHttpClient()
var oauthNonce: string = OauthNonce()
var timeStamp: int = toInt(epochTime())
var oauthSignature:string = OauthSignature(xApi, "POST", TWEET_ENDPOINT, text, oauthNonce, timeStamp)
var AUTH_STRING: string =
&"OAuth oauth_consumer_key=\"{xAPI.consumerKey}\"," &
&"oauth_token=\"{xAPI.accessToken}\"," &
&"oauth_signature_method=\"HMAC-SHA1\"," &
&"oauth_timestamp=\"{timeStamp}\"," &
&"oauth_nonce=\"{oauthNonce}\"," &
&"oauth_version=\"1.0\"," &
&"oauth_signature=\"{oauthSignature}\""

client.headers = newHttpHeaders({"Content-Type": "application/json",
"authorization": AUTH_STRING})


let body = %*{
"text": &"{text}",
"media": {
"media_ids": media_ids
}
}

var response: Response
try:
response = client.request(TWEET_ENDPOINT,
Expand Down

0 comments on commit c1f5407

Please sign in to comment.