Comparing version 0.1.2 to 0.1.3
36
index.js
@@ -42,2 +42,3 @@ /*jshint esversion:6, node:true */ | ||
if(!Array.isArray(thisPage)){ | ||
reporter(); | ||
return thisPage; | ||
@@ -201,3 +202,3 @@ } | ||
registerFunction('follow',function(wrapped,id){ | ||
return getIDs(id,wrapped).map((item)=>this.twitter( | ||
return Promise.all(getIDs(id,wrapped).map((item)=>this.twitter( | ||
'friendships/create','this',{ | ||
@@ -207,3 +208,3 @@ screen_name:isTwitterId(item)? undefined : item, | ||
method:'post' | ||
},1,1)); | ||
},1,1))); | ||
}); | ||
@@ -220,3 +221,3 @@ | ||
let ids = getIDs(id,wrapped); | ||
return ids.map((item)=>this.twitter( | ||
return Promise.all(ids.map((item)=>this.twitter( | ||
'friendships/destroy','this',{ | ||
@@ -226,3 +227,3 @@ screen_name:isTwitterId(item)? undefined : item, | ||
method:'post' | ||
},1,1)); | ||
},1,1))); | ||
}); | ||
@@ -251,13 +252,30 @@ | ||
registerFunction('twitterUser',function(wrapped,screenName){ | ||
//TODO handle situations where the number of input screen names or ids exceed 100 | ||
let screenNames = []; | ||
let ids = []; | ||
if(screenName){ | ||
screenNames.push(screenName); | ||
}else if(!Array.isArray(wrapped)){ | ||
screenNames = typeof wrapped === 'object'? [wrapped.screen_name] :[wrapped]; | ||
if(isTwitterId(screenName)){ | ||
ids.push(screenName); | ||
}else{ | ||
screenNames.push(screenName); | ||
} | ||
}else{ | ||
screenNames = wrapped.map((user)=>typeof user === 'string'? user : user.screen_name); | ||
wrapped = Array.isArray(wrapped)?wrapped : [wrapped]; | ||
console.assert(wrapped.length <= 100,'twitterUser function cannot handle lists longer than 100 items'); | ||
wrapped.forEach((item)=>{ | ||
let name = typeof item === 'string'? item : (item.screen_name || item.id_str); | ||
if(isTwitterId(name)){ | ||
ids.push(name); | ||
}else{ | ||
screenNames.push(name); | ||
} | ||
}); | ||
} | ||
return this.twitter( | ||
'users/lookup','this', | ||
{screen_name:screenNames.join()},1,1 | ||
{ | ||
method:'post', | ||
screen_name:screenNames.join(), | ||
user_id:ids.join() | ||
},1,1 | ||
); | ||
@@ -264,0 +282,0 @@ }); |
{ | ||
"name": "dstwitter", | ||
"version": "0.1.2", | ||
"version": "0.1.3", | ||
"description": "Simplify usage of twitter API", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -5,4 +5,6 @@ dstwitter - Easy use of twitter API | ||
You will need access token key, access token secret, consumer key and consumer token to run the code. | ||
An important feature is its ability to break single requests with many required items into many API calls and when reached twitter API quota, wait for 15 minutes until the API is open again. For example, if you would like to search for 10,000 tweets about javascript, a single API call will not do. The twitter API limits each request to 100 tweets and there can only be 180 API calls in a 15 minute window. This package handles paging and waits when reached the 15 minute window quota. | ||
You will need access token key, access token secret, consumer key and consumer token to run the code. | ||
## Examples | ||
@@ -12,3 +14,3 @@ ### Initialize the environment | ||
const ME = 'MY_SCREEN_NAME'; | ||
require('dstwitter');//initialize twitter functions | ||
require('dstwitter'); //initialize twitter functions | ||
start = require('dstools').Collection | ||
@@ -24,11 +26,15 @@ .context('twitter',{ //context function sets the context twitter credentials | ||
```javascript | ||
//get followers and following IDs | ||
let followers = start.followersIDs(ME); | ||
let following = start.followingIDs(ME); | ||
//dot is at the end of the line so pasting into REPL will work | ||
let followers, following; | ||
start.followersIDs(ME).//load followers | ||
do((output)=>followers=output).//set followers variable | ||
followingIDs(ME).//load following | ||
do((output)=>following=output).//set following variable | ||
do(()=>{ | ||
console.log('followers:',followers.length); | ||
console.log('I follow:',following.length); | ||
console.log("Follow me that I don't follow back:", Collection(followers).drop(following).count());//use Collection function to create dstools collection and use its functions such as `drop` | ||
console.log("Don't follow me back:", Collection(following).drop(followers).count()); | ||
}); | ||
//some stats | ||
console.log('followers:',followers.count()); | ||
console.log('I follow:',following.count()); | ||
console.log("Follow me that I don't follow back:", followers.drop(following).count()); | ||
console.log("Don't follow me back:", following.drop(followers).count()); | ||
``` | ||
@@ -38,19 +44,19 @@ ### Follow back all users that follow me | ||
//follow users that follow me (that I don't already follow) | ||
followers | ||
.drop(following) //ignore users I am already following | ||
.follow(); //follow them | ||
start.collection(followers). //wrap in collection. Need to start with `start` because `start` has the context twitter credentials | ||
drop(following). //ignore users I am already following | ||
follow(); //follow them | ||
### create a word cloud of user tweets | ||
start.tweets('SOME_SCREEN_NAME',10000) | ||
.column('text').toLowerCase() | ||
.terms().dropStopwords('term') | ||
.sortDesc('count').head(50).wordCloud('term','count').save('word-cloud.html'); | ||
start.tweets('JavaScriptDaily',10000). | ||
column('text').toLowerCase(). | ||
terms().dropStopwords('term'). | ||
sortDesc('count').head(50).wordCloud('term','count').save('word-cloud.html'); | ||
``` | ||
### Find users tweeting about javascript and react | ||
```javascript | ||
start.searchTweets('javascript react',10000,{result_type:'mixed'}) | ||
.filter((tweet)=>!tweet.text.startsWith('RT @'))//filter out retweets | ||
.map((tweet)=>tweet.user)//get users of the tweets | ||
.column('screen_name')//get their screen name | ||
.do((names)=>console.log('screen names',names.join()));//print names after finish loading tweets | ||
start.searchTweets('javascript react',10000,{result_type:'mixed'}). | ||
filter((tweet)=>!tweet.text.startsWith('RT @')). //filter out retweets | ||
map((tweet)=>tweet.user). //get users of the tweets | ||
column('screen_name'). //get their screen name | ||
do((names)=>console.log('screen names',names.join())); //print names after finish loading tweets | ||
``` | ||
@@ -57,0 +63,0 @@ ## Installing dstwitter |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
16891
5
335
69
0