Twitter4j issues with user's protected data - java

I am using twitter java api tweet4j in order to retrieve data from twitter users. I ve got a list of users ids from which I want to retrieve data. I read a list of string ids and I am trying to extract data via twitter java api into my database. However, some users has protection on their data. Thus, I am getting authentication errors, in some ids. Since my list is enormous, how is it possible to automatically create exceptions from user with no authentication right? Is there any method in twitter4j for protected or not protected users?
The error that I am getting:
Exception in thread "main" 401:Authentication credentials
(https://dev.twitter.com/pages/auth) were missing or incorrect. Ensure that you have
set valid consumer key/secret, access token/secret, and the system clock is in sync.
[Wed Apr 30 15:35:18 EEST 2014]date: Wed, 30 Apr 2014 12:36:58 GMT
[Wed Apr 30 15:35:18 EEST 2014]x-transaction: 0d6f0c50c647c52e
[Wed Apr 30 15:35:18 EEST 2014]pragma: no-cache
[Wed Apr 30 15:35:18 EEST 2014]cache-control: no-cache, no-store, must-revalidate, pre-
check=0, post-check=0
{"request":"\/1.1\/statuses\/user_timeline.json?
user_id=787323013&include_my_retweet=true&include_entities=true","error":"Not
authorized."}
[Wed Apr 30 15:35:18 EEST 2014]x-content-type-options: nosniff
[Wed Apr 30 15:35:18 EEST 2014]x-xss-protection: 1; mode=block
[Wed Apr 30 15:35:18 EEST 2014]x-rate-limit-limit: 180
Relevant discussions can be found on the Internet at:
[Wed Apr 30 15:35:18 EEST 2014]expires: Tue, 31 Mar 1981 05:00:00 GMT
http://www.google.co.jp/search?q=0742d262 or
http://www.google.co.jp/search?q=32729de5
[Wed Apr 30 15:35:18 EEST 2014]set-cookie: guest_id=v1%3A139886141837883092;
Domain=.twitter.com; Path=/; Expires=Fri, 29-Apr-2016 12:36:58 UTC
[Wed Apr 30 15:35:18 EEST 2014]set-cookie: lang=en
[Wed Apr 30 15:35:18 EEST 2014]www-authenticate: OAuth realm="https://api.twitter.com"
[Wed Apr 30 15:35:18 EEST 2014]content-length: 143
[Wed Apr 30 15:35:18 EEST 2014]x-rate-limit-reset: 1398862209
[Wed Apr 30 15:35:18 EEST 2014]server: tfe
[Wed Apr 30 15:35:18 EEST 2014]strict-transport-security: max-age=631138519
[Wed Apr 30 15:35:18 EEST 2014]x-access-level: read-write-directmessages
[Wed Apr 30 15:35:18 EEST 2014]{"request":"\/1.1\/statuses\/user_timeline.json?
user_id=787323013&include_my_retweet=true&include_entities=true","error":"Not
authorized."}
TwitterException{exceptionCode=[0742d262-32729de5], statusCode=401, message=null,
code=-1, retryAfter=-1, rateLimitStatus=RateLimitStatusJSONImpl{remaining=114,
limit=180, resetTimeInSeconds=1398862209, secondsUntilReset=890}, version=4.0.1}
at twitter4j.HttpClientImpl.handleRequest(HttpClientImpl.java:164)
at twitter4j.HttpClientBase.request(HttpClientBase.java:53)
at twitter4j.HttpClientBase.get(HttpClientBase.java:71)
at twitter4j.TwitterImpl.get(TwitterImpl.java:1968)
at twitter4j.TwitterImpl.getUserTimeline(TwitterImpl.java:156)
at twitter4j.TwitterImpl.getUserTimeline(TwitterImpl.java:177)
at twitter4j.examples.tweets.UpdateStatus.main(UpdateStatus.java:116)

You should catch the exception and continue processing, something like this:
for (long userId : userIds) { // presumably you have a list of users
try {
// call twitter4j here and process results
}
catch (TwitterException e) {
// do not throw if user has protected tweets, or if they deleted their account
if (e.getStatusCode() == HttpResponseCode.UNAUTHORIZED ||
e.getStatusCode() == HttpResponseCode.NOT_FOUND) {
// log something here
}
else {
throw e;
}
}
}

Related

Spring Crontab pattern: every weekday at specific times

Here is the documentation for the Spring Crontab syntax.
I want to run a task every weekday at 14:40, 14:45, 14:50, 14:55, and 15:00, but I can't figure out how to express this with a Crontab pattern. The closest I've come up with so far is:
0 40/5 14 * * MON-FRI
But this doesn't run at 15:00.
Is this possible to express with a Crontab pattern at all?
You need to split it into two expressions :
0 40-55/5 14 * * MON-FRI
0 0 15 * * MON-FRI
If you only want to have one expression and you are okay with the last trigger time in a day run one minute before, you can use :
0 40-55/5,59 14 * * MON-FRI
Spring internally use CronSequenceGenerator to generate the next trigger time for a cron expression. You can use it to verify a cron expression.
For example , to verify the next several trigger times of 0 40-55/5,59 14 * * MON-FRI:
CronSequenceGenerator generator = new CronSequenceGenerator("0 40-55/5,59 14 * * MON-FRI");
Date d = Date.from(LocalDateTime.now().atZone(ZoneId.systemDefault()).toInstant());
for (int i = 0; i < 10; i++) {
d = generator.next(d);
System.out.println(d);
}
which will print out :
Tue Jun 02 14:40:00 HKT 2020
Tue Jun 02 14:45:00 HKT 2020
Tue Jun 02 14:50:00 HKT 2020
Tue Jun 02 14:55:00 HKT 2020
Tue Jun 02 14:59:00 HKT 2020
Wed Jun 03 14:40:00 HKT 2020
Wed Jun 03 14:45:00 HKT 2020
Wed Jun 03 14:50:00 HKT 2020
Wed Jun 03 14:55:00 HKT 2020
Wed Jun 03 14:59:00 HKT 2020

Lunar code magnet,Head first java, pg-314

I am new to java can anyone explain to me how this code works in this example
specially the LOOP portion
import java.util.*;
import static java.lang.System.out;
class FullMoons {
static int DAY_IM = 1000 * 60 * 60 * 24;
public static void main(String [] args) {
Calendar c = Calendar.getInstance();
c.set(2004,0,7,15,40);
long day1 = c.getTimeInMillis();
for (int x = 0; x < 60; x++) {
day1 += (DAY_IM * 29.52);
c.setTimeInMillis(day1);
out.println(String.format("full moon on %tc", c));
}
}
}
output is
full moon on Fri Feb 06 04:06:35 MST 2004
full moon on Sat Mar 06 16:38:23 MST 2004
full moon on Mon Apr 05 06:07:11 MDT 2004
an answer should be displayed in first three loops why it takes entire 60 runnings
What is the answer and why it should be displayed in first 3 loops?
This code creates Calendar and then added to the first full moon date the duration of lunar month (about 29.52 days).
The cycle body runs 60 times, because condition x < 60 and x++ increase x by 1.
//increase number of milliseconds
day1 += (DAY_IM * 29.52);
//set increased number to Calendar c
c.setTimeInMillis(day1);
//create formatted string
String s = String.format("full moon on %tc", c);
//output the string
out.println(s);
read more about how String.format() works in official documentation
This code will run 60 times throughout the Loop..
I realized my mistake which is iam only going by theory rather than doing practical...so guys this is lesson, and iam posting the result so that this question dosent create any unnecessary confusion further.
full moon on Fri Feb 06 04:08:48 IST 2004
full moon on Sat Mar 06 16:37:36 IST 2004
full moon on Mon Apr 05 05:06:24 IST 2004
full moon on Tue May 04 17:35:12 IST 2004
full moon on Thu Jun 03 06:04:00 IST 2004
full moon on Fri Jul 02 18:32:48 IST 2004
full moon on Sun Aug 01 07:01:36 IST 2004
full moon on Mon Aug 30 19:30:24 IST 2004
full moon on Wed Sep 29 07:59:12 IST 2004
full moon on Thu Oct 28 20:28:00 IST 2004
full moon on Sat Nov 27 08:56:48 IST 2004
full moon on Sun Dec 26 21:25:36 IST 2004
full moon on Tue Jan 25 09:54:24 IST 2005
full moon on Wed Feb 23 22:23:12 IST 2005
full moon on Fri Mar 25 10:52:00 IST 2005
full moon on Sat Apr 23 23:20:48 IST 2005
full moon on Mon May 23 11:49:36 IST 2005
full moon on Wed Jun 22 00:18:24 IST 2005
full moon on Thu Jul 21 12:47:12 IST 2005
full moon on Sat Aug 20 01:16:00 IST 2005
full moon on Sun Sep 18 13:44:48 IST 2005
full moon on Tue Oct 18 02:13:36 IST 2005
full moon on Wed Nov 16 14:42:24 IST 2005
full moon on Fri Dec 16 03:11:12 IST 2005
full moon on Sat Jan 14 15:40:00 IST 2006
full moon on Mon Feb 13 04:08:48 IST 2006
full moon on Tue Mar 14 16:37:36 IST 2006
full moon on Thu Apr 13 05:06:24 IST 2006
full moon on Fri May 12 17:35:12 IST 2006
full moon on Sun Jun 11 06:04:00 IST 2006
full moon on Mon Jul 10 18:32:48 IST 2006
full moon on Wed Aug 09 07:01:36 IST 2006
full moon on Thu Sep 07 19:30:24 IST 2006
full moon on Sat Oct 07 07:59:12 IST 2006
full moon on Sun Nov 05 20:28:00 IST 2006
full moon on Tue Dec 05 08:56:48 IST 2006
full moon on Wed Jan 03 21:25:36 IST 2007
full moon on Fri Feb 02 09:54:24 IST 2007
full moon on Sat Mar 03 22:23:12 IST 2007
full moon on Mon Apr 02 10:52:00 IST 2007
full moon on Tue May 01 23:20:48 IST 2007
full moon on Thu May 31 11:49:36 IST 2007
full moon on Sat Jun 30 00:18:24 IST 2007
full moon on Sun Jul 29 12:47:12 IST 2007
full moon on Tue Aug 28 01:16:00 IST 2007
full moon on Wed Sep 26 13:44:48 IST 2007
full moon on Fri Oct 26 02:13:36 IST 2007
full moon on Sat Nov 24 14:42:24 IST 2007
full moon on Mon Dec 24 03:11:12 IST 2007
full moon on Tue Jan 22 15:40:00 IST 2008
full moon on Thu Feb 21 04:08:48 IST 2008
full moon on Fri Mar 21 16:37:36 IST 2008
full moon on Sun Apr 20 05:06:24 IST 2008
full moon on Mon May 19 17:35:12 IST 2008
full moon on Wed Jun 18 06:04:00 IST 2008
full moon on Thu Jul 17 18:32:48 IST 2008
full moon on Sat Aug 16 07:01:36 IST 2008
full moon on Sun Sep 14 19:30:24 IST 2008
full moon on Tue Oct 14 07:59:12 IST 2008
full moon on Wed Nov 12 20:28:00 IST 2008

Java, failed test after changing Time zone on PC

Initially I had quite simple test:
#Test
public void testMe() {
System.out.println("Records in H2 db:");
List<DateRangeBean> all = dateRangeServiceImpl.findAll();
all.forEach(x -> System.out.println(x.getDateTo()));
Clock.system(ZoneId.of("UTC"));
ZonedDateTime currentDate = ZonedDateTime.of(2016, 8, 9, 0, 0, 0, 0, clock.getZone());
Date currentDate_date = Date.from(currentDate.toInstant());
System.out.println("Current Date is: " + currentDate_date);
List<DateRangeBean> result = new ArrayList<>();
dateRangeServiceImpl.findGreaterDateTo(currentDate_date).iterator().forEachRemaining(result::add);
result.forEach(x -> System.out.println(x.getDateTo()));
assertEquals(result.size(), 2);
}
that always was executed on PC with default TimeZone UTC and with the following output:
Records in H2 db:
Mon Aug 08 00:00:00 UTC 2016
Thu Oct 20 00:00:00 UTC 2016
Thu Oct 20 00:00:00 UTC 2016
Current Date is: Tue Aug 09 00:00:00 UTC 2016
Thu Oct 20 00:00:00 UTC 2016
Thu Oct 20 00:00:00 UTC 2016
where:
dateRangeServiceImpl.findAll() implemented as:
public List<DateRangeBean> findAll (){
List<DateRangeBean> result = new ArrayList<>();
dateRangeRepository.findAll().iterator().forEachRemaining(result::add);
return result;
}
dateRangeServiceImpl.findGreatedDateTo(Date date):
public List<DateRangeBean> test (Date currentDate){
List<DateRangeBean> result = new ArrayList<>();
dateRangeRepository.findGreatedDateTo(currentDate).iterator().forEachRemaining(result::add);
return result;
}
and dateRangeRepository - is pure interface as part of Spring Data
public interface DateRangeRepository extends PagingAndSortingRepository<DateRangeBean, Long> {
#Query("from DateRangeBean drb where (drb.dateTo >= :currentDate)")
List<DateRangeBean> findGreatedDateTo(#Param("currentDate") Date currentDate);
}
and DateRangeBean:
#Entity
public class DateRangeBean implements java.io.Serializable {
#Temporal(TemporalType.DATE)
#Column(name = "date_To", length = 10)
private Date dateTo;
.....................
public Date getDateTo() {
return new Date(this.dateTo.getTime());
}
public void setDateTo(Date dateTo) {
this.dateTo = new Date(dateTo.getTime());
}
}
Today I have tried to run this test on PC with Time Zone: UTC -07:00 and test failed with the following output:
Records in H2 db:
Mon Aug 08 00:00:00 MST 2016
Thu Oct 20 00:00:00 MST 2016
Thu Oct 20 00:00:00 MST 2016
Current Date is: Mon Aug 08 17:00:00 MST 2016
Mon Aug 08 00:00:00 MST 2016
Thu Oct 20 00:00:00 MST 2016
Thu Oct 20 00:00:00 MST 2016
java.lang.AssertionError: expected [2] but found [3]
Expected :2
Actual :3
Okey, maybe, it is reasonable: we defined currentDate variable Aug 9 UTC TZ, but on the next step we re-converted currentDate to currentDate_date in MST TZ and as result it is Aug 8.
BUT:
Q1:
based on our #Query: drb.DateTo >= :currentDate
in current case: Mon Aug 08 00:00:00 MST 2016 >= Mon Aug 08 17:00:00 MST 2016 - this statement is incorrect!
Assume, that Spring Data compares only Day, Month, Year and trims Time segment.
Is it correct??
Q2:
I tried to fix the test putting the timezone directly in the test:
public void testMe() {
TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
..............
}
But I still getting failed result:
Records in H2 db:
Mon Aug 08 07:00:00 UTC 2016
Thu Oct 20 07:00:00 UTC 2016
Thu Oct 20 07:00:00 UTC 2016
Current Date is: Tue Aug 09 00:00:00 UTC 2016
Mon Aug 08 07:00:00 UTC 2016
Thu Oct 20 07:00:00 UTC 2016
Thu Oct 20 07:00:00 UTC 2016
java.lang.AssertionError: expected [2] but found [3]
And at the moment I can't understand how is it possible:
Mon Aug 08 07:00:00 UTC 2016 >= Tue Aug 09 00:00:00 UTC 2016 ?
Q3:
I tried to specify TZ as part of Spring Context:
<bean id="defaultZoneInfo" class="sun.util.calendar.ZoneInfo" factory-method="getTimeZone">
<constructor-arg type="java.lang.String" value="UTC"/>
</bean>
<bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="staticMethod" value="java.util.TimeZone.setDefault"/>
<property name="arguments">
<list>
<ref bean="defaultZoneInfo"/>
</list>
</property>
</bean>
but the result was the same as with Q2.
Q4:
Specify TZ in static block:
static {
TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
}
and test - passed!
Records in H2 db:
Mon Aug 08 00:00:00 UTC 2016
Thu Oct 20 00:00:00 UTC 2016
Thu Oct 20 00:00:00 UTC 2016
Current Date is: Tue Aug 09 00:00:00 UTC 2016
Thu Oct 20 00:00:00 UTC 2016
Thu Oct 20 00:00:00 UTC 2016
So, can someone explain why specifying default TZ in Spring Context xml file and directly in test method didn't fix problem?
And only specifying TZ via static block solved the problem?
I think it may be your database problem not your application
for example Oracle database not accept connection from client after changing Client location in control panel.

Calender.YEAR not printing the right value

I'm parsing the Date that received from user query and another date from XML file.
When I want to get just the YEAR, it print 1 instead of year, by using:
calender.YEAR
This is the code:
Elements date = doc.getElementsByTag("DATE");
DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
Date inputDate = dateFormat.parse(obj.tryParse(dateFrom));
Date xmlDate = dateFormat.parse(obj.tryParse(date.get(0).text()));
Calendar calInput = Calendar.getInstance();
Calendar calXml = Calendar.getInstance();
calInput.setTime(inputDate);
calXml.setTime(xmlDate);
System.out.println(calInput.YEAR +" "+ calXml.YEAR +" = "+calInput.getTime().toString()+"|||"+ calXml.getTime().toString());
if(calInput.YEAR != calXml.YEAR)
continue;
And this is a sample of the output:
Info: 1 1 = Wed Jan 01 00:00:00 IST 2003|||Tue Jan 01 00:00:00 IST 2002
Info: 1 1 = Wed Jan 01 00:00:00 IST 2003|||Tue Jan 01 00:00:00 IST 2002
Info: 1 1 = Wed Jan 01 00:00:00 IST 2003|||Tue Jan 01 00:00:00 IST 2002
Info: 1 1 = Wed Jan 01 00:00:00 IST 2003|||Tue Jan 01 00:00:00 IST 2002
Info: 1 1 = Wed Jan 01 00:00:00 IST 2003|||Tue Jan 01 00:00:00 IST 2002
Info: 1 1 = Wed Jan 01 00:00:00 IST 2003|||Tue Jan 01 00:00:00 IST 2002
Info: 1 1 = Wed Jan 01 00:00:00 IST 2003|||Tue Jan 01 00:00:00 IST 2002
Info: 1 1 = Wed Jan 01 00:00:00 IST 2003|||Tue Jan 01 00:00:00 IST 2002
Info: 1 1 = Wed Jan 01 00:00:00 IST 2003|||Tue Jan 01 00:00:00 IST 2002
Info: 1 1 = Wed Jan 01 00:00:00 IST 2003|||Tue Jan 01 00:00:00 IST 2002
Info: 1 1 = Wed Jan 01 00:00:00 IST 2003|||Sun Feb 01 00:00:00 IST 2004
Info: 1 1 = Wed Jan 01 00:00:00 IST 2003|||Mon Apr 01 00:00:00 IDT 2002
Info: 1 1 = Wed Jan 01 00:00:00 IST 2003|||Mon Apr 01 00:00:00 IDT 2002
Info: 1 1 = Wed Jan 01 00:00:00 IST 2003|||Mon Apr 01 00:00:00 IDT 2002
Info: 1 1 = Wed Jan 01 00:00:00 IST 2003|||Mon Apr 01 00:00:00 IDT 2002
Info: 1 1 = Wed Jan 01 00:00:00 IST 2003|||Mon Apr 01 00:00:00 IDT 2002
Info: 1 1 = Wed Jan 01 00:00:00 IST 2003|||Mon Apr 01 00:00:00 IDT 2002
Info: 1 1 = Wed Jan 01 00:00:00 IST 2003|||Mon Apr 01 00:00:00 IDT 2002
Info: 1 1 = Wed Jan 01 00:00:00 IST 2003|||Mon Apr 01 00:00:00 IDT 2002
calXml.YEAR should be written Calendar.YEAR, because it is a constant (public static final) value to be used as a parameter to methods like get(int field) and add(int field, int amount).
To get the year, call calXml.get(Calendar.YEAR).
The Answer by Andreas is correct.
java.time
Alternatively, you can use the new java.time framework built into Java 8 and later. The old date-time classes are a mess and should be avoided.
Convert from java.util.Date to java.time.Instant. An Instant is a moment on the timeline in UTC.
Instant instant = yourJavaUtilDate.toInstant();
Apply a time zone. Your Question's code implicitly depends on the JVM’s current default time zone. I suggest that it is better to explicitly specify your desired/expected time zone. The time zone is critical in determining a date and therefore the year.
ZoneId zoneId = ZoneId.of( "Asia/Kolkata" );
ZonedDateTime zdt = ZonedDateTime.ofInstant( instant , zoneId );
Now we can extract the year number.
int year = zdt.getYear();
Or more interesting, we can get a Year object.
Year year = Year.from( zdt );
Boolean isLeapYear = year.isLeap();
int yearNumber = year.getValue();

Subtracting dates in Java. Daylight Savings Time Changes

The problem is that the subtraction of the two dates leaves me double identical value.
From : Thu Nov 29 00:00:00 CET 2012
Value 121: To: Sat Mar 30 00:00:00 CET 2013
Value 122: To: Sun Mar 31 00:00:00 CET 2013
Value 122: To: Mon Apr 01 00:00:00 CEST 2013
Value 123: To: Tue Apr 02 00:00:00 CEST 2013
Long today = (To.getTime() - From.getTime()) / (1000 * 60 * 60 * 24);
Is it possible to somehow avoid that logically? (sorry for English)

Categories

Resources