Skip to content

Instantly share code, notes, and snippets.

@feliciaan
Forked from pecigonzalo/mbox2gg.py
Created August 4, 2019 22:37
Show Gist options
  • Save feliciaan/1b363227561f04a0c20ac4b7bc32ac75 to your computer and use it in GitHub Desktop.
Save feliciaan/1b363227561f04a0c20ac4b7bc32ac75 to your computer and use it in GitHub Desktop.
mbox to Google Groups
#! /usr/bin/env python
# Import a mbox file to a Google Group using https://developers.google.com/admin-sdk/groups-migration/index
# You'll need to install https://developers.google.com/api-client-library/python/
# and enable Groups Migration API, read prerequisits of the API SDK
from __future__ import print_function
import mailbox
import StringIO
import time
from os import listdir
from os.path import isfile, join
import apiclient
import httplib2
from apiclient import discovery
from apiclient.errors import MediaUploadSizeError
from oauth2client import client, tools
from oauth2client.file import Storage
try:
import argparse
flags = argparse.ArgumentParser(parents=[tools.argparser]).parse_args()
except ImportError:
flags = None
# The email address of the group to import to
groupId = raw_input('Enter groupId: ')
# https://console.developers.google.com/project/mysociety-groups-import/apiui/credential
# Generate a Client ID for Native Application.
# You'll be prompted to complete an auth flow on the first run.
# The user will need to be an admin.
scope = 'https://www.googleapis.com/auth/apps.groups.migration'
storage = Storage('credentials.dat')
credentials = storage.get()
if not credentials or credentials.invalid:
client_id = raw_input('Enter client_id: ')
client_secret = raw_input('Enter client_secret: ')
flow = client.OAuth2WebServerFlow(client_id, client_secret, scope)
if flags:
credentials = tools.run_flow(flow, storage, flags)
else:
# Needed only for compatibility with Python 2.6
credentials = tools.run(flow, storage)
http = credentials.authorize(httplib2.Http())
service = discovery.build('groupsmigration', 'v1', http=http)
mbox_folder_path = raw_input('Enter folder containing mbox files: ')
mboxes = [f for f in listdir(mbox_folder_path) if isfile(join(mbox_folder_path, f)) and '.mbox' in f]
print(mboxes)
number_of_mboxes = len(mboxes)
for mbox_index, mbox_file in enumerate(mboxes,1):
print(mbox_file)
mbox_path = join(mbox_folder_path, mbox_file)
mb = mailbox.mbox(mbox_path) # The path of the mbox file to import
total_messages = len(mb)
for i, msg in enumerate(mb,1):
stream = StringIO.StringIO()
stream.write(msg.as_string())
media = apiclient.http.MediaIoBaseUpload(
stream, mimetype='message/rfc822')
try:
response = service.archive().insert(
groupId=groupId, media_body=media).execute()
print('Message {} of {} ({}/{}): {}'.format(
i,
total_messages,
mbox_index,
number_of_mboxes,
response['responseCode'])
)
except MediaUploadSizeError:
print('Message {} of {} ({}/{}) has an attachment which is too large.'.format(
i,
total_messages,
mbox_index,
number_of_mboxes
))
except:
print('Message {} of {} ({}/{}): an error has occured'.format(
i,
total_messages,
mbox_index,
number_of_mboxes
))
time.sleep(0.1)
print('Done.')
@Yeikop
Copy link

Yeikop commented Jan 16, 2021

Hi ,

I have a problem when I execute your code, I receive this error;

Traceback (most recent call last):
File "mboxggfeli.py", line 10, in
import StringIO
ModuleNotFoundError: No module named 'StringIO'

I am not developer if you can help me please? I really appreciate your help

Thank you so much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment