tuitviewer/tuitsview.py
tuitsview.pyRaw
1# -*- coding: utf-8 -*-
2
3import sys
4import tweepy
5import os
6import xml.etree.cElementTree as ET
7
8consumer_key = 'XXXXXXX'
9consumer_secret = 'XXXXXXX'
10access_token = 'XXXXXXX'
11access_secret = 'XXXXXXX'
12
13
14auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
15auth.set_access_token(access_token, access_secret)
16
17
18api = tweepy.API(auth)
19
20#gets the last twits que veuria l'usuari al home
21##display homepage tweets of authenticated user
22for i in range(0, 5):
23 print(" ")
24print("...........---------/////////TL\\\\\\\\-------............")
25print(" ")
26
27root = ET.Element("root")
28llistatuits = ET.SubElement(root, "llistatuits")
29
30public_tweets = api.home_timeline(count=90)
31for tweet in reversed(public_tweets):
32 print("::::::::::::::::")
33 hora= tweet.created_at.strftime('%H')
34 #horari d'estiu
35 hora= int(hora) + 2
36 emisor= tweet.user.name + " - @" + tweet.user.screen_name
37 print(emisor + " -" + str(hora) + ":" + tweet.created_at.strftime('%M:%S'))
38 #continguttuit=tweet.text.encode('latin-1', 'ignore').decode('utf-8', 'ignore')
39 continguttuit=tweet.text.encode('latin-1', 'ignore').decode('utf-8', 'ignore')
40 print (continguttuit)
41 nretuits=str(tweet.retweet_count)
42 print (nretuits)
43 horatuit= str(hora) + ":" + tweet.created_at.strftime('%M:%S')
44 if 'media' in tweet.entities:
45 for image in tweet.entities['media']:
46 imatgetuit= image['media_url']
47 ET.SubElement(llistatuits, "tuit", de=emisor, retuits=nretuits, hora=horatuit, imatge=imatgetuit).text = str(continguttuit)
48 else:
49 ET.SubElement(llistatuits, "tuit", de=emisor, retuits=nretuits, hora=horatuit).text = str(continguttuit)
50
51
52tree = ET.ElementTree(root)
53tree.write("dadestw.xml")
54
55print(" ")
56
57#OPCIÓ QUE OBRE AUTOMÀTICAMENT UNA FINESTRA DEL NAVEGADOR PER VISUALITZAR ELS TWEETS
58#import webbrowser
59#webbrowser.open_new('index.html')