perl Dowload email headers from gmail for parsing - perl

I am writing an Icinga plugin to check if the smtp server we have contracted with a third party gets blacklisted.
The service uses an unknown number of smtp relays. I need to download all the "Received" sections of the headers, and parse them to get the different IPs of the SMTP relays.
I am trying to use Mail::IMAPClient, and I can perform some operations on the account (login, chose folder, search the messages, etc), but I haven't found a way to get the whole header nor the sections of it I need.
I don't mind using a different module if needed.

You could try using the parse_headers function. According to the example in the documentation, you can use it like this:
$hashref = $imap->parse_headers(1,"Date","Received","Subject","To");
And then you get a hash reference that maps field names to references to array of values, like this:
$hashref = {
"Date" => [ "Thu, 09 Sep 1999 09:49:04 -0400" ] ,
"Received" => [ q/
from mailhub ([111.11.111.111]) by mailhost.bigco.com
(Netscape Messaging Server 3.6) with ESMTP id AAA527D for
<bigshot#bigco.com>; Fri, 18 Jun 1999 16:29:07 +0000
/, q/
from directory-daemon by mailhub.bigco.com (PMDF V5.2-31 #38473)
id <0FDJ0010174HF7#mailhub.bigco.com> for bigshot#bigco.com
(ORCPT rfc822;big.shot#bigco.com); Fri, 18 Jun 1999 16:29:05 +0000 (GMT)
/, q/
from someplace ([999.9.99.99]) by smtp-relay.bigco.com (PMDF V5.2-31 #38473)
with ESMTP id <0FDJ0000P74H0W#smtp-relay.bigco.com> for big.shot#bigco.com; Fri,
18 Jun 1999 16:29:05 +0000 (GMT)
/] ,
"Subject" => [ qw/ Help! I've fallen and I can't get up!/ ] ,
"To" => [ "Big Shot <big.shot#bigco.com> ] ,
};
That should give you all the Received headers in a single array.

Related

Telegraf inputs.tail with zimbra.log

I have some questions, how I can set telegraf.conf file for collect logs from the "zimbra.conf" file?
Now I tried to use this config text, but it does not work :(((
I want to send this logs to grafana
One of the lines "zimbra.conf" for example:
Oct 1 10:20:46 webmail postfix/smtp[7677]: BD5BAE9999: to=user#mail.com, relay=mo94.cloud.mail.com[92.97.907.14]:25, delay=0.73, delays=0.09/0.01/0.58/0.19, dsn=2.0.0, status=sent (250 2.0.0 Ok: queued as 4C25fk2pjFz32N5)
And I do not understand exactly how works the "grok_patterns ="
[[inputs.tail]]
files = ["/var/log/zimbra.log"]
from_beginning = false
grok_patterns = ['%{SYSLOGTIMESTAMP:timestamp} %{SYSLOGHOST} %{DATA:program}(?:\[%{POSINT}\])?: %{GREEDYDATA:message}']
name_override = "zimbra_access_log"
grok_custom_pattern_files = []
grok_custom_patterns = '''
TS_UNIX %{MONTH}%{SPACE}%{MONTHDAY}%{SPACE}%{HOUR}:%{MINUTE}:%{SECOND}
TS_CUSTOM %{MONTH}%{SPACE}%{MONTHDAY} %{HOUR}:%{MINUTE}:%{SECOND}
'''
grok_timezone = "Local"
data_format = "grok"
I have copied your example line into a log file called Prueba.txt wich contains the following lines:
Oct 3 00:52:32 webmail postfix/smtp[7677]: BD5BAE9999: to=user#mail.com, relay=mo94.cloud.mail.com[92.97.907.14]:25, delay=0.73, delays=0.09/0.01/0.58/0.19, dsn=2.0.0, status=sent (250 2.0$
Oct 13 06:25:01 webmail systemd-logind[949]: New session 229478 of user zimbra.
Oct 13 06:25:02 webmail zmconfigd[27437]: Shutting down. Received signal 15
Oct 13 06:25:02 webmail systemd-logind[949]: Removed session c296.
Oct 13 06:25:03 webmail sshd[28005]: Failed password for invalid user julianne from 120.131.2.210 port 10570 ssh2
I have been able to parse the data with this configuration of the tail.input plugin:
[[inputs.tail]]
files = ["Prueba.txt"]
from_beginning = true
data_format = "grok"
grok_patterns = ['%{TIMESTAMP_ZIMBRA} %{GREEDYDATA:source} %{DATA:program}(?:\[%{POSINT}\])?: %{GREEDYDATA:message}']
grok_custom_patterns = '''
TIMESTAMP_ZIMBRA (\w{3} \d{1,2} \d{2}:\d{2}:\d{2})
'''
name_override = "log_frames"
You need to match the input string with regular expressions. For that there are some predefined patters such as GREEDYDATA = .* that you can use to match your input (another example will be NUMBER = (?:%{BASE10NUM}) BASE16NUM (?<![0-9A-Fa-f])(?:[+-]?(?:0x)?(?:[0-9A-Fa-f]+))) . You can also define your own patterns in grok_custom_patterns. Take a look at this website with some patters: https://streamsets.com/documentation/datacollector/latest/help/datacollector/UserGuide/Apx-GrokPatterns/GrokPatterns_title.html
In this case I defined a TIMESTAMP_ZIMBRA pattern for matching Oct 3 00:52:32 and Oct 03 00:52:33 alike inputs.
Here is the collected metric by Prometheus:
# HELP log_frames_delay Telegraf collected metric
# TYPE log_frames_delay untyped
log_frames_delay{delays="0.09/0.01/0.58/0.19",dsn="2.0.0",host="localhost.localdomain",message="BD5BAE9999:",path="Prueba.txt",program="postfix/smtp",relay="mo94.cloud.mail.com[92.97.907.14]:25",source="webmail",status="sent (250 2.0.0 Ok: queued as 4C25fk2pjFz32N5)",to="user#mail.com"} 0.73
P.D.: Ensure that telegraf has access to the log files.

creating arrays of results based upon chunks of time

I've got an app that has lots of sensor_events being saved; I'd like to get results by a date then map those into chunks of 15 minute times... this is not the same as doing a group by in postgres as that would only return something averaged and I need the specific events...
What I'm thinking is given a day I get the beginning_of_day and split it up as 15 minute chunks as keys to a hash of arrays ie
def return_time_chunk_hash
t=Date.today
st = t.beginning_of_day
times = Hash.new
while st<t.end_of_day
times[st.to_formatted_s(:time)] = Array.new
st = st + 15.minutes
end
return times
end
And from that I would compare the sensor_events created_at date, find which bucket it belonged to and plop it in there. Once I've got it that way I know whether or not a chunk has any (.count) and if so can do all the data manipulation on the specific events.
Does this seem nutty? Is there a simpler way I'm not seeing?
Update:
I liked the way jgraft was thinking but thought it wouldn't work as I'd have to do multiple queries based upon the group column flag, but then I thought of the group_by of Enumerable so I tried something like this in the actual SensorEvent model:
def chunk
Time.at((self.created_at.to_f / 15.minutes).floor * 15.minutes).to_formatted_s(:time)
end
This allows me to get all the sensor events I need as usual (ie #se=SensorEvent.where(sensor_id: 10)) but then I could do #se.group_by(&:chunk) and I get those singular events grouped into a hash ie:
{"13:30"=>
[#<SensorEvent:0x007ffac0128438
id: 25006,
force: 0.0,
xaccel: 502.0,
yaccel: 495.0,
zaccel: 616.0,
battery: 0.0,
position: 25.0,
created_at: Thu, 18 Jun 2015 13:33:37 EDT -04:00,
updated_at: Thu, 18 Jun 2015 15:51:32 EDT -04:00,
deviceID: "D36330135FE3D36",
location: 3,
sensor_id: 10>,
#<SensorEvent:0x007ffac0128140
id: 25007,
force: 0.0,
xaccel: 502.0,
yaccel: 495.0,
zaccel: 616.0,
battery: 0.0,
position: 27.0,
created_at: Thu, 18 Jun 2015 13:39:46 EDT -04:00,
updated_at: Thu, 18 Jun 2015 15:51:32 EDT -04:00,
deviceID: "D36330135FE3D36",
location: 3,
sensor_id: 10>,
.........
The trouble is of course not every chunk of time might be created since there was no event to spawn it; also that being a hash it's not sorted in anyway:
res.keys
=> ["13:30",
"13:45",
"14:00",
"13:00",
"15:45",
"16:00",
"16:15",
"16:45",
"17:00",
"17:15",
"17:30",
"14:15",
"14:30",
I have to do calculations on the chunks of events; I might keep a master TIMECHUNKS array to compare / lookup in order...
Why not just add a column to the sensor_events table that specifies which block it belongs to? Would basically give you an array as you could do a query like:
SensorEvent.where(date: Date.today, block: 1)
and return a relation of the data in an array-esque format.
You could just add an after_create callback to the SensorEvents model that sets the block column.
class SensorEvent < ActiveRecord::Base
after_create :set_block
private
def set_block
value = ((4 * created_at.hour) + (created_at.min.to_f / 15).ceil).to_i
self.update_columns(block: value)
end

Dealing with (birthday) dates and Timezone with Dojo

I have a simple widget that has:
<input class="input" id="${id}_dateOfBirth" name="dateOfBirth" data-dojo-type="dijit/form/DateTextBox" />
Note that it's a birthday. So, it's meant to stay the same, regardless of where you are when you are (timezone shouldn't happen). If you are born on the 10th of January at 3:00AM in England, and view your personal information from New York, you are meant to still see 10th of January, NOT the 9th!
I am in GMT+8 right now.
When I submit this form, this actually gets to the server when I put in 1/1/1970:
dateOfBirth: "1969-12-31T16:00:00.000Z"
Which is bad, because it's 8 hours short of the actual date.
Basically, I need a way for the DateTextBox to show the date as it came from the server, effectively ignoring the browser's timezone.
FWIW, here is my variant of UTCDateTextBox:
define([
"dojo/_base/declare",
"dijit/form/DateTextBox"
], function(declare, DateTextBox) {
function isValidDate(value) {
return value instanceof Date && isFinite(value.getTime());
}
function toUTCDate(value) {
if (isValidDate(value)) {
value = new Date(
Date.UTC(value.getFullYear(), value.getMonth(), value.getDate())
);
}
return value;
}
return declare(DateTextBox, {
_getValueAttr : function() {
return toUTCDate(this.inherited("_getValueAttr", arguments));
}
});
});
For my use case, I found that I didn't need to override _setValueAttr(). With the above implementation, when getUTCXXX(), toUTCString(), toISOString() or toJSON() are called on the date object returned from _getValueAttr(), then the correct UTC date with zeroed time elements is returned.
Hope this helps.
After much hacking and analysing Dojo's source code, I came up with this:
var UTCDateTextBox = declare( 'UTCDateTextBox', [ DateTextBox ], {
_getValueAttr: function(){
var ov = this.inherited(arguments);
if( ov ){
ov.setTime( ov.getTime() - ov.getTimezoneOffset() * 60 * 1000 );
}
return ov;
},
_setValueAttr: function( value, priorityChange, formattedValue){
var v = stamp.fromISOString( value );
if( v ){
v.setTime( v.getTime() + v.getTimezoneOffset() * 60 * 1000 );
value = v;
}
this.inherited(arguments);
}
});
Basically:
When the value is set, the timezone difference gets added. This means that if the server has 1979-12-25T00:00:00.000Z, rather than assigning Tue Dec 25 1979 08:00:00 GMT+0800 (WST), it will assign Tue Dec 25 1979 00:00:00 GMT+0800 (WST) . Basically, the date is converted locally to whatever it was in UTC.
When the value is parsed, it will be changed from Tue Dec 25 1979 00:00:00 GMT+0800 (WST) to Tue Dec 25 1979 08:00:00 GMT+0800 (WST)
The changed value is the one submitted to the server. So, it will be correct regardless of what timezone it will be edited at.
Since I only ever ever deal with dates, if the server has 1979-12-31T23:00:00Z (which is an error: for birthdays, the time is actually ignored and mustn't matter), this will happen:
When the value is set, ISO is 1979-12-31T23:00:00.000Z. So, Tue Jan 01 1980 07:00:00 GMT+0800 (WST) is changed into Mon Dec 31 1979 23:00:00 GMT+0800 (WST). This means that the right date is placed into the date textbox (31/12/1979).
When the value is parsed from the textbox, Mon Dec 31 1979 00:00:00 GMT+0800 (WST) becomes Mon Dec 31 1979 08:00:00 GMT+0800 (WST). So, the server will save 1979-12-31T00:00:00Z -- which is, again, the correct date!
If there are bettere solutions, please let me know. Frankly, I hope there are as this one feels like a bit of a cheat!

How to parse Twitter search result with iOS NSJSONSerialization in a UITableView?

I'm having trouble parsing a Twitter search result with the built in iOS JSON parser.
NSDictionary *resultorig = [NSJSONSerialization JSONObjectWithData:self.responseData options: NSJSONReadingMutableContainers error: &err];
NSMutableArray *result = [resultorig objectForKey:#"results"];
NSLog(#"%#", result);
I'm searching Twitter with this url: http://search.twitter.com/search.json?q=%23xbox
in which prints out:
{"completed_in":0.013,"max_id":288030748135530498,"max_id_str":"288030748135530498","next_page":"?page=2&max_id=288030748135530498&q=%23xbox","page":1,"query":"%23xbox","refresh_url":"?since_id=288030748135530498&q=%23xbox","results":[{"created_at":"Sun, 06 Jan 2013 21:14:16 +0000","from_user":"momarkmagic","from_user_id":41954598,"from_user_id_str":"41954598","from_user_name":"Mark Molnar","geo":null,"id":288030748135530498,"id_str":"288030748135530498","iso_language_code":"en","metadata":{"result_type":"recent"},"profile_image_url":"http:\/\/a0.twimg.com\/profile_images\/342500438\/avat_normal.jpg","profile_image_url_https":"https:\/\/si0.twimg.com\/profile_images\/342500438\/avat_normal.jpg","source":"<a href="http:\/\/twitter.com\/">web<\/a>","text":"RT #conceptartworld: Check out Halo 4 Concept Art by Thomas Scholes! http:\/\/t.co\/JdDr40XM #xbox #illustration http:\/\/t.co\/AKjZGKgN","to_user":null,"to_user_id":0,"to_user_id_str":"0","to_user_name":null},{"created_at":"Sun, 06 Jan 2013 21:14:07 +0000","from_user":"DeJesusRaymond","from_user_id":57501625,"from_user_id_str":"57501625","from_user_name":"Raymond DeJesus\u0950","geo":null,"id":288030711229861888,"id_str":"288030711229861888","iso_language_code":"da","metadata":{"result_type":"recent"},"profile_image_url":"http:\/\/a0.twimg.com\/profile_images\/3069994571\/4b2761d7571bb012354f7efd47c71eb2_normal.jpeg","profile_image_url_https":"https:\/\/si0.twimg.com\/profile_images\/3069994571\/4b2761d7571bb012354f7efd47c71eb2_normal.jpeg","source":"<a href="http:\/\/instagr.am">Instagram<\/a>","text":"#360 #xbox #swag #thuglife. http:\/\/t.co\/Q0mteFMA","to_user":null,"to_user_id":0,"to_user_id_str":"0","to_user_name":null},{"created_at":"Sun, 06 Jan 2013 21:13:59 +0000","from_user":"ShotzLiam_3","from_user_id":598301611,"from_user_id_str":"598301611","from_user_name":"Liam","geo":null,"id":288030679453798400,"id_str":"288030679453798400","iso_language_code":"en","metadata":{"result_type":"recent"},"profile_image_url":"http:\/\/a0.twimg.com\/profile_images\/2866443960\/9c7349d8672774cae56aedfcc1955b31_normal.jpeg","profile_image_url_https":"https:\/\/si0.twimg.com\/profile_images\/2866443960\/9c7349d8672774cae56aedfcc1955b31_normal.jpeg","source":"<a href="http:\/\/twitter.com\/download\/iphone">Twitter for iPhone<\/a>","text":"#FifaaGiveaways can I have some coins #xbox","to_user":"FifaaGiveaways","to_user_id":943979888,"to_user_id_str":"943979888","to_user_name":"Free UT Giveaways"},{"created_at":"Sun, 06 Jan 2013 21:13:28 +0000","from_user":"Kuvaga","from_user_id":83686315,"from_user_id_str":"83686315","from_user_name":"Kut V. \u26a1","geo":null,"id":288030544850206720,"id_str":"288030544850206720","iso_language_code":"es","metadata":{"result_type":"recent"},"profile_image_url":"http:\/\/a0.twimg.com\/profile_images\/3060318139\/af19b6a35cdf09e4f54e12835e18093e_normal.jpeg","profile_image_url_https":"https:\/\/si0.twimg.com\/profile_images\/3060318139\/af19b6a35cdf09e4f54e12835e18093e_normal.jpeg","source":"<a href="http:\/\/twitter.com\/download\/iphone">Twitter for iPhone<\/a>","text":"Este es el que te dije #GarcIA_MGM #Xbox http:\/\/t.co\/04N9fESH","to_user":null,"to_user_id":0,"to_user_id_str":"0","to_user_name":null},{"created_at":"Sun, 06 Jan 2013 21:13:24 +0000","from_user":"benzybk","from_user_id":275588581,"from_user_id_str":"275588581","from_user_name":"Benzy Babykutty","geo":null,"id":288030531063541762,"id_str":"288030531063541762","iso_language_code":"en","metadata":{"result_type":"recent"},"profile_image_url":"http:\/\/a0.twimg.com\/profile_images\/3040249039\/d741b8ccfdb9d130be72eadd77471adc_normal.jpeg","profile_image_url_https":"https:\/\/si0.twimg.com\/profile_images\/3040249039\/d741b8ccfdb9d130be72eadd77471adc_normal.jpeg","source":"<a href="http:\/\/twitter.com\/download\/iphone">Twitter for iPhone<\/a>","text":"RT #TheSunNewspaper: The next Xbox versus the PS4, we take a look at what to expect. http:\/\/t.co\/SdFzZUYv #xbox #ps3 #gaming","to_user":null,"to_user_id":0,"to_user_id_str":"0","to_user_name":null},{"created_at":"Sun, 06 Jan 2013 21:13:13 +0000","from_user":"Lewis_Whitfield","from_user_id":53468672,"from_user_id_str":"53468672","from_user_name":"ImLatchingOntoYou.","geo":null,"id":288030482623516672,"id_str":"288030482623516672","iso_language_code":"en","metadata":{"result_type":"recent"},"profile_image_url":"http:\/\/a0.twimg.com\/profile_images\/3008431805\/15b78d1e299047711e5ab6da0cbe3e28_normal.jpeg","profile_image_url_https":"https:\/\/si0.twimg.com\/profile_images\/3008431805\/15b78d1e299047711e5ab6da0cbe3e28_normal.jpeg","source":"<a href="http:\/\/instagr.am">Instagram<\/a>","text":"Completed Forza Horizon! Yay #xbox #xbox360 #forza #completed #sad #intense #best #game #ever http:\/\/t.co\/AedVwph0","to_user":null,"to_user_id":0,"to_user_id_str":"0","to_user_name":null},{"created_at":"Sun, 06 Jan 2013 21:12:51 +0000","from_user":"sexyboy529","from_user_id":394412198,"from_user_id_str":"394412198","from_user_name":"Sexyboy","geo":null,"id":288030392844435456,"id_str":"288030392844435456","iso_language_code":"en","metadata":{"result_type":"recent"},"profile_image_url":"http:\/\/a0.twimg.com\/sticky\/default_profile_images\/default_profile_5_normal.png","profile_image_url_https":"https:\/\/si0.twimg.com\/sticky\/default_profile_images\/default_profile_5_normal.png","source":"<a href="http:\/\/twitter.com\/">web<\/a>","text":"RT #Sam_James96: My life is dedicated to xbox today. #xbox #GTA5","to_user":null,"to_user_id":0,"to_user_id_str":"0","to_user_name":null},{"created_at":"Sun, 06 Jan 2013 21:12:28 +0000","from_user":"Andreact93","from_user_id":211217957,"from_user_id_str":"211217957","from_user_name":"Andrea Gazzo","geo":null,"id":288030296312537090,"id_str":"288030296312537090","iso_language_code":"da","metadata":{"result_type":"recent"},"profile_image_url":"http:\/\/a0.twimg.com\/profile_images\/1771937572\/foto_twitter_normal.jpg","profile_image_url_https":"https:\/\/si0.twimg.com\/profile_images\/1771937572\/foto_twitter_normal.jpg","source":"<a href="http:\/\/instagr.am">Instagram<\/a>","text":"#xbox360 #controller #game #halo4 #games #gamers #xbox #modded #led #blue http:\/\/t.co\/bTkCuyT0","to_user":null,"to_user_id":0,"to_user_id_str":"0","to_user_name":null},{"created_at":"Sun, 06 Jan 2013 21:12:24 +0000","from_user":"2xMolly2xBlunt","from_user_id":259448291,"from_user_id_str":"259448291","from_user_name":"\u2665Rihanna\u2665BabyDaddy\u2665\ue420","geo":null,"id":288030277299736577,"id_str":"288030277299736577","iso_language_code":"en","metadata":{"result_type":"recent"},"profile_image_url":"http:\/\/a0.twimg.com\/profile_images\/2956632635\/442f652a4e993f26d285e80ccb57910a_normal.png","profile_image_url_https":"https:\/\/si0.twimg.com\/profile_images\/2956632635\/442f652a4e993f26d285e80ccb57910a_normal.png","source":"<a href="http:\/\/twitter.com\/">web<\/a>","text":"Time to play 2k who wanna play #RT #2K13 #XBOX","to_user":null,"to_user_id":0,"to_user_id_str":"0","to_user_name":null},{"created_at":"Sun, 06 Jan 2013 21:12:23 +0000","from_user":"_kdog","from_user_id":118113755,"from_user_id_str":"118113755","from_user_name":"Kirsten Thomson","geo":null,"id":288030276670611458,"id_str":"288030276670611458","iso_language_code":"en","metadata":{"result_type":"recent"},"profile_image_url":"http:\/\/a0.twimg.com\/profile_images\/3061762292\/c8d1047d30ab44a6ee922500bce85370_normal.jpeg","profile_image_url_https":"https:\/\/si0.twimg.com\/profile_images\/3061762292\/c8d1047d30ab44a6ee922500bce85370_normal.jpeg","source":"<a href="http:\/\/instagr.am">Instagram<\/a>","text":"I love skyrim so much. #skyrim #Lynda #Clyde #ilovewater #xbox #sarahando http:\/\/t.co\/bM2dSIkZ","to_user":null,"to_user_id":0,"to_user_id_str":"0","to_user_name":null},{"created_at":"Sun, 06 Jan 2013 21:12:10 +0000","from_user":"ReeceAxten","from_user_id":405803362,"from_user_id_str":"405803362","from_user_name":"ReeceAxten\u00ae","geo":null,"id":288030220164952064,"id_str":"288030220164952064","iso_language_code":"tl","metadata":{"result_type":"recent"},"profile_image_url":"http:\/\/a0.twimg.com\/profile_images\/3074248109\/0fa728912afd324e8722169c4efc3ab2_normal.jpeg","profile_image_url_https":"https:\/\/si0.twimg.com\/profile_images\/3074248109\/0fa728912afd324e8722169c4efc3ab2_normal.jpeg","source":"<a href="http:\/\/twitter.com\/download\/iphone">Twitter for iPhone<\/a>","text":"Fifa anyone? #xbox","to_user":null,"to_user_id":0,"to_user_id_str":"0","to_user_name":null},{"created_at":"Sun, 06 Jan 2013 21:11:02 +0000","from_user":"karlheath6","from_user_id":984240522,"from_user_id_str":"984240522","from_user_name":"Karl heath","geo":null,"id":288029933144518657,"id_str":"288029933144518657","iso_language_code":"en","metadata":{"result_type":"recent"},"profile_image_url":"http:\/\/a0.twimg.com\/profile_images\/3007892972\/4988eca8a918c2fa1b28b48d0f0980df_normal.jpeg","profile_image_url_https":"https:\/\/si0.twimg.com\/profile_images\/3007892972\/4988eca8a918c2fa1b28b48d0f0980df_normal.jpeg","source":"<a href="https:\/\/mobile.twitter.com">Mobile Web (M2)<\/a>","text":"RT #Retroshock316: For a chance 2 win #DirtShowdown for #Xbox, follow #Retroshock316 + RT this. Winner announced when we hit 2k followers. #Comp #Competition","to_user":null,"to_user_id":0,"to_user_id_str":"0","to_user_name":null},{"created_at":"Sun, 06 Jan 2013 21:10:45 +0000","from_user":"SWARNERx","from_user_id":244070634,"from_user_id_str":"244070634","from_user_name":"- Shell Warner\u2717","geo":null,"id":288029861786820609,"id_str":"288029861786820609","iso_language_code":"en","metadata":{"result_type":"recent"},"profile_image_url":"http:\/\/a0.twimg.com\/profile_images\/3071322789\/ba7c82da179f851fea6a4a12ad66c0bd_normal.jpeg","profile_image_url_https":"https:\/\/si0.twimg.com\/profile_images\/3071322789\/ba7c82da179f851fea6a4a12ad66c0bd_normal.jpeg","source":"<a href="http:\/\/twitter.com\/">web<\/a>","text":"back to #xbox .#Blackops2 add me : SWARNERx . #Zombies.","to_user":null,"to_user_id":0,"to_user_id_str":"0","to_user_name":null},{"created_at":"Sun, 06 Jan 2013 21:10:26 +0000","from_user":"headshotcola","from_user_id":1064288336,"from_user_id_str":"1064288336","from_user_name":"Xbox_Headshotcola","geo":null,"id":288029783584022528,"id_str":"288029783584022528","iso_language_code":"en","metadata":{"result_type":"recent"},"profile_image_url":"http:\/\/a0.twimg.com\/sticky\/default_profile_images\/default_profile_6_normal.png","profile_image_url_https":"https:\/\/si0.twimg.com\/sticky\/default_profile_images\/default_profile_6_normal.png","source":"<a href="http:\/\/twitter.com\/download\/android">Twitter for Android<\/a>","text":"People buy #ForzaHorizon it is such a good game best racing game you would #xbox","to_user":null,"to_user_id":0,"to_user_id_str":"0","to_user_name":null},{"created_at":"Sun, 06 Jan 2013 21:10:15 +0000","from_user":"joecurry281186","from_user_id":338231824,"from_user_id_str":"338231824","from_user_name":"Joe Curry","geo":{"coordinates":[52.092676,-1.836255],"type":"Point"},"id":288029737761263616,"id_str":"288029737761263616","iso_language_code":"en","metadata":{"result_type":"recent"},"profile_image_url":"http:\/\/a0.twimg.com\/profile_images\/2402359791\/image_normal.jpg","profile_image_url_https":"https:\/\/si0.twimg.com\/profile_images\/2402359791\/image_normal.jpg","source":"<a href="http:\/\/twitter.com\/download\/iphone">Twitter for iPhone<\/a>","text":"Just deleted a load of content to install FableIII and its still running like crap #xbox #ffs","to_user":null,"to_user_id":0,"to_user_id_str":"0","to_user_name":null}],"results_per_page":15,"since_id":0,"since_id_str":"0"}
I"m trying to display the tweet text in each row of the tableview but it crashes with my current code:
NSDictionary *aTweet = [result objectAtIndex:indexPath.row];
cell.theTweet.text = [aTweet objectForKey:#"text"];
NSLog(#"%#", [aTweet objectForKey:#"text"]);
What am I doing wrong? Any help would be appreciated because I've spent almost all day trying to fix this crash. Thanks.
I figured it out finally. It was in the parsing of the tree. I was parsing it incorrectly. I needed to loop through the tree as follows:
for (NSDictionary *newDictionary in result) {
NSString *tweetNew = [newDictionary objectForKey:#"text"];
}
The JSON Response you are obtaining is valid. So in usual situations there should not be any problems in parsing it.
Personally I am a fan of SBJson for JSON parsing. Please refer this link
http://stig.github.com/json-framework/
Its good if you can map the in coming data to model classes. To map data to model classes you can make use of any KVC wrappers.
Hope this helps.

How do I set the timezone for Perl's localtime()?

In Perl, I'd like to look up the localtime in a specific timezone. I had been using this technique:
$ENV{TZ} = 'America/Los_Angeles';
my $now = scalar localtime;
print "It is now $now\n";
# WORKS: prints the current time in LA
However, this is not reliable -- notably, if I prepend another localtime() call before setting $ENV{TZ}, it breaks:
localtime();
$ENV{TZ} = 'America/Los_Angeles';
my $now = scalar localtime;
print "It is now $now\n";
# FAILS: prints the current time for here instead of LA
Is there a better way to do this?
Use POSIX::tzset.
use POSIX qw(tzset);
my $was = localtime;
print "It was $was\n";
$ENV{TZ} = 'America/Los_Angeles';
$was = localtime;
print "It is still $was\n";
tzset;
my $now = localtime;
print "It is now $now\n";
$ perl -v
This is perl, v5.8.8 built for x86_64-linux-thread-multi
Copyright 1987-2006, Larry Wall
Perl may be copied only under the terms of either the Artistic License or the
GNU General Public License, which may be found in the Perl 5 source kit.
Complete documentation for Perl, including FAQ lists, should be found on
this system using "man perl" or "perldoc perl". If you have access to the
Internet, point your browser at http://www.perl.org/, the Perl Home Page.
$ perl tzset-test.pl
It was Wed Apr 15 15:58:10 2009
It is still Wed Apr 15 15:58:10 2009
It is now Wed Apr 15 12:58:10 2009
I'd strongly suggest using a module to do this. Specifically, I'd suggest using DateTime (see Perl DateTime Wiki or CPAN
Then you should be able to do something like the following:
use strict;
use warnings;
use DateTime;
my $dt = DateTime->now(); # *your* local time assuming your system knows it!
my $clone1 = $dt->clone; # taking a copy.
$clone1->set_time_zone('America/Los_Angeles');
print "$clone1\n"; # output using ISO 8601 format (there a lot of choices)
print "$dt\n";
Whilst your code works fine for me on both Linux (Perl 5.10.0) and MacOS X (5.8.9), there is a possible solution.
The underlying C functons used by Perl (ctime(), localtime(), etc) call tzset() the first time they're invoked, but not necessarily afterwards. By calling it yourself you should ensure that the timezone structures are correctly re-initialised after any change to $TZ.
Fortunately this is easy - the tzset() function is available in the POSIX module:
#!/usr/bin/perl -w
use POSIX qw[tzset];
$ENV{'TZ'} = 'Europe/London';
tzset();
print scalar localtime();
NB: some Google searches suggest that this is only necessary with Perl versions up to and including 5.8.8. Later versions always call tzset() automatically before each call to localtime().
use Time::Zone;
my $TZ = 'America/Los_Angeles';
my $now = scalar localtime time() + tz_offset($TZ);
print "It is now $now\n";
seems to work here. (The 'scalar' is redundant here since $now gives it scalar context, but it's also nice to be explicit.)
As per the comment, I got the original problem. This seems to fix it for me, but given that others aren't having the original problem, the "seems to work here" bit is intended as an invitation for those people to try this solution as well to ensure it doesn't break anything. (I have to wonder if alnitak noticed the difference between what I posted and the original post?)
Expanding on BrianP007 answer you use both TZ and _tzset
$was = localtime;
print "It was $was\n";
$ENV{TZ} = 'CST6CDT'; # America/Chicago
Time::Piece::_tzset(); # Local time is now Chicago Time
$was = localtime;
print "It is $was\n"; # Right now in Chicago
The trick is the TZ is set from your location to GMT. So normally you would think Chicago is UTC-6, but from Chicago it is 6 hours to UTC which = 'CST6'.
See http://science.ksc.nasa.gov/software/winvn/userguide/3_1_4.htm
Executive Summary:
Setting $ENV{TZ}='/*&+000000000005' and calling Time::Piece::_tzset() fixes localtime() to agree with the windoz system clock.
Sanguinarily gory details:
On Strawberry Perl, windoz 7/64, none of the "Standard" time zones works in the TZ environmental variable to localize localtime(). 'America/Chicago' gives exactly the same time as 'America/Los_Angeles' == 'CDT' == 'CST' == 'UTC' == '-01:00', etc. The list is infinite.
Every timezone on http://www.timeanddate.com/time/zones/ that I tried gives the right time if you are in Greenwich.
Every time from: http://en.wikipedia.org/wiki/List_of_tz_database_time_zones
also fails to change localtime() at all. And, there is no apparent indication. They do nothing and say nothing.
There is NO tzset() on windoz:
POSIX::tzset not implemented on this architecture
There is not even any concept of POSIX ???
C:\bin>cpan install POSIX
...
Warning: Cannot install POSIX, don't know what it is.
Try the command i /POSIX/
It appears to be baked into win8 and there are some dot NOT libraries for it.
For Austin, Texas, in the very Center of Central Intergalactic Time, thee correct $ENV{TZ} which gives me a scalar localtime() which ~agrees with the o/s level time function and the windoz clock is:
'/*&+5' !!! Yes Slash-Star-Ampersand-Plus-5 works!
P:\br1\sxsw.2015\sx-2015.0318\done>time
The current time is: 16:36:39.44
...
Time=Apr 14 16:36:42 2015, ENV->TZ=/*&+5
By running a for loop and trying random values from various posts, for Strawberry Perl uname='Win32 strawberry-perl 5.18.2.2...' with known timezone bugs, any 3 chars I tried (didn't try + or -) followed by +/- and a small number worked. Here is an array of text values and their output below:
use Time::Piece;
#tz = ('', 'CDT+5', 'CST+5', 'FKU+5', 'XYZ+5', '+5', '+05', '+05.00',
'America/Chicago', 'America/Los_Angeles', 'CDT',
'CST', 'UTC', 'PDT', 'PST', '-01:00', '+01:00', '-05:00'.
'ACDT', 'EASST', '5000', '+0500', '+5:00', '+05:00', 'SSS+1', 'SSS+0',
'zzz-1', 'ZZ1+5', '123+5', '___+5', '/*&+5', , '/*&+05', '/*&+005',
'/*&+000000000005');
foreach $tz (#tz) {
$ENV{TZ} = $tz if $tz;
Time::Piece::_tzset() if $tz;
printf("T%s, ENV->TZ=%s\n", scalar localtime, $ENV{TZ} || 'NoTZ');
}
Most every try with anything but XXX . +|- . integer gave UTC, but many were an hour off for no reason (America/Los_Angeles and America/Chicago gave the same value). I am almost sure I used to get away with just CDT and CST, possibly on Activestate (switched to Strawberry to compile my own Perl modules rather than rely on Activestate for everything). This is the first major snarl.
I rebuilt DateTime from scratch and it worked fine. DateTime::TimeZone::Local::Win32 "failed for 'Win32::TieRegistry'"
Here's the sorted result of the attempted zones above:
P:\br1\sxsw.2015\sx-2015.0318\done>bb | sort
Running c:/bin/bb.pl Tue Apr 14 21:43:56 2015
TTue Apr 14 16:43:56 2015, ENV->TZ=/*&+000000000005
TTue Apr 14 16:43:56 2015, ENV->TZ=/*&+005
TTue Apr 14 16:43:56 2015, ENV->TZ=/*&+05
TTue Apr 14 16:43:56 2015, ENV->TZ=/*&+5
TTue Apr 14 16:43:56 2015, ENV->TZ=___+5
TTue Apr 14 16:43:56 2015, ENV->TZ=123+5
TTue Apr 14 16:43:56 2015, ENV->TZ=CDT+5
TTue Apr 14 16:43:56 2015, ENV->TZ=CST+5
TTue Apr 14 16:43:56 2015, ENV->TZ=FKU+5
TTue Apr 14 16:43:56 2015, ENV->TZ=XYZ+5
TTue Apr 14 16:43:56 2015, ENV->TZ=ZZ1+5
ABOVE ALL WORKED Below most failed with UTC or +1 hour???
TTue Apr 14 20:43:56 2015, ENV->TZ=SSS+1
TTue Apr 14 21:43:56 2015, ENV->TZ=-01:00
TTue Apr 14 21:43:56 2015, ENV->TZ=+01:00
TTue Apr 14 21:43:56 2015, ENV->TZ=+05
TTue Apr 14 21:43:56 2015, ENV->TZ=+05:00
TTue Apr 14 21:43:56 2015, ENV->TZ=+0500
TTue Apr 14 21:43:56 2015, ENV->TZ=+5
TTue Apr 14 21:43:56 2015, ENV->TZ=+5:00
TTue Apr 14 21:43:56 2015, ENV->TZ=5000
TTue Apr 14 21:43:56 2015, ENV->TZ=CDT
TTue Apr 14 21:43:56 2015, ENV->TZ=CDT
TTue Apr 14 21:43:56 2015, ENV->TZ=CST
TTue Apr 14 21:43:56 2015, ENV->TZ=PDT
TTue Apr 14 21:43:56 2015, ENV->TZ=PST
TTue Apr 14 21:43:56 2015, ENV->TZ=SSS+0
TTue Apr 14 21:43:56 2015, ENV->TZ=UTC
TTue Apr 14 22:43:56 2015, ENV->TZ=-05:00ACDT
TTue Apr 14 22:43:56 2015, ENV->TZ=+05.00
TTue Apr 14 22:43:56 2015, ENV->TZ=America/Chicago
TTue Apr 14 22:43:56 2015, ENV->TZ=America/Los_Angeles
TTue Apr 14 22:43:56 2015, ENV->TZ=EASST
TTue Apr 14 22:43:56 2015, ENV->TZ=zzz-1
Even after finding and installing the Holy Grail, the TzFile module for the Olsen Database, it is still screwed, no difference!
Installing C:\bin\strawberry_perl_5_18\perl\site\lib\DateTime\TimeZone\Tzfile.pm
ZEFRAM/DateTime-TimeZone-Tzfile-0.010.tar.gz
C:\bin\strawberry_perl_5_18\perl\bin\perl.exe ./Build install --uninst 1 -- OK
Here are all of the alleged timezones which do nothing on this platform from:
#atz = DateTime::TimeZone->all_names();
printf("All tz names [%d] = %s\n", scalar #atz, join(", ", #atz));
All tz names [349] = Africa/Abidjan, Africa/Accra, Africa/Algiers, Africa/Bissau, Africa/Cairo, Africa/Casablanca, Africa/Ceuta, Africa/El_Aaiun, Africa/Johannesburg, Africa/Khartoum, Africa/Lagos, Africa/Maputo, Africa/Monrovia, Africa/Nairobi, Africa/Ndjamena, Africa/Tripoli, Africa/Tunis, Africa/Windhoek, America/Adak, America/Anchorage, America/Araguaina, America/Argentina/Buenos_Aires, America/Argentina/Catamarca, America/Argentina/Cordoba, America/Argentina/Jujuy, America/Argentina/La_Rioja, America/Argentina/Mendoza, America/Argentina/Rio_Gallegos, America/Argentina/Salta, America/Argentina/San_Juan, America/Argentina/San_Luis, America/Argentina/Tucuman, America/Argentina/Ushuaia, America/Asuncion, America/Atikokan, America/Bahia, America/Bahia_Banderas, America/Barbados, America/Belem, America/Belize, America/Blanc-Sablon, America/Boa_Vista, America/Bogota, America/Boise, America/Cambridge_Bay, America/Campo_Grande, America/Cancun, America/Caracas, America/Cayenne, America/Chicago, America/Chihuahua, America/Costa_Rica, America/Creston, America/Cuiaba, America/Curacao, America/Danmarkshavn, America/Dawson, America/Dawson_Creek, America/Denver, America/Detroit, America/Edmonton, America/Eirunepe, America/El_Salvador, America/Fortaleza, America/Glace_Bay, America/Godthab, America/Goose_Bay, America/Grand_Turk, America/Guatemala, America/Guayaquil, America/Guyana, America/Halifax, America/Havana, America/Hermosillo, America/Indiana/Indianapolis, America/Indiana/Knox, America/Indiana/Marengo, America/Indiana/Petersburg, America/Indiana/Tell_City, America/Indiana/Vevay, America/Indiana/Vincennes, America/Indiana/Winamac, America/Inuvik, America/Iqaluit, America/Jamaica, America/Juneau, America/Kentucky/Louisville, America/Kentucky/Monticello, America/La_Paz, America/Lima, America/Los_Angeles, America/Maceio, America/Managua, America/Manaus, America/Martinique, America/Matamoros, America/Mazatlan, America/Menominee, America/Merida, America/Metlakatla, America/Mexico_City, America/Miquelon, America/Moncton, America/Monterrey, America/Montevideo, America/Montreal, America/Nassau, America/New_York, America/Nipigon, America/Nome, America/Noronha, America/North_Dakota/Beulah, America/North_Dakota/Center, America/North_Dakota/New_Salem, America/Ojinaga, America/Panama, America/Pangnirtung, America/Paramaribo, America/Phoenix, America/Port-au-Prince, America/Port_of_Spain, America/Porto_Velho, America/Puerto_Rico, America/Rainy_River, America/Rankin_Inlet, America/Recife, America/Regina, America/Resolute, America/Rio_Branco, America/Santa_Isabel, America/Santarem, America/Santiago, America/Santo_Domingo, America/Sao_Paulo, America/Scoresbysund, America/Sitka, America/St_Johns, America/Swift_Current, America/Tegucigalpa, America/Thule, America/Thunder_Bay, America/Tijuana, America/Toronto, America/Vancouver, America/Whitehorse, America/Winnipeg, America/Yakutat, America/Yellowknife, Antarctica/Casey, Antarctica/Davis, Antarctica/DumontDUrville, Antarctica/Macquarie, Antarctica/Mawson, Antarctica/Palmer, Antarctica/Rothera, Antarctica/Syowa, Antarctica/Troll, Antarctica/Vostok, Asia/Almaty, Asia/Amman, Asia/Anadyr, Asia/Aqtau, Asia/Aqtobe, Asia/Ashgabat, Asia/Baghdad, Asia/Baku, Asia/Bangkok, Asia/Beirut, Asia/Bishkek, Asia/Brunei, Asia/Chita, Asia/Choibalsan, Asia/Colombo, Asia/Damascus, Asia/Dhaka, Asia/Dili, Asia/Dubai, Asia/Dushanbe, Asia/Gaza, Asia/Hebron, Asia/Ho_Chi_Minh, Asia/Hong_Kong, Asia/Hovd, Asia/Irkutsk, Asia/Jakarta, Asia/Jayapura, Asia/Jerusalem, Asia/Kabul, Asia/Kamchatka, Asia/Karachi, Asia/Kathmandu, Asia/Khandyga, Asia/Kolkata, Asia/Krasnoyarsk, Asia/Kuala_Lumpur, Asia/Kuching, Asia/Macau, Asia/Magadan, Asia/Makassar, Asia/Manila, Asia/Nicosia, Asia/Novokuznetsk, Asia/Novosibirsk, Asia/Omsk, Asia/Oral, Asia/Pontianak, Asia/Pyongyang, Asia/Qatar, Asia/Qyzylorda, Asia/Rangoon, Asia/Riyadh, Asia/Sakhalin, Asia/Samarkand, Asia/Seoul, Asia/Shanghai, Asia/Singapore, Asia/Srednekolymsk, Asia/Taipei, Asia/Tashkent, Asia/Tbilisi, Asia/Tehran, Asia/Thimphu, Asia/Tokyo, Asia/Ulaanbaatar, Asia/Urumqi, Asia/Ust-Nera, Asia/Vladivostok, Asia/Yakutsk, Asia/Yekaterinburg, Asia/Yerevan, Atlantic/Azores, Atlantic/Bermuda, Atlantic/Canary, Atlantic/Cape_Verde, Atlantic/Faroe, Atlantic/Madeira, Atlantic/Reykjavik, Atlantic/South_Georgia, Atlantic/Stanley, Australia/Adelaide, Australia/Brisbane, Australia/Broken_Hill, Australia/Currie, Australia/Darwin, Australia/Eucla, Australia/Hobart, Australia/Lindeman, Australia/Lord_Howe, Australia/Melbourne, Australia/Perth, Australia/Sydney, CET, CST6CDT, EET, EST, EST5EDT, Europe/Amsterdam, Europe/Andorra, Europe/Athens, Europe/Belgrade, Europe/Berlin, Europe/Brussels, Europe/Bucharest, Europe/Budapest, Europe/Chisinau, Europe/Copenhagen, Europe/Dublin, Europe/Gibraltar, Europe/Helsinki, Europe/Istanbul, Europe/Kaliningrad, Europe/Kiev, Europe/Lisbon, Europe/London, Europe/Luxembourg, Europe/Madrid, Europe/Malta, Europe/Minsk, Europe/Monaco, Europe/Moscow, Europe/Oslo, Europe/Paris, Europe/Prague, Europe/Riga, Europe/Rome, Europe/Samara, Europe/Simferopol, Europe/Sofia, Europe/Stockholm, Europe/Tallinn, Europe/Tirane, Europe/Uzhgorod, Europe/Vienna, Europe/Vilnius, Europe/Volgograd, Europe/Warsaw, Europe/Zaporozhye, Europe/Zurich, HST, Indian/Chagos, Indian/Christmas, Indian/Cocos, Indian/Kerguelen, Indian/Mahe, Indian/Maldives, Indian/Mauritius, Indian/Reunion, MET, MST, MST7MDT, PST8PDT, Pacific/Apia, Pacific/Auckland, Pacific/Bougainville, Pacific/Chatham, Pacific/Chuuk, Pacific/Easter, Pacific/Efate, Pacific/Enderbury, Pacific/Fakaofo, Pacific/Fiji, Pacific/Funafuti, Pacific/Galapagos, Pacific/Gambier, Pacific/Guadalcanal, Pacific/Guam, Pacific/Honolulu, Pacific/Kiritimati, Pacific/Kosrae, Pacific/Kwajalein, Pacific/Majuro, Pacific/Marquesas, Pacific/Nauru, Pacific/Niue, Pacific/Norfolk, Pacific/Noumea, Pacific/Pago_Pago, Pacific/Palau, Pacific/Pitcairn, Pacific/Pohnpei, Pacific/Port_Moresby, Pacific/Rarotonga, Pacific/Tahiti, Pacific/Tarawa, Pacific/Tongatapu, Pacific/Wake, Pacific/Wallis, UTC, WET