diff --git a/Project design/Database design/database.sql b/Project design/Database design/database.sql index 6c4268b..29b303c 100644 --- a/Project design/Database design/database.sql +++ b/Project design/Database design/database.sql @@ -12,8 +12,7 @@ CREATE TABLE moderators( CREATE TABLE twitterberichten( twitterid bigint PRIMARY KEY, - datumpublished date NOT NULL, - tijdpublished time NOT NULL + datumpublished timestamp NOT NULL ); CREATE TABLE statuses( diff --git a/moderator.py b/moderator.py index a5f1cf8..827e203 100644 --- a/moderator.py +++ b/moderator.py @@ -1,9 +1,9 @@ import hashlib import getpass import database_connector -import api_keys +from api_keys import * import datetime -import TwitterAPI +from TwitterAPI import TwitterAPI conn = database_connector.conn cur = database_connector.cur @@ -64,6 +64,39 @@ def publishtwit(berichtid): waarden die twitter mee heeft gegeven zoals: id, tijd, datum na het successfol publishen gaat de bericht status naar published(5) """ + twitter_API = TwitterAPI(TWITTER_APIKEY, TWITTER_APISECRET, TWITTER_ACCESSTOKEN, TWITTER_ACCESSSECRET) + + cur.execute("SELECT * FROM berichten WHERE berichtid = (%s)", [berichtid]) + bericht = cur.fetchone() + + cur.execute("SELECT naam FROM locaties WHERE locatieid = (%s)", [bericht[9]]) + locatie = cur.fetchone() + + formatted = str(bericht[1] + ' | Door ' + bericht[4] + ' op ' + locatie[0]) + + if len(formatted) < 180: + response = twitter_API.request('statuses/update', {'status': formatted}) + jsonresponse = response.json() + if response.status_code == 200: + # vul tabel twitterberichten eerst in + QUERY = 'INSERT INTO twitterberichten(twitterid, datumpublished) VALUES (%s, %s)' + DATA = (jsonresponse['id'], jsonresponse['created_at']) + cur.execute(QUERY, DATA) + + # link twitterberichten met bericht in berichten tabel + QUERY = 'UPDATE berichten SET twitterid = (%s) WHERE berichtid = (%s)' + DATA = (jsonresponse['id'], berichtid) + cur.execute(QUERY, DATA) + conn.commit() + return True + else: + print('Probleem met het publishen naar twitter') + return False + else: + print('bericht is te lang') + return False + + def getmessage(moderatorid): """ @@ -139,10 +172,22 @@ def moderateloop(): try: modinput = input('r = reject, a = accept, q = quit: ').lower() if modinput == 'r': - rejectmessage(bericht[0], moderatorid) + if rejectmessage(bericht[0], moderatorid): + print('Moderatie opgeslagen') + else: + print('Probleem met het opslaan van de moderatie') exitvar = True elif modinput == 'a': - acceptmessage(bericht[0], moderatorid) + if acceptmessage(bericht[0], moderatorid): + print('Bericht successvol opgeslagen in database') + else: + print('Probleem met het opslaan van de moderatie.') + + if publishtwit(bericht[0]): + print('Bericht is successvol gepubliceerd op twitter') + else: + print('Probleem met het publiceren op twitter') + exitvar = True elif modinput == 'q': unlockmessage(bericht[0]) diff --git a/publish.py b/publish.py index 0ac5526..325d63f 100644 --- a/publish.py +++ b/publish.py @@ -16,12 +16,12 @@ from api_keys import * -#twitter_API = TwitterAPI(TWITTER_APIKEY, TWITTER_APISECRET, TWITTER_ACCESSTOKEN, TWITTER_ACCESSSECRET) +twitter_API = TwitterAPI(TWITTER_APIKEY, TWITTER_APISECRET, TWITTER_ACCESSTOKEN, TWITTER_ACCESSSECRET) -#r = twitter_API.request('statuses/update', {'status':'Another tweet'}) -#print(r.status_code) -#print(r.json()) +r = twitter_API.request('statuses/update', {'status':'Another test tweet'}) +print(r.status_code) +print(r.json()) -test = {'created_at': 'Tue Sep 29 20:39:57 +0000 2020', 'id': 1311043058117550087, 'id_str': '1311043058117550087', 'text': 'Another tweet', 'truncated': False, 'entities': {'hashtags': [], 'symbols': [], 'user_mentions': [], 'urls': []}, 'source': '', 'in_reply_to_status_id': None, 'in_reply_to_status_id_str': None, 'in_reply_to_user_id': None, 'in_reply_to_user_id_str': None, 'in_reply_to_screen_name': None, 'user': {'id': 1308331480553267202, 'id_str': '1308331480553267202', 'name': 'nszuiltest33332', 'screen_name': 'nszuiltest33332', 'location': '', 'description': 'this is a testing account for a school project because I MUST use twitter and no other services that also offer an API', 'url': None, 'entities': {'description': {'urls': []}}, 'protected': True, 'followers_count': 0, 'friends_count': 0, 'listed_count': 0, 'created_at': 'Tue Sep 22 09:05:14 +0000 2020', 'favourites_count': 0, 'utc_offset': None, 'time_zone': None, 'geo_enabled': False, 'verified': False, 'statuses_count': 3, 'lang': None, 'contributors_enabled': False, 'is_translator': False, 'is_translation_enabled': False, 'profile_background_color': 'F5F8FA', 'profile_background_image_url': None, 'profile_background_image_url_https': None, 'profile_background_tile': False, 'profile_image_url': 'http://abs.twimg.com/sticky/default_profile_images/default_profile_normal.png', 'profile_image_url_https': 'https://abs.twimg.com/sticky/default_profile_images/default_profile_normal.png', 'profile_link_color': '1DA1F2', 'profile_sidebar_border_color': 'C0DEED', 'profile_sidebar_fill_color': 'DDEEF6', 'profile_text_color': '333333', 'profile_use_background_image': True, 'has_extended_profile': True, 'default_profile': True, 'default_profile_image': True, 'following': False, 'follow_request_sent': False, 'notifications': False, 'translator_type': 'none'}, 'geo': None, 'coordinates': None, 'place': None, 'contributors': None, 'is_quote_status': False, 'retweet_count': 0, 'favorite_count': 0, 'favorited': False, 'retweeted': False, 'lang': 'en'} +#test = {'created_at': 'Tue Sep 29 20:39:57 +0000 2020', 'id': 1311043058117550087, 'id_str': '1311043058117550087', 'text': 'Another tweet', 'truncated': False, 'entities': {'hashtags': [], 'symbols': [], 'user_mentions': [], 'urls': []}, 'source': '', 'in_reply_to_status_id': None, 'in_reply_to_status_id_str': None, 'in_reply_to_user_id': None, 'in_reply_to_user_id_str': None, 'in_reply_to_screen_name': None, 'user': {'id': 1308331480553267202, 'id_str': '1308331480553267202', 'name': 'nszuiltest33332', 'screen_name': 'nszuiltest33332', 'location': '', 'description': 'this is a testing account for a school project because I MUST use twitter and no other services that also offer an API', 'url': None, 'entities': {'description': {'urls': []}}, 'protected': True, 'followers_count': 0, 'friends_count': 0, 'listed_count': 0, 'created_at': 'Tue Sep 22 09:05:14 +0000 2020', 'favourites_count': 0, 'utc_offset': None, 'time_zone': None, 'geo_enabled': False, 'verified': False, 'statuses_count': 3, 'lang': None, 'contributors_enabled': False, 'is_translator': False, 'is_translation_enabled': False, 'profile_background_color': 'F5F8FA', 'profile_background_image_url': None, 'profile_background_image_url_https': None, 'profile_background_tile': False, 'profile_image_url': 'http://abs.twimg.com/sticky/default_profile_images/default_profile_normal.png', 'profile_image_url_https': 'https://abs.twimg.com/sticky/default_profile_images/default_profile_normal.png', 'profile_link_color': '1DA1F2', 'profile_sidebar_border_color': 'C0DEED', 'profile_sidebar_fill_color': 'DDEEF6', 'profile_text_color': '333333', 'profile_use_background_image': True, 'has_extended_profile': True, 'default_profile': True, 'default_profile_image': True, 'following': False, 'follow_request_sent': False, 'notifications': False, 'translator_type': 'none'}, 'geo': None, 'coordinates': None, 'place': None, 'contributors': None, 'is_quote_status': False, 'retweet_count': 0, 'favorite_count': 0, 'favorited': False, 'retweeted': False, 'lang': 'en'} -print(test['id']) \ No newline at end of file +#print(test['id']) \ No newline at end of file