mardi 4 août 2015

Pulling posts & messages from the last year on facebook for project

I want to try and pull posts and messages from the last year from Facebook for my project. I've managed to pull the data, but I don't know how I would pull it for the last 365 days specifically.

Here's my code so far:

#Modules
import requests
import facebook

def some_action(post):
    print posts['data']
    print post['created_time']

#Token
access_token = '...'
user = 'walkers'

#Posts
graph = facebook.GraphAPI(access_token)
profile = graph.get_object(user)
posts = graph.get_connections(profile['id'], 'posts')

x = 0
while x < 10:
    #while True:
    try:
        posts = requests.get(posts['paging']['next']).json()
        #print posts

    except KeyError:
        break
    x = x+1

#Write
f = open("111.txt", "w")
f.write (str(posts) + "\n")
f.close()

And here's some of the the output:

{u'paging': {u'next': u'http://ift.tt/1KNIaK1', u'previous': u'http://ift.tt/1ImpzPx'}, u'data': [{u'picture': u'http://ift.tt/1KNIaK3', u'story': u'Walkers added 8 new photos to the album: 20 Years of Gary.', u'likes': {u'paging': {u'cursors': {u'after': u'MzgxNzk1MDQ4NjQ3MTI3', u'before': u'MTAxNTI0OTY1MDIwNDIyODI='}}, u'data': [{u'id': u'10152496502042282', u'name': u'Aaron Hanson'}, {u'id': u'10203040513950876', u'name': u'Gary GazRico Hinchliffe'}, {u'id': u'10152934096109345', u'name': u'Stuart Collister'}, {u'id': u'10152297022606059', u'name': u'Helen Preston'}, {u'id': u'326285380900188', u'name': u'Rhys Edwards'}, {u'id': u'10204744346589601', u'name': u'Aaron Benfield'}, {u'id': u'10200910780691953', u'name': u'Mike S Q Wilkins'}, {u'id': u'10204902354187051', u'name': u'Paul Owen Davies'}, {u'id': u'10152784755311784', u'name': u'Dafydd Ifan'}, {u'id': u'1517704468487365', u'name': u'Stephen Collier'}, {u'id': u'10202198826115234', u'name': u'John McKellar'}, {u'id': u'10151949129487143', u'name': u'Lucy Morrison'}, {u'id': u'1474199509524133', u'name': u'Christine Leek'}, {u'id': u'381795048647127', u'name': u'Sandra Taylor'}]}, u'from': {u'category': u'Product/Service', u'name': u'Walkers', u'id': u'53198517648'}, u'name': u'20 Years of Gary', u'is_hidden': False, u'privacy': {u'allow': u'', u'deny': u'', u'friends': u'', u'description': u'', u'value': u''}, u'is_expired': False, u'actions': [{u'link': u'http://ift.tt/1ImpBH5', u'name': u'Comment'}, {u'link': u'http://ift.tt/1ImpBH5', u'name': u'Like'}], u'updated_time': u'2015-05-07T11:13:26+0000', u'caption': u'To celebrate Gary\u2019s 20 years as the face of Walkers, we\u2019re recreating some scenes from his first ad in the ultimate #TBT! The cast have waited two decades for this sequel. #CantHelpButSmile', u'link': u'http://ift.tt/1KNIaK5', u'object_id': u'10153202855322649', u'shares': {u'count': 71}, u'story_tags': {u'0': [{u'length': 7, u'offset': 0, u'type': u'page', u'id': u'53198517648', u'name': u'Walkers'}]}, u'created_time': u'2015-05-07T11:13:26+0000', u'message': u'To celebrate Gary\u2019s 20 years as the face of Walkers, we\u2019re recreating some scenes from his first ad in the ultimate #TBT! The cast have waited two decades for this sequel. #CantHelpButSmile', u'type': u'photo', u'id': u'53198517648_10153202855322649', u'status_type': u'added_photos', u'icon': u'http://ift.tt/1JLaV6n'}, {u'picture': u'http://ift.tt/1KNI9pv', u'is_hidden': False, u'likes': {u'paging': {u'cursors': {u'after': u'NjIwNTUxMDg0NzAwODgx', u'before': u'Njk5NjczMDQzNDYxODgx'}, u'next': u'http://ift.tt/1ImpzPz'}, u'data': [{u'id': u'699673043461881', u'name': u'Summer Louise Lee'}, {u'id': u'778869825481375', u'name': u'Princess Nicole-sunny \u015eenyurt'}, {u'id': u'10203535327456872', u'name': u'Tamtam Harbi'}, {u'id': u'830511573639656', u'name': u'Jennifer Baker'}, {u'id': u'721613451224404', u'name': u'Ana Monroy'}, {u'id': u'383509561823365', u'name': u'Sheila Baud'}, {u'id': u'383392095146766', u'name': u'Gemma Fletcher'}, {u'id': u'10100758061580924', u'name': u'Rosalind Lee'}, {u'id': u'10152407789928449', u'name': u'Louise Allen'}, {u'id': u'10153154705875787', u'name': u'Tracey Hunt'}, {u'id': u'1570440839870835', u'name': u'Christine Bray'}, {u'id': u'462703947225536', u'name': u'David Williams'}, {u'id': u'1421406008149892', u'name': u'Caitlyn Molyneux'}, {u'id': u'10152538544760959', u'name': u'Hima Thanki'}, {u'id': u'608844039202546', u'name': u'Sara Veiga'}, {u'id': u'1692079987687260', u'name': u'Fraser George Stevens'}, {u'id': u'1410872812543572', u'name': u'Abby Sanderson'}, {u'id': u'10152954172786298', u'name': u'Jasmin Gosalia'}, {u'id': u'10204166664027765', u'name': u'Anthony Millward'}, {u'id': u'10152480783622038', u'name': u'Rachel Holms'}, {u'id': u'10202286549805262', u'name': u'Farzana Kausar'}, {u'id': u'787359004643685', u'name': u'Hayley Green'}, {u'id': u'305329966329014', u'name': u'Jemma Bailey'}, {u'id': u'972264679466809', u'name': u'Margaret Catley'}, {u'id': u'620551084700881', u'name': u'Suzanne Wrigley'}]}, u'from': {u'category': u'Product/Service', u'name': u'Walkers', u'id': u'53198517648'}, u'comments': {u'paging': {u'cursors': {u'after': u'MQ==', u'before': u'MQ=='}}, u'data': [{u'from': {u'name': u'David Williams', u'id': u'462703947225536'}, u'like_count': 0, u'can_remove': False, u'created_time': u'2015-05-03T08:57:05+0000', u'message': u'Yes takes me back, not to the pram , but when I could eat crisps with out worrying about my weight.\U0001f612', u'id': u'10153192551122649_10153194592792649', u'user_likes': False}]}, u'privacy': {u'allow': u'', u'deny': u'', u'friends': u'', u'description': u'', u'value': u''}, u'is_expired': False, u'actions': [{u'link': u'http://ift.tt/1KNIaK7', u'name': u'Comment'}, {u'link': u'http://ift.tt/1KNIaK7', u'name': u'Like'}], u'properties': [{u'text': u'00:07', u'name': u'Length'}], u'source': u'http://ift.tt/1ImpBHd', u'link': u'http://ift.tt/1KNI9px', u'object_id': u'10153192551122649', u'shares': {u'count': 10}, u'created_time': u'2015-05-02T11:17:49+0000', u'message': u'#CantHelpButSmile', u'updated_time': u'2015-05-03T08:57:05+0000', u'type': u'video', u'id': u'53198517648_10153192551122649', u'status_type': u'added_video', u'icon': u'http://ift.tt/1JLaVTY'}, {u'picture': u'http://ift.tt/1KNI9pz', u'is_hidden': False, u'likes': {u'paging': {u'cursors': {u'after': u'ODc1MDc4MDc1ODQxNzg4', u'before': u'Nzc4ODY5ODI1NDgxMzc1'}, u'next': u'http://ift.tt/1ImpBXv'}, u'data': [{u'id': u'778869825481375', u'name': u'Princess Nicole-sunny \u015eenyurt'}, {u'id': u'10203535327456872', u'name': u'Tamtam Harbi'}, {u'id': u'842151215867368', u'name': u'Waqas Javid'}, {u'id': u'925564307454778', u'name': u'Steph Louise'}, {u'id': u'10152641983603000', u'name': u'Harry Vincent'}, {u'id': u'513166088828812', u'name': u'Dillon Morgan-Jones'}, {u'id': u'1029234627087388', u'name': u'Alison Richards'}, {u'id': u'10152636466428548', u'name': u'Elin Mai Jones'}, {u'id': u'1791653647727554', u'name': u'Cameron Ward'}, {u'id': u'1035254736490945', u'name': u'Umer Choudhary'}, {u'id': u'1571886136400010', u'name': u'Liam Llewellyn'}, {u'id': u'10205154376362734', u'name': u'Kate Neesham'}, {u'id': u'10152034590826596', u'name': u'Tim Allan'}, {u'id': u'550381381747086', u'name': u'Stephanie Tracey Stubbs Fry'}, {u'id': u'10154569916710635', u'name': u'Becky Cronin'}, {u'id': u'10153110708653885', u'name': u'Jason Plastow'}, {u'id': u'10203270854452666', u'name': u'Sean Uprichard'}, {u'id': u'778773632211715', u'name': u'Shohel Rana'}, {u'id': u'10200984087444257', u'name': u'Cally Wilson'}, {u'id': u'10152010672247327', u'name': u'Daniel Newbie Newman'}, {u'id': u'862886047084648', u'name': u'Niamh Angel Hodge'}, {u'id': u'10152670558197305', u'name': u'Steven Dixon'}, {u'id': u'4788216560078', u'name': u'Marcin Franke'}, {u'id': u'615901501858007', u'name': u'Arham Chaudhry'}, {u'id': u'875078075841788', u'name': u'Jordon Adams'}]}, u'from': {u'category': u'Product/Service', u'name': u'Walkers', u'id': u'53198517648'}, u'comments': {u'paging': {u'cursors': {u'after': u'MQ==', u'before': u'OA=='}}, u'data': [{u'from': {u'name': u'Mark Brown', u'id': u'10154058875275543'}, u'like_count': 6, u'can_remove': False, u'created_time': u'2015-05-02T08:52:30+0000', u'message': u'This looks absolutely dreadful', u'id': u'10153186245247649_10153192311622649', u'user_likes': False}, {u'from': {u'name': u'Jonny Nicholson', u'id': u'753425971376851'}, u'like_count': 1, u'can_remove': False, u'created_time': u'2015-05-02T21:06:15+0000', u'message': u'when will you be making full bag?', u'id': u'10153186245247649_10153193614087649', u'user_likes': False}, {u'from': {u'name': u'Emma Louise Webb', u'id': u'10152574201891007'}, u'like_count': 0, u'can_remove': False, u'created_time': u'2015-05-02T18:01:03+0000', u'message': u'Wow amazing', u'id': u'10153186245247649_10153193237142649', u'user_likes': False}, {u'from': {u'name': u'Thom Williams', u'id': u'10153528602434310'}, u'like_count': 0, u'can_remove': False, u'created_time': u'2015-05-03T12:57:49+0000', u'message': u'Jesus christ', u'id': u'10153186245247649_10153194928492649', u'user_likes': False}, {u'from': {u'name': u'Mattie Bradshaw', u'id': u'10154017626580117'}, u'like_count': 0, u'can_remove': False, u'created_time': u'2015-05-03T14:37:20+0000', u'message': u'Tragic marketing', u'id': u'10153186245247649_10153195099017649', u'user_likes': False}, {u'from': {u'name': u'Loriley Sessions', u'id': u'10152696432105155'}, u'message_tags': [{u'length': 10, u'offset': 0, u'type': u'user', u'id': u'10154384070225403', u'name': u'James Clee'}], u'like_count': 0, u'can_remove': False, u'created_time': u'2015-04-29T16:19:03+0000', u'message': u'James Clee ya main man', u'id': u'10153186245247649_10153186337617649', u'user_likes': False}]}, u'privacy': {u'allow': u'', u'deny': u'', u'friends': u'', u'description': u'', u'value': u''}, u'is_expired': False, u'actions': [{u'link': u'http://ift.tt/1KNI9pB', u'name': u'Comment'}, {u'link': u'http://ift.tt/1KNI9pB', u'name': u'Like'}], u'properties': [{u'text': u'00:39', u'name': u'Length'}], u'source': u'http://ift.tt/1ImpA5T', u'link': u'http://ift.tt/1KNIaKb', u'object_id': u'10153186245247649', u'shares': {u'count': 4}, u'created_time': u'2015-04-29T16:00:01+0000', u'message': u'Need a laugh? Here\u2019s our latest Snackdown podcast, a monthly roundup of news with a group of comedy friends. #CantHelpButSmile http://apple.co/1dsPrk0', u'updated_time': u'2015-04-29T16:00:01+0000', u'type': u'video', u'id': u'53198517648_10153186245247649', u'status_type': u'added_video', u'icon': u'http://ift.tt/1JLaVTY'}, {u'picture': u'http://ift.tt/1ImpA5V', u'is_hidden': False, u'likes': {u'paging': {u'cursors': {u'after': u'NjM0MzMyOTczMzU5NDI0', u'before': u'MTM4MzY5MDcwMTkzMjAzMg=='}, u'next': u'http://ift.tt/1KNIaKd'}, u'data': [{u'id': u'1383690701932032', u'name': u'Phil Bonsall'}, {u'id': u'778869825481375', u'name': u'Princess Nicole-sunny \u015eenyurt'}, {u'id': u'10203535327456872', u'name': u'Tamtam Harbi'}, {u'id': u'10152575804559808', u'name': u'Justine Edwards'}, {u'id': u'775351169210278', u'name': u'Nathan Evans'}, {u'id': u'808059839312508', u'name': u'Tanya Houghton'}, {u'id': u'398300857023260', u'name': u'Angel Jupe'}, {u'id': u'511529258982389', u'name': u'Amyy James'}, {u'id': u'528646077244967', u'name': u'Finley Rowland'}, {u'id': u'333791050140805', u'name': u'Usman Ali'}, {u'id': u'325500274322410', u'name': u'Shaneil Swaby'}, {u'id': u'731784150271691', u'name': u'Abi Meadows'}, {u'id': u'514048395438260', u'name': u'Erin Tylor Ava Johnson'}, {u'id': u'1475264966125569', u'name': u'Teresa Stubbs'}, {u'id': u'1562679640663006', u'name': u'Kira Dunford'}, {u'id': u'10203874163608982', u'name': u'Matt Reoch'}, {u'id': u'647680085337571', u'name': u'Miszz D Ox'}, {u'id': u'373314132842801', u'name': u'Aoife Mc Erlean'}, {u'id': u'684877051580321', u'name': u'Rajwinder Dhillon'}, {u'id': u'10155891196985346', u'name': u'Joanna Arnold'}, {u'id': u'10155155762375301', u'name': u"Ali 'Als' Moore"}, {u'id': u'10154103048425192', u'name': u'Jolene Chalk'}, {u'id': u'476069229195785', u'name': u'Mollie Ashfield'}, {u'id': u'607143526096436', u'name': u'Liam Costa'}, {u'id': u'634332973359424', u'name': u'Chadley Peakman'}]}, u'from': {u'category': u'Product/Service', u'name': u'Walkers', u'id': u'53198517648'}, u'comments': {u'paging': {u'cursors': {u'after': u'ODI=', u'before': u'MTA2'}, u'next': u'http://ift.tt/1ImpBXF'}, u'data': [{u'from': {u'name': u'Jack Reacher', u'id': u'10152949153527863'}, u'like_count': 46, u'can_remove': False, u'created_time': u'2015-04-26T10:32:19+0000', u'message': u'It would have to be a very small sandwich if we were to use a bag of walkers on it!!!', u'id': u'10153170723582649_10153177520892649', u'user_likes': False}, {u'from': {u'name': u'Meg Awuah', u'id': u'641625565957901'}, u'like_count': 0, u'can_remove': False, u'created_time': u'2015-04-27T19:05:22+0000', u'message': u'I won a free lunch \U0001f44c\U0001f3fb\nThanks Walkers', u'id': u'10153170723582649_10153181235597649', u'user_likes': False}, {u'from': {u'name': u'Kyle Thomas Spence', u'id': u'10152444873003458'}, u'like_count': 0, u'can_remove': False, u'created_time': u'2015-04-28T22:06:47+0000', u'message': u'How about a 1 in 6 chance of crisps in the bag? Would be a nice change.', u'id': u'10153170723582649_10153184604542649', u'user_likes': False}, {u'from': {u'name': u'Steve Hewitt', u'id': u'10205646534593447'}, u'like_count': 23, u'can_remove': False, u'created_time': u'2015-04-27T16:52:37+0000', u'message': u'A full packet would do for me!', u'id': u'10153170723582649_10153180951107649', u'user_likes': False}, {u'from': {u'name': u'Maxine Seller', u'id': u'1572191589662328'}, u'like_count': 1, u'can_remove': False, u'created_time': u'2015-04-27T11:51:45+0000', u'message': u'Iv won 3 vouchers so far \U0001f60a thankyou walkers \U0001f496', u'id': u'10153170723582649_10153180275017649', u'user_likes': False}, {u'from': {u'name': u'Kayleigh Bowe', u'id': u'1206523482707595'}, u'like_count': 0, u'can_remove': False, u'created_time': u'2015-04-29T16:58:45+0000', u'message': u'Was just wondering what banana flavoured walkers crisps would taste like x hmmm x mmmm! X lol', u'id': u'10153170723582649_10153186403352649', u'user_likes': False}, {u'from': {u'name': u'Jenny Wark', u'id': u'10153197354099670'}, u'like_count': 3, u'can_remove': False, u'created_time': u'2015-04-27T07:54:26+0000', u'message': u'Crisps on a a sandwich is just WRONG!!!!!!!!\U0001f61d', u'id': u'10153170723582649_10153179966542649', u'user_likes': False}, {u'from': {u'name': u'Lisa Watson', u'id': u'297450463798269'}, u'like_count': 0, u'can_remove': False, u'created_time': u'2015-04-28T20:14:07+0000', u'message': u'I like strawberry jam sandwich  with a bag of cheese and onion crisps.  x', u'id': u'10153170723582649_10153184252522649', u'user_likes': False}, {u'from': {u'name': u'Alison Largue', u'id': u'10152452678791016'}, u'like_count': 0, u'can_remove': False, u'created_time': u'2015-04-27T14:57:16+0000', u'message': u'Banana with salt and vinegar crisps :-) or cheddar cheese with prawn cocktail crisps. Sandwiches have to be cut into squares though :-D', u'id': u'10153170723582649_10153180732142649', u'user_likes': False}, {u'from': {u'name': u'Stacey Jayne Goodwin', u'id': u'10154913080295714'}, u'like_count': 18, u'can_remove': False, u'created_time': u'2015-04-27T10:21:16+0000', u'message': u'Bring us back nom nom', u'id': u'10153170723582649_10153180140847649', u'user_likes': False}, {u'from': {u'name': u'Siobhan Michelle Hishon', u'id': u'10204461658164675'}, u'like_count': 0, u'can_remove': False, u'created_time': u'2015-04-27T12:52:13+0000', u'message': u'I used to love jam sandwiches with cheese and onion crisps tastes lovely', u'id': u'10153170723582649_10153180466617649', u'user_likes': False}...

As you can see there is a date in the code, but I doubt this would be recognized as a date? Best thing I could do is perhaps specify today's date and say pull from a year before this date. But I'm not sure how I would do this. Does anyone know how to do this?

Aucun commentaire:

Enregistrer un commentaire