Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import json
- from snapp_email import ApiClient
- from snapp_email.datacontract import classes
- import examples.config as config
- import datetime
- import examples.create_post as create_post
- import requests
- import schedule
- import time
- integration_key = "36f8055c-173a-4d3a-a7af-88c5c0a4b3c1@4thoffice.com"
- integration_secret = "1f031a3d-9b95-d738-fbc7-1ab7125a9dc4"
- mylist=["Trip","Travel","trip","travel","flight","Flight","vacation","Vacation","tour","Tour","Outing"
- ,"outing","weekend","Weekend","expedition","Expedition","roundabout","Roundabout","break","Break","Layoff","Rest"
- ,"layoff","rest","lay off","Lay off","leisure","Leisure","day off","Day off","arrival", "Arrival","Departure","Ride","Swing","Cruise"
- ,"departure","ride", "swing","cruise","holiday","Holiday"]
- #,"izak1996@gmail.com",
- #"luka.todorovic.boznar@gmail.com"
- #"masa.petan@gmail.com",
- #"vehovecnina@gmail.com"
- email_addresses=["luka.todorovic.boznar@gmail.com"]
- api_client = ApiClient(integration_key, integration_secret, 7, client_codename="THE 1st Of 4O", api_url="https://clean-sprint-app.4thoffice.com/api")
- def get_agenda_sdk(api_client,start_datetime, end_datetime=None, size=100, offset=0, impersonate_user_id=None):
- agenda = api_client.appointment.ListOfAgendaPage_22.get(None, start_datetime, size, offset, end_datetime, impersonate_user_id)
- return agenda
- def user_id_for_email_sdk(email_address):
- api_client = ApiClient(username=config.INTEGRATION_KEY,
- password=config.INTEGRATION_SECRET,
- auth_type=7,
- api_url=config.API_URL)
- user = create_post.create_user_object_from_email(email_address)
- stream_user = classes.StreamUser_22(User=user)
- response = api_client.stream.StreamUser_22.create(stream_user)
- return response.User.Id
- def create_card_html_sdk(api_client, to_user_email, title, content, attachment_names_ids=None):
- share_list = []
- share_list.append(create_post.create_user_object_from_email(to_user_email))
- share_list = classes.ListOfResources_13(share_list)
- post_obj = classes.Post_22(BodyHtml=content, ShareList=share_list)
- if attachment_names_ids:
- files = []
- for attachment_id in attachment_names_ids:
- file = classes.File_14()
- file.set_Id(attachment_id)
- files.append(file)
- post_obj.set_Files(classes.ListOfFiles_14(files))
- post_obj.set_Name(title)
- created_post = api_client.post.Post_22.create(post_obj)
- return created_post
- def exists(path):
- r = requests.head(path)
- return r.status_code == requests.codes.ok
- def get_accommodation_email_string(location, json):
- text = '''<span style="font-weight: bold; font-size: 18px;">Hi there!</span><br>
- <span>Seems like you are planning a trip to ''' + location + '''</span><br><br>
- <span>Here is a super easy way to focus on your work instead of spending lots of time searching for the right accommodation.</span><br><br>
- <span>In the description below are 3 hotels, based on the ratings from previous guests.
- I think one of them will definitely suit your needs, so check them out. ;)</span>
- <table style="width: 100%; margin-bottom: 20px;" cellspacing="10">
- <tr>
- '''
- for i in range(3):
- j = 0
- for j in range(len(json['results'][i]['images'])):
- if exists(json['results'][i]['images'][j]['source_url']):
- img_url = json['results'][i]['images'][j]['source_url']
- break
- elif j == len(json['results'][i]['images']) - 1:
- img_url = 'http://drkeyurparmar.com/wp-content/uploads/2015/02/dummy-article-img.jpg'
- if len(json['results'][i]['images']) == 0:
- img_url = 'http://drkeyurparmar.com/wp-content/uploads/2015/02/dummy-article-img.jpg'
- text += '''
- <td style="width: 33.33%; height: 100px; margin: 20px; text-align: center; padding: 5px; vertical-align: top;">
- <h4>{}</h4>
- <img src="{}" style="max-width: 100%;">
- <span>{}</span>
- <a href="{}" style="margin-top: 10px; display: block;">Read more...</a>
- </td>
- '''.format(json['results'][i]['name'], img_url,
- json['results'][i]['snippet'], json['results'][i]['booking_info']['vendor_object_url'])
- text += '''</tr></table>
- <span>I hope that these recommendations were of help and that you managed to find your perfect stay.</span><br><br>
- <span>Have a great time in ''' + location + '''!</span><br>
- 4th Office - your best friend in email management.'''
- return text
- def get_sightseeing_email_string(location, json):
- text = '''<span style="font-weight: bold; font-size: 18px;">Hi there!</span><br><br>
- <span>There are a lot of great attractions waiting for you in ''' + location + '''.</span>
- <span>In case you find some free time as your schedule is probably filled with work I found 9 great places you should visit.
- They are chosen depending on the rating and recommendations from previous tourists so they should fit your needs.</span>
- <table style="width: 100%; margin-bottom: 20px;" cellspacing="10">
- '''
- for i in range(3):
- text += '<tr>'
- for j in range(3):
- k = 0
- for k in range(len(json['results'][i * 3 + j]['images'])):
- if exists(json['results'][i * 3 + j]['images'][k]['source_url']):
- img_url = json['results'][i * 3 + j]['images'][k]['source_url']
- break
- elif k == len(json['results'][i * 3 + j]['images']) - 1:
- img_url = 'http://drkeyurparmar.com/wp-content/uploads/2015/02/dummy-article-img.jpg'
- if len(json['results'][i * 3 + j]['images']) == 0:
- img_url = 'http://drkeyurparmar.com/wp-content/uploads/2015/02/dummy-article-img.jpg'
- snippet = (json['results'][i * 3 + j]['snippet'][:120] + '...') if len(
- json['results'][i * 3 + j]['snippet']) > 120 else json['results'][i * 3 + j]['snippet']
- text += '''
- <td style="width: 33.33%;margin: 20px; text-align: center; padding: 5px; vertical-align: top;">
- <h4>{}</h4>
- <img src="{}" style="max-width: 100%;">
- <span>{}</span>
- <a href="{}" style="margin-top: 10px; display: block;">Read more...</a>
- </td>
- '''.format(json['results'][i * 3 + j]['name'], img_url,
- snippet, json['results'][i * 3 + j]['attribution'][0]['url'])
- text += '</tr>'
- text += '''</table><span>Hope you like them!</span><br>
- 4th Office - your best friend in email management.'''
- return text
- def get_location_url(lat, long):
- latstr = "{:.6f}".format(lat)
- longstr = "{:.6f}".format(long)
- link_to_location = 'http://maps.google.com/maps?q=loc:' + latstr + ',' + longstr
- return link_to_location
- def get_restaurant_email_string(location, json):
- text = '''<span style="font-weight: bold; font-size: 18px;">Hi there!</span><br><br>
- <span>Your trip to {} is approaching. There are only two days left until exploring that place.</span><br><br>
- <span>Here are {} restaurants and cafes, so you won`t go hungry while working abroad.</span><br>
- <ul>'''.format(location, len(json['results']))
- for i in range(len(json['results'])):
- text += '<li><a href="' + get_location_url(json['results'][i]['coordinates']['latitude'],
- json['results'][i]['coordinates']['longitude']) + '">' + \
- json['results'][i]['name'] + '</a></li>\n'
- text += '''</ul><br>There is nothing better than a good meal so I hope you like these suggestions! :)<br><br>
- Enjoy local specialities in ''' + location + '''.<br>
- 4th Office - your best friend in email management.
- '''
- return text
- def api_information_hotels(location,email):
- title = " Your accommodation for "+location
- category = "hotels"
- r = requests.get('https://www.triposo.com/api/v2/poi.json?location_id=' + location + '&tag_labels=' + category + '&order_by=-score&count=3&fields=name,coordinates,booking_info,snippet,images&account=R7M1ZPHW&token=vm563syh3v72rujab3csnhtfanwl15ak')
- json = r.json()
- #link_to_location=
- string= get_accommodation_email_string(location,json)
- create_card_html_sdk(api_client, email, title, string)
- def api_information_sightseeing(location, email):
- title = "Places to visit in " + location
- category = "sightseeing"
- r = requests.get(
- 'https://www.triposo.com/api/v2/poi.json?location_id=' + location + '&tag_labels=' + category + '&order_by=-sightseeing_score&count=9&fields=name,snippet,coordinates,score,images,attribution&account=R7M1ZPHW&token=vm563syh3v72rujab3csnhtfanwl15ak')
- json = r.json()
- string=get_sightseeing_email_string(location,json)
- create_card_html_sdk(api_client, email, title, string)
- def api_information_restaurants(location, email):
- title = "Where to dine in " + location
- category = "eatingout"
- r = requests.get(
- "https://www.triposo.com/api/v2/poi.json?location_id=" + location + "&tag_labels=" + category + "&order_by=-eatingout_score&count=10&fields=id,name,snippet,coordinates,images&account=R7M1ZPHW&token=vm563syh3v72rujab3csnhtfanwl15ak")
- json = r.json()
- string=get_restaurant_email_string(location,json)
- create_card_html_sdk(api_client, email, title, string)
- def job():
- for k in range(len(email_addresses)):
- print(email_addresses[k])
- agenda = api_client.appointment.ListOfAgendaPage_22.get(None, datetime.datetime.today(), 100, 0,
- datetime.datetime.today() + datetime.timedelta(30),
- user_id_for_email_sdk(email_addresses[k]))
- for i in range(len(agenda.AgendaList.Resource)):
- todaydate = datetime.datetime.today().strftime("%Y-%m-%d")
- name = agenda.AgendaList.Resource[i].AgendaItem.Parent.Name
- locat = agenda.AgendaList.Resource[i].AgendaItem.Location
- if(locat is None):
- continue
- locationtown = locat.split(",")
- loc=locationtown[0].replace(" ", "_")
- if (todaydate in agenda.AgendaList.Resource[i].AgendaItem.Created):
- for j in range(len(mylist)):
- if (mylist[j] in name):
- api_information_hotels(loc,email_addresses[k])
- print(loc)
- date = datetime.datetime.strptime(todaydate, "%Y-%m-%d")
- date_2 = date + datetime.timedelta(days=2)
- if (date_2.strftime("%Y-%m-%d") in agenda.AgendaList.Resource[i].DueDate):
- for j in range(len(mylist)):
- if (mylist[j] in name):
- api_information_restaurants(loc,email_addresses[k])
- api_information_sightseeing(loc, email_addresses[k])
- print(loc)
- schedule.every(0.1).minutes.do(job)
- #schedule.every().day.at("19:51").do(job)
- while True:
- schedule.run_pending()
- time.sleep(10)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement